【发布时间】:2021-03-03 13:55:00
【问题描述】:
我已经看到堆栈指针/esp 在调用printf 之前由4 递减并在调用printf 之后由12 重新调整的示例:
section .text
global main
extern printf
main:
sub esp, 4
push msg
push format_str
call printf
add esp, 12
ret
section .data:
msg db "print me!", 0
format_str db "%s", 0
我已经看到堆栈指针/esp 在调用printf 之前由8 递减并在调用printf 之后由16 重新调整的示例:
section .text
global main
extern printf
main:
sub esp, 8
push msg
push format_str
call printf
add esp, 16
ret
section .data:
msg db "print me!", 0
format_str db "%s", 0
从我读到的内容来看,esp 应该减少8,然后在从 libc 调用任何函数之前重新调整/增加16。
这些示例中的差异让我感到困惑,哪个堆栈对齐示例是正确的,为什么?是否可以解释这个递增/递减过程以减少混淆?
【问题讨论】:
-
第二个例子实际上是错误的,堆栈没有在 16 字节边界上对齐。并不是网上所有的例子都是正确的。
标签: assembly x86 stack memory-alignment abi