Homework 4 Solution cs345 1. Multi-part a) #assume the declaration of the following string xGTEy: .asciiz "x is greater than or equal to y\n" #assume x is in $t1 and y is in $t2 sge $t3, $t1, $t2 beq $t3, $zero, endif li $v0, 4 la $a0, xGTEy syscall endif: b) #assume the declaration of the following string xLTz: .asciiz "x is less than z\n” #assume x is in $t1, y is in $t2, and z is in $t3 slt $t4, $t1, $t2 slt $t5, $t2, $t3 and $t6, $t4, $t5 beq $t6, $zero, endif li $v0, 4 la $a0, xLTz syscall endif: c) #assume the declaration of the following strings xGTz: .asciiz "x might be greater than z\n" xGLTyz: .asciiz "x is greater than or equal to y and less than z\n" #assume x is in $t1, y is in $t2, and z is in $t3 slt $t4, $t1, $t2 sge $t5, $t1, $t3 or $t6, $t4, $t5 beq $t6, $zero, else #branch to else li $v0, 4 la $a0, xGTz syscall b endif #always branch to endif else: li $v0, 4 la $a0, xGLTyz syscall endif: d) #assume the declaration of the following strings lessThanOrEq: .asciiz " is less than or equal to " asIs: .asciiz " as is " newLine: .asciiz "\n" #assume x is in $s1, y is in $s2, and z is in $s3 add $t1, $s1, $s2 #x + y mul $t2, $s1, $s2 #x * y sle $t4, $t1, $s3 #x + y <= z sle $t5, $t2, $s3 #x * y <= z and $t6, $t4, $t5 #x+y <= z && x*y <= z beq $t6, $zero, endif li $v0, 1 #print x+y move $a0, $t1 syscall li $v0, 4 #print a string la $a0, lessThanOrEq syscall li $v0, 1 #print z move $a0, $s3 syscall li $v0, 4 #print "as is" la $a0, asIs syscall li $v0, 1 # print x*y move $a0, $t2 syscall li $v0, 4 #print newLine la $a0, newLine syscall endif: 2. Multi-part question a) sub $s2, $t3, $t5 000000 01011 01101 10010 00000 100010 opcode rs rt rd shamt funct 0000 0001 0110 1101 1001 0000 0010 0010 0x016d9022 b) addi $t1, $s2, 127 001000 10010 01001 0000 0000 0111 1111 opcode rs rt immediate 0010 0010 0100 1001 0000 0000 0111 1111 0x2249007f c) lw $s3, 12($s4) 100011 10100 10011 0000 0000 0000 1100 opcode rs rt immediate 1000 1110 1001 0011 0000 0000 0000 1100 0x8e93000c d) or $t3, $t4, $t5 000000 01100 01101 01011 00000 100101 opcode rs rt rd shamt funct 0000 0001 1000 1101 0101 1000 0010 0101 0x018d5825 3. #AreaCalculator.asm # #A program to compute the area of a triangle or a rectangle #Inputs: 1 for a triangle and 2 for a rectangle # The length of the side of a square or the circle radius .data inputPrompt: .asciiz "Enter 1 for triangle 2 for rectangle: " trianglePrompt: .asciiz "Enter the base and height of the triangle: " rectanglePrompt: .asciiz "Enter the height and width of the rectangle: " incorrectInput: .asciiz "Invalid input\n" outputData: .asciiz "The area is " newline: .asciiz "\n" two: .double 2.0 .text main: #Prompt user for input la $a0, inputPrompt li $v0, 4 syscall #Read an integer la $v0, 5 syscall move $t1, $v0 #triangle #if t1 == 1 beq $t1, 2, elseif bne $t1, 1, else #prompt for triangle la $a0, trianglePrompt li $v0, 4 syscall l.d $f4, two #read base into $f0 li $v0, 7 syscall mov.d $f2, $f0 #read height into $f0 li $v0, 7 syscall #compute and print area la $a0, outputData li $v0, 4 syscall mul.d $f12, $f2, $f0 div.d $f12, $f12, $f4 li $v0, 3 syscall la $a0, newline li $v0, 4 syscall b endif elseif: #prompt for rectangle la $a0, rectanglePrompt li $v0, 4 syscall #read height into $f0 li $v0, 7 syscall mov.d $f2, $f0 #read width into $f0 li $v0, 7 syscall #compute and print area la $a0, outputData li $v0, 4 syscall mul.d $f12, $f0, $f2 li $v0, 3 syscall la $a0, newline li $v0, 4 syscall j endif else: la $a0, incorrectInput li $v0, 4 syscall endif: #end program li $v0, 10 syscall 4. Multi-part answer a) #assume the following constants const1: .double 0.332 #assume x is $f2 l.d $f0, const1 add.d $f2, $f2, $f0 b) #assume the following constants const1: .double 31.02 const2: .double 6.2 #assume x is in $f2, and y is in $f4 l.d $f0, const1 l.d $f6, const2 mul.d $f6, $f6, $f4 #6.2*y sub.d $f6, $f6, $f2 #6.2*y - x div.d $f4, $f6, $f0 #(6.2*y - x) * 31.02 c) #assume the following constants one: .double 1.0 four: .double 4.0 three: .double 3.0 five: .double 5.0 #assume z is in $f2 l.d $f4, one l.d $f6, four l.d $f8, three l.d $f10, five div.d $f10, $f10, $f8 # 5.0/3.0 mul.d $f10, $f10, $f6 # 5.0/3.0*4.0 add.d $f2, $f10, $f4 # 4.0/3.0*4.0+1.0 d) #assume the following constants const1: .float 4.13 #assume f is in $f0, a is in $f2, b is in $f4, and c is in $f6 l.s $f8, const1 #load const1 cvt.d.s $f10, $f8 #convert const1 to a double mul.d $f0, $f4, $f6 #b * c div.d $f0, $f0, $f10 #b * c / 4.13 add.d $f0, $f0, $f2 #a + b * c / 4.13 5. Multi-part question a) x < y && y < z Assume x is $t1, y is $t2, and z is $t3 slt $t4, $t1, $t2 slt $t5, $t2, $t3 and $t6, $t4, $t5 b) x <= y || y <= z Assume x is $t1, y is $t2, and z is $t3 sle $t4, $t1, $t2 sle $t5, $t2, $t3 or $t6, $t4, $t5 c) x | y & z Assume x is $t1, y is $t2, and z is $t3 and $t4, $t2, $t3 or $t5, $t1, $t4 d) (a + b) < (c / d) Assume a is $t1, b is $t2, c is $t3, and d is $t4 add $t5, $t1, $t2 div $t6, $t3, $t4 slt $t7, $t6, $t5