RISC-V汇编实现矩阵阶乘

1) 源代码 

long long fact(long long n) {
	if (n < 1) return 1;
	else return (n*fact(n - 1));
}

2) 汇编代码 

fact:
    addi sp, sp, -16       // adjust stack for 2 items
    sd x1, 8(sp)           // save the return address
    sd x10, 0(sp)          // save the argument n
    addi x5, x10, -1       // x5 = n - 1
    bge x5, x0, L1         // if (n - 1) >= 0, go to L1
    addi x10, x0, 1        // return 1
    addi sp, sp, 16        // pop 2 items off stack
    jalr x0, 0(x1)         // return to caller

L1:
    addi x10, x10, -1      // n >= 1: argument gets (n - 1)
    jal x1, fact           // call fact with (n - 1)
    ld x6, 0(sp)           // restore argument n
    ld x1, 8(sp)           // restore the return address
    addi sp, sp, 16        // adjust stack pointer to pop 2 items
    mul x10, x10, x6       // return n * fact(n - 1)
    jalr x0, 0(x1)         // return to the caller

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值