mips指令集的递归小程序

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版本)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值