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

前端 未结 1 1265
轻奢々
轻奢々 2021-01-27 04:35

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 tr

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 04:54

    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.)

    0 讨论(0)
提交回复
热议问题