Push/Pop segmentation fault at Assembly x86

后端 未结 3 1018
小鲜肉
小鲜肉 2020-12-22 05:00

I\'m using elf64 to compile my assembly x86 code: I\'ve this sub-routine:

printNumber:
    mov EAX, EDX ; EDX contain some value like \"35\"         


        
3条回答
  •  半阙折子戏
    2020-12-22 06:00

    The instruction

    push rdx;
    

    can, on itself, only cause a segmentation fault in rather rare cases: When you're running out of stack or when you've messed around with (E)SP. Since you can run to that code, I don't think you did the second, and the first is rather unrealistic, if this is all your application does. But Michael above pointed to the right direction: It's not the push instruction that's causing the segmentation fault but the missing pops before the ret. At the end of your function, the stack must contain exactly the same number of elements as at the beginning, or the ret instruction will read whatever is at the bottom of the stack and try to use it as return address -> bang.

    You cannot use the stack to return values this way. You (for instance) need the calling function to allocate memory for the return data and provide it's address as argument. Read up about calling conventions and passing arguments in assembly language.

提交回复
热议问题