Homework #4

1. Convert the following conditional statements into MIPS assembly language.
For decision making, use only the slt, beq, and bne instructions.

Assume: a is in $t4,b is in $s0, c is in $s2. a,b and c are signed. (I've created some labels to make it easier - you may need more or different labels depending on your implementation)

if(a<b)
{
    a=c+a
}
after_if:
c=c+a
 
Part 1:
    slt $t0,$t4,$s0
    beq $t0,$0,after_if
    add $t4,$s2,$t4
after_if:
    add $s2,$s2,$t4

Part 2:
    sltu $t0,$t4,$s0
    beq $t0,$0,after_if
    addu $t4,$s2,$t4
after_if:
    addu $s2,$s2,$t4

Part 3:
    bge $t4,$s0,after_if
    add $t4,$s2,$t4
after_if:
    add $s2,$s2,$t4


do
{
loop:
    b=b-a
} while(a!=b)
c=c+a
 

Part 1:
loop:
    sub $s0,$s0,$t4
    bne $t4,$s0,loop
    add $s2,$s2,$t4

Part 2:
loop:
   subu $s0,$s0,$t4
   bne $t4,$s0,loop
   addu $s2,$s2,$t4

Part 3 (No change from 1):
loop:
    sub $s0,$s0,$t4
    bne $t4,$s0,loop
    add $s2,$s2,$t4


if(a<=b)
{
    b=b+b
}
after_if:
c=c+a

Part 1:
    slt $t0,$s0,$t4
    bne $t0,$0,after_if
    add $s0,$s0,$s0
after_if:
    add $s2,$s2,$t4

Part 2:
    sltu $t0,$s0,$t4
    bne $t0,$0,after_if
    addu $s0,$s0,$s0
after_if:
    addu $s2,$s2,$t4

Part 3:
    bgt $t4,$s0,after_if
    add $s0,$s0,$s0
after_if:
    add $s2,$s2,$t4

while(a>b)
{
    b=b-a
}
after_while:
c=c+a
Part 1:
while:
    slt $t0,$s0,$t4
    beq $t0,$0,after_while
    sub $s0,$s0,$t4
    j while
after_while:
    add $s2,$s2,$t4

Part 2:
while:
    sltu $t0,$s0,$t4
    beq $t0,$0,after_while
    subu $s0,$s0,$t4
    j while
after_while:
    addu $s2,$s2,$t4

Part 3:
while:
    ble $t4,$s0,after_while
    sub $s0,$s0,$t4
    j while
after_while:
    add $s2,$s2,$t4
 

for(a=b+b;a<10;a++)
{
    b=b+a
}
c=c+a
Part 1:
    add $t4,$s0,$s0    addi $t5,$0,10
loop_tst:
    slt $t0,$t4,$t5

    beq $t0,$0,end_loop
    add $s0,$s0,$st # b=b+a
    addi $t4,$t4,1 # a++
    j loop_tst
end_loop:
    add $s2,$s2,$t4

Part 2:
    addu $t4,$s0,$s0
loop_tst:
     addiu $t5,$0,10     sltu $t0,$t4,$t5

    beq $t0,$0,end_loop
    addu $s0,$s0,$st # b=b+a
    addiu $t4,$t4,1 # a++
    j loop_tst
end_loop:
    addu $s2,$s2,$t4

Part 3:
    add $t4,$s0,$s0
loop_tst:
    bge $t4,10,end_loop

    add $s0,$s0,$st # b=b+a
    addi $t4,$t4,1 # a++
    j loop_tst
end_loop:
    add $s2,$s2,$t4
 

2. How would you solution to the above change if a and b were unsigned? (Re-Write the solution)

3. How could you "simplify" problem 1 if you used pseudo-ops? (Re-Write the solution)
   (Your new solution should at least be easier to read/understand)