(20 pts) Exercise 2-18: Pseudo-instructions • Below you will see several problems of the form: li $t1, small # $t1 = small • This is an example pseudo-instruction, with its meaning given as a comment. The instruction should load ‘small’ into $t1, where ‘small’ means a constant that fits within 16 bits. The constant is also called an ‘immediate’ – ‘li’ stands for ‘load immediate’ Your job is to write the real MIPS instruction (or sequence of instructions) that the compiler would produce for each given pseudo-instruction. For instance, the solution for the above pseudo-instruction is: addi $t1, $zero, small • • • In the problems, ‘small’ and ‘big’ refer to some arbitary constants (numbers). A ‘big’ constant needs 32 bits to be represented. You will need some notation to talk about the upper (most significant) 16 bits and the lower (least significant) bits of this number. Use UPPER(big) to refer to the most significant bits and LOWER(big) to refer to the other bits. To make your answers simpler, you may make use of the ‘li’ psuedoinstruction where helpful (except(beq, when defining li itself). Do this it will NOTES: Remember that branches bne) must take two registers – no–constants. make job easier!! Andyour load/stores cannot be of the form “lw $t2, $t3($t4)” – the memory address must be a register plus a base, not two registers added together. Also, for temp space must use reserved register $at, so don’t clobber needed values in other registers. • clear $to # $t0 = 0 add $t0, $zero, $zero • beq $t3, small, L # if ($t3 == small) go to L • beq $t2, big, L # if ($t2 == big) go to L • li $t2, big # $t2 = big • bge $t5, $t4, L # if ($t5 >= $t4) go to L li $at, small beq $t3, $at, L li $at, big beq $t2, $at, L lui $t2, UPPER(big) ori $t2, $t2, LOWER(big) slt $at, $t5, $t4 beq $at, $zero, L • lw $t0, big($t2) # t0 = Memory[$t2+big] li $at, big add $at, $t2, $at lw $t0, 0($at) (5 pts) Exercise 2-21 • f $s0 a.) What is the MIPS assembly code for the following: g $s1 do { h $s2 i $s3 g = g + j; j $s4 } while (g <h); Variables f to j are assigned to registers $s0 to $s4 Use $v0, $v1 as temporaries if needed b.) Did your solution use any pseudo-instructions? YES or NO Loop: add $s1, $s1, $s4 blt $s1, $s2, Loop YES, blt is pseudo-instruction (5 pts) Exercise 2-22 • f $s0 a.) What is the MIPS assembly code for the following: g $s1 do { h $s2 i $s3 g = g + j; j $s4 } while (g <100); Variables f to j are assigned to registers $s0 to $s4 Use $v0, $v1 as temporaries if needed b.) Did your solution use any pseudo-instructions? YES or NO Loop: add $s1, $s1, $s4 slti $v0, $s1, 100 bne $v0, $zero, Loop NO, no pseudo-instructions # v0 = 1 if (g < 100) # branch if v0 = 1 (5 pts) Exercise 2-23 • Note different test at start f $s0 a.) What is the MIPS assembly code for the following: g $s1 while (g < i) { h $s2 i $s3 g = g + j; j $s4 } Variables f to j are assigned to registers $s0 to $s4 Use $v0, $v1 as temporaries if needed b.) Did your solution use any pseudo-instructions? YES or NO Must test first, but many possible ways to do this. Below is one possibility. Loop: Test: j Test # need to test first add $s1, $s1, $s4 blt $s1, $s3, Loop YES, blt is pseudo-instruction (10 pts) Exercise 2-24 • f $s0 a.) What is the MIPS assembly code for the following: g $s1 while (g > i) { h $s2 i $s3 g = g + 3; j $s4 } Variables f to j are assigned to registers $s0 to $s4 Use $v0, $v1 as temporaries if needed b.) Did your solution use any pseudo-instructions? YES or NO Loop: Test: j Test addi $s1, $s1, 3 bgt $s1, $s3, Loop # need to test first YES, bgt is pseudo-instruction (15 pts) Exercise 2-26 • • • The MIPS translation of the C segment: while (save[i] == k) i = i + 1; is given on page 92-93 of the textbook as follows: Loop: sll $t1, $s3, 2 # temp reg t1 = i *4 add $t1, $t1, $s6 # t1 = address of save[i] lw $t0, 0($t1) # temp reg t0 = save[i] bne $t0, $s5, Exit # goto Exit if save[i] != k addi $s3, $s3, 1 # loop body: i = i + 1 j Loop # repeat the loop This code uses both a conditional branch and an unconditional jump each time through the loop. Only poor compilers would produce code with this loop overhead. Rewrite the assembly code so that it uses at most one branch or jump each time through the loop. You will have to think carefully about how to reorganize things to make this possible. Make sure that your final version still computes the same result as the original code! Optimized version: (Basically we want to change the ‘bne’ to be a branch to continue the loop, instead of a branch to exit the loop followed by a jump. However, we then have to move the add to $s3 instruction so that it is executed inside the loop, but not in the first iteration (otherwise, $s3(i) would have the wrong value – either when used for finding save[i] or for the value when the loop is finished). Below is one possible way – move the add to the top of the loop and skip it on the first iteration. Another way would be to place the add just before the branch, and undo its effect by doing a subtraction to $s3 when we exit the loop.) j Test # first time skip to the test Loop: addi $s3, $s3, 1 # loop body: i++ Test: sll $t1, $s3, 2 # temp reg t1 = 4 *i add $t1, $t1, $s6 # t1 = address of save[i] lw $t0, 0($t1) # temp reg t0 = save[i] beq $t0, $s5, Loop # repeat loop if save[i] == k (20 pts) Exercise 2-27 • • • (10 pts) Add comments to the MIPS code above. Assume that $a0 and $a1 are used for the input and both initially contain the integers ‘a’ and ‘b’, respectively. You may assume ‘a’ and ‘b’ are both greater than zero. (10 pts) In one simple sentence, what does this code compute (in terms of ‘a’ and ‘b’)? The code computes a * b + 100