首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个阶乘程序给出的结果较低?

为什么这个阶乘程序给出的结果较低?
EN

Stack Overflow用户
提问于 2020-09-08 23:44:47
回答 1查看 43关注 0票数 0

这是一个简单的阶乘程序。

代码语言:javascript
复制
.global main

main:
        mov     r0,#7       // insert here the number to calculate the factorial, e.g. 7
        mov     r1,r0       // it will be useful after
        push    {ip,lr} // save the lr
        bl      factorial   // jump to factorial label
        pop     {ip,lr} // reset the lr so the program can return
        bx      lr

factorial:
        cmp     r1,#1       // if r1=1
        moveq   pc,lr       // then return
        sub     r1,r1,#1    // else r1 = r1-1
        mul     r0,r1,r0    // and multiply r0*r1
        b       factorial   // then do it again until moveq return to main

如果我执行它,我会收到错误的结果(非常低):

代码语言:javascript
复制
$ ./a.out 
$ echo $?
176

176而不是5040..一定是逻辑错误,你能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2020-09-08 23:52:40

5040 & 0xff = 176 --程序的退出代码只有8位

https://en.wikipedia.org/wiki/Exit_status#POSIX <-有几种方法可以检索整数,但是$?只会给出最低有效字节。

其中,waitid() 7调用检索完整的32位退出状态,但较早的

()和waitpid() 8调用仅检索退出状态的最低有效8位。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63797327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档