segmentation fault at every assembly code

社会主义新天地 提交于 2021-02-17 07:10:03

问题


I'am trying to learn assemly on raspberry pi.But I couldn't get started, every code i write gets "Segmentation Fault".

.text
.global _start
_start:
   MOV R0, #2
   SWI 0

This code gets segmentation fault. Even if I delete the MOV line it gets segmentation fault.


回答1:


Try:

bx   lr                   @ Exit if use gcc as linker

or

mov  r7, #1               @ Exit if use ld as linker
svc  #0                   @ Exit if use ld as linker

Some version use swi, I have success with svc using ld as the linker. If you use gcc as the linker, the lr register has the return to OS. Be sure to save the lr register at the beginning of your code and restore it before the bx lr. See Using C functions with ARM assembly for more details.




回答2:


In the EABI that the Raspberry Pi uses, SWI 0 is just the system call entry. You still need to specify which function you want using r7. So, something like this should work:

.text
.global _start
_start:
   MOV R0, #2
   MOV R7, #1 ; sys_exit
   SWI 0


来源:https://stackoverflow.com/questions/45085591/segmentation-fault-at-every-assembly-code

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