(20 pts) Exercise 2-28 • • • • (10 pts) Add comments to the MIPS code above. This code processes two arrays and produces an important value in register $v0. Assume that each array consists of 2500 words indexed 0 through 2499, that the base addresses of the arrays are stored in $a0 and $a1 respectively, and their sizes (2500) and stored in $a2 and $a3, respectively. In your comments, call the arrays Array1 and Array2. (10 pts) In one sentence, what does this code compute and store in $v0? Be very specific (make sure that your answer doesn’t have more than one interpretation of what it means) The code determines the total number of matching elements between the two arrays (in any position) and returns this number in register $v0. Another way of saying this is as follows: Let ci = the number of times that array1[i] appears at any position in array2. Then this program computes c0 + c1 + … + c2499. (5 pts) Exercise 2-31 • Suppose you are given the code for the following function: int function1(int a, int b); Write MIPS code to call function1(3, 7) and then store the result in $s0 addi $a0, $zero, 3 addi $a1, $zero, 7 jal function1 move $s0, $v0 (5 pts) Exercise 2-32 • Now you have this definition for function1: int function1(int a, int b) { return (a – b); } Write MIPS code to define function1. function1: sub $v0, $a0, $a1 jr $ra (10 pts) Exercise 2-33 • Write MIPS code to define the following function: int cat(int a, int b) { cat: if (a < b) blt $a0, $a1, DoA # skip ahead if a<b return a; move $vo, $a1 # if not, retval = b else j Exit return b; DoA: } move $v0, $a0 # retval = a Exit: jr $ra # actually return (5 pts) Exercise 2-36 Save $ra b/c nested Save $a0 because need ‘a’ after sub-call • Write the MIPS code to define the following function int function2(int g, int h) { return g + function1(g, h); } (You will need to store something on the stack – why?) function2: addi $sp, $sp, -8 sw $ra, 4($sp) sw $a0, 0($sp) jal function1 lw $ra, 4($sp) lw $a0, 0($sp) addi $sp, $sp, 8 add $v0, $v0, $a0 jr $ra A VITAL exercise to understand for the project. (5 pts) MustExercise store results of2-37 function6(a) and param b (saving param ‘a’ is optional) • Soln A:We use $s0 to store result of function6 –so must save/restore $s0! Solnthe B: Store the stackfunction Write MIPS result code of to function6 define theon following int function3(int a, int b) { return function6(a) + function7(b); } (You will need to store something on the stack – why?) function3: # Solution A addi $sp, $sp, -16 sw $ra, 0($sp) sw $a0, 4($sp) sw $a1, 8($sp) sw $s0, 12($sp) # we modify, so must save! jal function6 # args in right place already move $s0, $v0 # save for later function3: addi $sp, $sp, -16 sw $ra, 0($sp) sw $a0, 4($sp) sw $a1, 8($sp) # Solution B jal function6 # args in right place already sw $v0, 12($sp) # save for later lw $a0, 8($sp) # get b jal function7 add $v0, $v0, $s0 # add two results lw $a0, 8($sp) # get b jal function7 lw $t0, 12($sp) # get function6 result back add $v0, $v0, $t0 # add two results lw $ra, 0($sp) lw $s0, 12($sp) # must restore addi $sp, $sp, 16 jr $ra lw $ra, 0($sp) addi $sp, $sp, 16 jr $ra (15 pts) Exercise 2-38 • Write the MIPS code to define the following function int lemur(int a, int b) lemur: { return panda(a) + b; } addi $sp, $sp, -8 sw $ra, 4($sp) sw $a1, 0($sp) # save ‘b’ for later # ‘a’ already in $a0, so call panda now jal panda # result now in $v0 # warning -- $a0, $a1, others now clobbered! # reload stored values and fix up stack lw $ra, 0($sp) lw $a1, 4($sp) # reload b (was clobbered) addi $sp, $sp, 8 # add panda result to ‘b’, put in $v0 add $v0, $v0, $a1 jr $ra (15 pts) Exercise 2-39 • Write the MIPS code to define the following alpaca: function int alpaca(int x, int y, int z) addi $sp, $sp, -8 function2: sw $ra, 4($sp) { int temp = ferret(y, z); $sp, -8# save ‘x’ for later swaddi $a1,$sp, 0($sp) return hedgehog(temp + x); sw $ra, 4($sp) } sw $a0, $a1, $a1 0($sp)# set # save later move first ‘b’ argfor to ‘y’ move $a1, $a2 # set second arg to ‘z’ ‘a’ already in $a0, so call jal#ferret # result nowpanda in $v0now (temp) jal panda # result now in $v0 lw $a0, 4($sp) # reload x # reload stored fix up (get stacktemp add $a0, $a0, $v0values # addand in ‘temp’ $ra, 0($sp) # call hedgehog(temp+x) jallw hedgehog lw $a1, 4($sp) # reload b addi $sp, $sp,values 8 # reload stored and fix up stack lw $ra, 0($sp) # add addi $sp,panda $sp, 8result to ‘b’, put in $v0 add $v0, $v0, $a1 $ra jr jr $ra