Computer Science 371 Homework 2 solutions

Dr. Stephen Bloch

October 21, 1994

Assignment: problems 3.1–3.8, 3.11–3.17. 3.1

addi  $5,$5,-1

3.2

move  $5,$0

which is actually a pseudo-operation standing for something like

add   $5,$0,$0

3.3

sw    $0,1000

which actually stands for something like

sw    $0,1000($0)

Note that 1000 < 32767, and so the number 1000 fits inside the signed 16-bit offset field of an I-format instruction.

3.4

addi  $5,$5,1
blt   $5,$6,L1

which actually stands for something like

addi  $5,$5,1
slt   $1,$5,$6
bne   $0,$1,L1

3.5

addi  $5,$5,-1
bgt   $5,$0,L1

which actually stands for something like

addi  $5,$5,-1
slt   $1,$5,$6
bne   $0,$1,L1

Note that if we assume (as would normally be true in practice) that $5 is positive to begin with, sobgtr is equivalent to sobnz, i.e., “subtract one and branch if not equal to zero.” This could be converted into MIPS as two instructions:

addi  $5,$5,-1
bne   $5,$0,L1

3.6

addi  $11,$12,100

3.7

la    $11,4000000
lw    $12,44($11)   # note 44 = 4 · 11
add   $12,$12,$13
sw    $12,40($11)   # note 40 = 4 · 10

which actually stands for something like lui $11,61 # 4000000/65536

ori   $11,$11,2304  # 4000000 mod 65536
lw    $12,44($11)
add   $12,$12,$13
sw    $12,40($11)

3.8

loop:  lw    $3,0($4)      # Read next word from source
addi  $2,$2,1       # Increment count words copied
sw    $3,0($5)      # Write to destination
addi  $4,$4,4       # Advance pointer to next source
addi  $5,$5,4       # Advance pointer to next dest
bne   $3,$0,loop    # Loop if word copied ≠ zero

3.11 You can do this in several ways. I like the following:

       j     test
Loop:  add   $19,$19,$20
test:  mult  $9,$19,$10
       lw    $8,Sstart($9)
       beq   $8,$21,Loop

Notice that this code segment does an extra jump at the beginning, but in return it saves a jump every time through the loop. If the loop is iterated ten times, for example, the old code executes 5 instructions each of the first nine times, and 3 instructions the last time, for a total of 48 instructions; the new code, by contrast, executes 1 instruction at the beginning, 4 instructions each of the first nine times, and 3 instructions the last time, for a total of 40 instructions.

3.12

       beq   $19,$20,L1

3.13

       bne   $19,$20,L1

3.14 For the following couple of problems, note that the MIPS machine really has only two branch instructions, beq and bne, and only one condition-setting instruction slt that we’ve heard about; the others are all pseudo-instructions.

We’d like to say

       blt   $19,$20,L1

but the MIPS has no blt instruction. So instead we do

       slt   $1,$19,$20    # is $19 < $20?
       bne   $1,$0,L1      # if so, branch

3.15 We’d like to say

       ble   $19,$20,L1

but the MIPS has no ble instruction. So instead we do

       slt   $1,$20,$19    # is $20 < $19?
       beq   $1,$0,L1      # if not, then $19 ≤ $20 so branch

3.16 We’d like to say

bgt   $19,$20,L1

but the MIPS has no bgt instruction. So instead we do

slt   $1,$20,$19    # is $20 < $19?
bne   $1,$0,L1      # if so, then $19 > $20 so branch

3.17 We’d like to say

bge   $19,$20,L1

but the MIPS has no bge instruction. So instead we do

slt   $1,$19,$20    # is $19 < $20?
beq   $1,$0,L1      # if not, then $19 ≥ $20 so branch