微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么这个析因程序的结果较低?

如何解决为什么这个析因程序的结果较低?

这是简单的阶乘程序。

.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,r0    // and multiply r0*r1
        b       factorial   // then do it again until moveq return to main

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

$ ./a.out 
$ echo $?
176

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

解决方法

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

https://en.wikipedia.org/wiki/Exit_status#POSIX $?只会给您最低有效字节。

其中,waitid()[7]调用检索完整的32位退出状态, 但是较旧的wait()和waitpid()[8]调用只检索最少的 退出状态的有效8位。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。