GLOBAL main
extern puts
SECTION .text
main:
push rdi;save registers that puts uses 保存参数个数
push rsi; 保存参数字符串指针数组,
sub rsp, 8; must align stack before call 进入函数的时候,call 保留了下一条指令到栈里面
mov rdi, [rsi]; the augument string todisplay 字符串指针数组首地址 存入rdi
call puts; print it 调用puts, 输出
;
add rsp, 8;restore %rsp to pre-aligned value 恢复栈
pop rsi; //restore registersputs used pop 参数个数
pop rdi; pop 参数指针
add rsi, 8; point next argument 64位的,一个地址8位,往下偏移一个地址
dec rdi;count down 减小rdi, 参数个数的值,
jnz main ;如果还不是0,就会跳到main的地方继续执行
ret
对于栈对齐,我们做一个验证看看。
#include <stdio.h>
void Test()
{
63a: 55 push %rbp
63b: 48 89 e5 mov %rsp,%rbp
printf("hello world\n");
63e: 48 8d 3d af 00 00 00 lea 0xaf(%rip),%rdi # 6f4 <_IO_stdin_used+0x4>
645: e8 c6 fe ff ff callq 510 <puts@plt>
}
64a: 90 nop
64b: 5d pop %rbp
64c: c3 retq
000000000000064d <main>:
int main()
{
64d: 55 push %rbp
64e: 48 89 e5 mov %rsp,%rbp
Test();
651: b8 00 00 00 00 mov $0x0,%eax
656: e8 df ff ff ff callq 63a <Test>
return 0;
65b: b8 00 00 00 00 mov $0x0,%eax
}
这一段汇编里面,我们并没有看到类似stack align的动作,是因为call调用的时候,
先是RIP寄存器保存到栈里面了。然后push %rbp ,栈底寄存器保存进栈,刚好对齐了。