c語言寫的源程序:
</pre><pre name="code" class="cpp">int fact(int n)
{
if(n<1)
return 1;
else return n*fact(n-1)
}
下面給出mips程序
main:
add $a0,$zero,2
jal fact
jr $ra
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
slti $t0,$a0,1 ;test for n<1
beq $t0,$zero ;if n>=1,go to L1
addi $v0,$zero,1 ;return 1
addi $sp,$sp,8 ;pop 2 items off stack
jr $ra ;return to caller1
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:return restore argument.h
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
注:
剛學這門彙編語言,在windows下用的工具是pcspim(mips在pc機上的模擬器),可惜它只能編譯,不能編輯.s文件,只能用一些文本編輯器
在windows下用 notepad+應該夠用的,如果感興趣的話可以看一下emacs(在linux下的編輯神器,當然也有windows版本)