How to return a number larger than 8 bits from main()?

自闭症网瘾萝莉.ら 提交于 2021-02-05 07:55:29

问题


So as far as I can tell, the exit code returned from r0 only uses the lowest 8 bits of this register. How wouuld I return a value higher than 8 bits?

Here is the ARMv7 code:

@ looping.s
@ calculates sum of integers from 1 to 100
.text
.balign 4
.global main
main:
    MOV r1, #0      @ r1 = 0 as sum
    MOV r2, #0      @ r2 = 0 as counter
loop:
    ADD r2, r2, #1  @ counter = counter + 1
    ADD r1, r1, r2  @ sum = sum + counter
    CMP r2, #100    @ counter - 100
    BLT loop        @ if counter < 100 go to start of loop
    MOV r0, r1      @ Store sum in r0
    BX lr           @ Return summation result to OS

回答1:


The exit status of a process is 8 bits in size. It is not possible to return a larger exit status by normal means. If you want to output a number larger than 255, you could for example print it to stdout (file descriptor 1) with a write system call.




回答2:


You shouldn't be using the main() function to perform calculations. In general, functions are not restricted to 8-bit return values, so give your actual function a different name and return a larger integer in R0. Invoke your function from inside main() and then do whatever you need to do with the return value, perhaps print it to the console.



来源:https://stackoverflow.com/questions/58457878/how-to-return-a-number-larger-than-8-bits-from-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!