66.2k views
5 votes
Implement the following high-level code segments using the slt instruction. Assume the integer variables g and h are in registers $s0 and $s1, respectively. (MIPS Instruction Set Summary is given in page 8)

i. If (g > h)

g = g + h;

else

g = g − h;ii. If (g >= h)

g = g + 1;

else

h = h − 1;iii. If (g <= h)

g = 0;

else

h = 0;

1 Answer

5 votes
i. If (g > h)

slt $t0, $s0, $s1

beq $t0, $zero, ELSE

add $s0, $s0, $s1

j EXIT

ELSE:sub $s0, $s0, $s1

EXIT:

ii. If (g >= h)

slt $t0, $s1, $s0

beq $t0, $zero, ELSE

addi $s0, $s0, 1

j EXIT

ELSE:subi $s1, $s1, 1

EXIT:

iii. If (g <= h)

slt $t0, $s1, $s0

beq $t0, $zero, ELSE

addi $s0, $zero, 0

j EXIT

ELSE: addi $s1, $zero, 0

EXIT:
User Tbergelt
by
8.3k points