这是一个简单的阶乘程序。
.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如果我执行它,我会收到错误的结果(非常低):
$ ./a.out
$ echo $?
176176而不是5040..一定是逻辑错误,你能帮我吗?
发布于 2020-09-08 23:52:40
5040 & 0xff = 176 --程序的退出代码只有8位
https://en.wikipedia.org/wiki/Exit_status#POSIX <-有几种方法可以检索整数,但是$?只会给出最低有效字节。
其中,waitid() 7调用检索完整的32位退出状态,但较早的
()和waitpid() 8调用仅检索退出状态的最低有效8位。
https://stackoverflow.com/questions/63797327
复制相似问题