Homework 3 Solutions cs345 1. a) a = b + c * d - e Assume $s1 = a, $s2 = b, $s3 = c, $s4 = d, $s5 = e mul $t1, $s3, $s4 add $t1, $s2, $t1 sub $s1, $t1, $s5 b) a = a - 3 Assume $s1 = a subi $s1, $s1, 3 c) x = (y + z) * (z - y) Assume $s1 = x, $s2 = y, $s3 = z add $t1, $s2, $s3 sub $t2, $s3, $s2 mul $s1, $t1, $t2 d) f = (1 - x) * (1 - x) + 100*(y - x*x)*(y - x*x) Assume $s1 = f, $s2 = x, $s3 = y li $t1, 1 sub $t1, $t1, $s2 mul $t1, $t1, $t1 #$t1 is (1-x)^2 mul $t2, $s2, $s2 #$t2 is x^2 sub $t2, $s3, $t2 #$t2 is (y - x^2) mul $t2, $t2, $t2 #$t2 is (y - x^2)^2 mul $t2, $t2, 100 #$t2 is 100*(y - x^2)^2 add $s1, $t2, $t1 2. a) Convert 1101001 to decimal 75 = 1 + 8 + 32 + 64 b) convert 0111 1100 0100 1001 0110 1010 1011 1101 to hex 7 C 4 9 6 A B D c) Convert 18324 to binary 18324 % 2 = 0 9162 % 2 = 0 4581 % 2 = 1 2290 % 2 = 0 1145 % 2 = 1 572 % 2 = 0 286 % 2 = 0 143 % 2 = 1 71 % 2 = 1 35 % 2 = 1 17 % 2 = 1 8 % 2 = 0 4 % 2 = 0 2 % 2 = 0 1 result is 100011110010100 d) 100 0111 1001 0100 4 7 9 4 3. Program to print your name, course number, and career goals. #Program: Problem 3.asm .data #data declaration section Greeting: .asciiz "\nYour Name goes here!\n" CourseNum: .asciiz "CS" newLine: .asciiz "\n" CareerGoals: .asciiz "Your career goals go here\n" .text #start of code section main: #execution begins with the label "main" li $v0, 4 #system call code for printing string = 4 la $a0, Greeting # load address of string to be printed into $a0 syscall # call operating system to perform operation li $v0, 4 #system call code for printing string = 4 la $a0, CourseNum # load address of string to be printed into $a0 syscall # call operating system to perform operation li $v0, 1 #system call code for printing int = 4 li $a0, 345 # load address of string to be printed into $a0 syscall # call operating system to perform operation li $v0, 4 #system call code for printing string = 4 la $a0, newLine # load address of string to be printed into $a0 syscall # call operating system to perform operation li $v0, 4 #system call code for printing string = 4 la $a0, CareerGoals # load address of string to be printed into $a0 syscall # call operating system to perform operation li $v0, 10 #load termination code into $v0 syscall #terminate the program 4. Read, add, multiply, and find the mod of two integers, and print the result. #Program: Problem4.asm # #This problem provides the results of adding, multiplying, and finding the #remainder of two integers. .data #data declaration section IntegerPrompt: .asciiz "\nInput an integer: " AddResult: .asciiz "Adding the two integers gives a result of " MultResult: .asciiz "Multiplying the two integers gives a result of " RemResult: .asciiz "The remainder from dividing the two integers is " NewLine: .asciiz "\n" .text #start of code section main: #execution begins with the label "main" li $v0, 4 #system call code for printing string = 4 la $a0, IntegerPrompt # load address of string IntPrompt into $a0 syscall # call operating system to perform operation li $v0, 5 #read an integer value into $v0 syscall move $t1, $v0 #load the integer value into a temporary register li $v0, 4 #system call code for printing string = 4 la $a0, IntegerPrompt # load address of string IntPrompt into $a0 syscall # call operating system to perform operation li $v0, 5 #read an integer value into $v0 syscall move $t2, $v0 #load the integer value into a temporary register add $s1, $t1, $t2 #add the integers mul $s2, $t1, $t2 #multiply the integers rem $s3, $t1, $t2 #find the remainder li $v0, 4 #system call code for printing string = 4 la $a0, AddResult # load address of string AddResult into $a0 syscall # call operating system to perform operation li $v0, 1 #system call code for printing integer = 1 move $a0, $s1 #load value of $s1 into $a0 (move) syscall li $v0, 4 #system call code for printing string = 4 la $a0, NewLine #load address of NewLine into $a0 syscall #call operating system to perform operation li $v0, 4 #system call code for printing string = 4 la $a0, MultResult # load address of string MultResult into $a0 syscall # call operating system to perform operation li $v0, 1 #system call code for printing integer = 1 move $a0, $s2 #load value of $s2 into $a0 (move) syscall li $v0, 4 #system call code for printing string = 4 la $a0, NewLine #load address of NewLine into $a0 syscall #call operating system to perform operation li $v0, 4 #system call code for printing string = 4 la $a0, RemResult # load address of string RemResult into $a0 syscall # call operating system to perform operation li $v0, 1 #system call code for printing integer = 1 move $a0, $s3 #load value of $s3 into $a0 (move) syscall li $v0, 4 #system call code for printing string = 4 la $a0, NewLine #load address of NewLine into $a0 syscall #call operating system to perform operation li $v0, 10 #load termination code into $v0 syscall #terminate the program 5. a) li $v0, 1 lw $a0, 8($s1) syscall b) lw $t2, 8($s1) lw $t3, 24($s1) add $t1, $t2, $t3 # assume x is $t1 c) lw $t1, 16($s1) sw $t1, 28($s1) d) li $t1, 3 lw $t2, 24($s1) mul $t2, $t2, $t1 #arr[6]*3 lw $t1, 12($s1) add $t2, $t2, $t1 #arr[3]+ arr[6]*3 sw $t2, 0($s1)