= Solution
Keep both the limit and current argument in <callee-saved registers>, so a call to `foo` preserves them. Under the same no-delay-slot convention, use
``
li $s0, n
li $s1, 1
slti $t0, $s0, 1
bne $t0, $0, testEnd
testLoop:
move $a0, $s1
jal foo
break 1
beq $s1, $s0, testEnd
addi $s1, $s1, 1
b testLoop
testEnd:
break 0
b testEnd
``
At every \b[`break 1`], `$s1` is the tested value and `$v0` is its result. Resuming after the breakpoint continues to the next value; after $n$ the program reaches its separate final stop. The initial guard handles $n<1$ with no calls. Testing equality before incrementing also avoids overflowing the loop variable after the final call when $n$ is the largest positive machine integer. As with the supplied procedures, evaluating all calls assumes enough <stack> space and representable arithmetic results.
Back to article page