最近,我一直在从《从头开始编程》一书中学习 x86 汇编,但我有一台 x86-64 计算机,所以事情在某一时刻开始出错(在本书的早期)。我谈到了处理函数的部分,特别是电源示例。在此示例中,他将参数压入堆栈,然后将它们复制到函数后面的寄存器中。他的代码如下:
pushl $3 # second argument
pushl $2 # first argument
call power # call function
...
power:
pushl %ebp # save old base pointer
movl %esp, %ebp # make stack pointer the base pointer
subl $4, %esp # get room for our local storage
movl 8(%ebp), %ebx # put first argument in %eax
movl 12(%ebp), %ecx # put second argument in %ecx
Run Code Online (Sandbox Code Playgroud)
当然,这是32位的,而我运行的是64位,所以我尝试更新寄存器和指令后缀,最终得到这样的结果(这次不需要注释):
pushq $3
pushq $2
call power
...
power: …Run Code Online (Sandbox Code Playgroud)