MIPS
assembly code: (copied from textbook) |
(
n corresponds to the
argument register $a0 ) |
|
|
|
|
fact: |
addi |
$sp,$sp,-8 |
# adjust stack for 2 items |
|
sw |
$ra,4($sp) |
# save the return address |
|
sw |
$a0,0($sp) |
# save the argument n |
|
slti |
$t0,$a0,1 |
# test for n < 1 |
|
beq |
$t0,$zero,L1 |
# if n >= 1, go to L1. |
|
|
|
|
|
addi |
$v0,$zero,1 |
# return 1 |
|
addi |
$sp,$sp,8 |
# delete 0 and address |
|
jr |
$ra |
# return to after jal of L1 |
|
|
|
|
L1: |
addi |
$a0,$a0,-1 |
# n >= 1: argument gets (n - 1) |
|
jal |
fact |
# call fact with (n - 1) |
|
|
|
|
|
lw |
$a0,0($sp) |
# return from jal:restore argument n |
|
lw |
$ra,4($sp) |
# restore the return
address |
|
addi |
$sp,$sp,8 |
# adjust stack pointer to pop 2 items |
|
mul |
$v0,$a0,$v0 |
# return n * fact(n-1) |
|
jr |
$ra |
# return to the caller |
|
|
|
|
trace: n = 3 |
|
|
|
|
fact: |
addi |
$sp,$sp,-8 |
# adjust stack for
2 items |
|
sw |
$ra,4($sp) |
# save the return address in stack |
|
sw |
$a0,0($sp) |
# save the argument n = 3 in stack |
|
slti |
$t0,$a0,1 |
# test if n < 1 |
|
beq |
$t0,$zero,L1 |
# if n >= 1, go to L1. Now n = 3, so go to L1 |
|
|
|
|
L1: |
addi |
$a0,$a0,-1 |
# n = 3 - 1 = 2 |
|
jal |
fact |
# call fact with n = 2 |
|
|
|
|
fact: |
addi |
$sp,$sp,-8 |
# adjust stack for 2 items |
|
sw |
$ra,4($sp) |
# save the return address in stack |
|
sw |
$a0,0($sp) |
# save the argument n = 2 in stack |
|
slti |
$t0,$a0,1 |
# test if n < 1 |
|
beq |
$t0,$zero,L1 |
# if n >= 1, go to L1. Now n = 2, so go to L1 |
|
|
|
|
L1: |
addi |
$a0,$a0,-1 |
# n = 2 - 1 = 1 |
|
jal |
fact |
# call fact with n = 1 |
|
|
|
|
fact: |
addi |
$sp,$sp,-8 |
# adjust stack for 2 items |
|
sw |
$ra,4($sp) |
# save the return address in stack |
|
sw |
$a0,0($sp) |
# save the argument n = 1 in stack |
|
slti |
$t0,$a0,1 |
# test if n < 1 |
|
beq |
$t0,$zero,L1 |
# if n >= 1, go to L1. Now n = 1, so go to L1 |
|
|
|
|
L1: |
addi |
$a0,$a0,-1 |
# n = 1 - 1 = 0 |
|
jal |
fact |
# call fact with n = 0 |
|
|
|
|
fact: |
addi |
$sp,$sp,-8 |
# adjust stack for 2 items |
|
sw |
$ra,4($sp) |
# save the return address in stack |
|
sw |
$a0,0($sp) |
# save the argument n = 0 in stack |
|
slti |
$t0,$a0,1 |
# test if n < 1 |
|
beq |
$t0,$zero,L1 |
# if n >= 1, goto L1. n = 0,so goto next line |
|
|
|
|
|
addi |
$v0,$zero,1 |
# return 1 |
|
addi |
$sp,$sp,8 |
# pop 2 items(0 and return address) of stack |
|
jr |
$ra |
# return to the instruction after jal |
|
|
|
|
|
lw |
$a0,0($sp) |
# returned from jal. restore argument 1 |
|
lw |
$ra,4($sp) |
# restore the return address |
|
addi |
$sp,$sp,8 |
# pop 2 items(1 and return address) of stack |
|
mul |
$v0,$a0,$v0 |
# return n*fact(n-1) = 1*fact(0) = 1 * 1 = 1 |
|
jr |
$ra |
# return to the instruction after jal |
|
|
|
|
|
lw |
$a0,0($sp) |
# returned from jal. restore argument 2 |
|
lw |
$ra,4($sp) |
# restore the return address |
|
addi |
$sp,$sp,8 |
# pop 2 items(2 and return address) of stack |
|
mul |
$v0,$a0,$v0 |
# return n*fact(n-1) = 2*fact(1) = 2 * 1 = 2 |
|
jr |
$ra |
# return to the instruction after jal |
|
|
|
|
|
lw |
$a0,0($sp) |
# returned from jal. restore argument 3 |
|
lw |
$ra,4($sp) |
# restore the return address |
|
addi |
$sp,$sp,8 |
# pop 2 items(3 and return address) of stack |
|
mul |
$v0,$a0,$v0 |
# return n*fact(n-1) = 3*fact(2) = 3 * 2 = 6 |
|
jr |
$ra |
# return to the caller |
No comments:
Post a Comment