Assembly x86-64 get function parameters from stack

前端 未结 1 408
陌清茗
陌清茗 2021-01-15 18:04

Lately I\'ve been learning x86 Assembly from the book Programming from the Ground Up, but I have an x86-64 computer, so things start to go wrong at one point (pretty early i

相关标签:
1条回答
  • 2021-01-15 18:28

    The standard 64 bit conventions don't use the stack like that, they pass at least the first few arguments (type permitting) in registers. Of course for your own code you can still use the stack. However, you should adjust your offsets so that they use the appropriate size, 8 bytes instead of 4.

    subq $4, %rsp

    You should really use multiples of 8 unless you know what you are doing.

    movq 8(%rbp), %rdi

    You expect this to be the first argument but it isn't. It is actually the return address on the stack, since each item is 8 bytes now. So at 0(%rbp) you have the pushed rbp, and 8(%rbp) is the return address. Thus 16(%rbp) is the first argument and 24(%rbp) is the second.

    Note that in most environments you can still write 32 bit code, so if you want to keep using that book you might want to do that instead of trying to adjust for 64 bit.

    PS: You get a cookie for using a debugger :)

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