b) Write MIPS assembly that implements ack correctly. m and n are passed in
a0
and
a1
,
respectively. We’ve provided the prologue and epilogue for you.
s0
and
s1
are saved and
restored, so you can safely use them if you need to preserve values across function calls.
addiu $sp, $sp, -12
sw
$ra, 0($sp)
sw
$s0, 4($sp)
sw
$s1, 8($sp)
# the m == 0 case
bne
$a0, $0, elseif
addiu $v0, $a1, 1
# answer = n + 1
j
epilogue
# the m != 0, n == 0 case
elseif:
bne
$a1, $0, else
addiu $a0, $a0, -1
# arg0 = m-1
li
$a1, 1
# arg1 = 1
jal
ack
# answer = ack(m-1, 1)
j
epilogue
# the final case
else:
addiu $a1, $a1, -1
# arg1 = n-1.
arg0 is already m.
move
$s0, $a0
# preserve m
jal
ack
# v0 = ack(m, n-1)
addiu $a0, $s0, -1
# arg0 = m-1
move
$a1, $v0
# arg1 = ack(m, n-1)
jal
ack
# answer = ack(m-1, v0)
epilogue:
lw
$ra, 0($sp)
lw
$s0, 4($sp)
lw
$s1, 8($sp)
addiu $sp, $sp, 12
jr
$ra