Why does calling 'pop' in this piece of assembly code cause a segmentation fault?

一个人想着一个人 提交于 2019-12-20 05:01:23

问题


I'm playing around with x86-64 assembly on Mac OS (using NASM 2.09 and 2.13, to catch bugs caused by NASM issues). I'm trying to implement function calls at the moment, and tried using the push and pop instructions, but the pop always seems to cause a segfault:

line 10: 41072 Segmentation fault: 11 ./result

I've tried adjusting rsp, rbp etc manually, but the pop seems to be the issue. Any help would be appreciated!

section .data

default rel
global start
section .text

start:
    mov r12, 4
    push r12
    call label_0_print_digit
    (some stuff to exit program)

label_0_print_digit:
    pop r12
    (some stuff to print the digit - the issue persists even without this)
    ret 

回答1:


In the code shown in the question, the call instruction puts the return address on the stack and the pop instruction removes the return address from the stack (putting it into r12).

The ret instruction then pops 4 from the stack and jumps there. That isn't a valid code address, causing the fault. ret is basically just pop into RIP.


To access parameters to a function that are on the stack, use [rsp + 8], [rsp + 16], etc., instead of pop.

The standard calling conventions for x86-64 pass integer args in registers instead of the stack, where the callee can use them directly. And avoids the caller having to clean the stack after the function returns. (There are 2: Linux/MacOS/etc. vs. Windows, using different registers.)



来源:https://stackoverflow.com/questions/58151101/why-does-calling-pop-in-this-piece-of-assembly-code-cause-a-segmentation-fault

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