x86 的调用堆栈保存单个字节
我目前正在通过“Linux 中的汇编语言指南”学习 x86 汇编,第 241 页上写着只有 16 位字或 32 位字保存到堆栈中,但这是真的吗? 我的意思是在 C 中,字符数组由单个字节组成,这些字节被保存到堆栈中,因为 C 由使用调用堆栈的函数组成,对吗?那么我错了什么?
I'm currently learning x86 assembly with "Guide to assembly language in Linux" and on page 241 there is written that only 16 bit words or 32 bit words are saved onto the stack, but is this true?
I mean in C a char array consists of single bytes and those are saved onto the stack as C consists of functions which use the call stack, right? So what am i getting wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使字节在推送之前也会用零填充并转换为 16 位或 32 位字。
将堆栈视为一堆特定尺寸(16 或 32)的盘子。有没有办法可以推一半尺寸的盘子..不?即使你想推一半尺寸,你也要把它垫成全尺寸的板,然后再推。
Even bytes are padded with zeros and converted to 16 bit or 32 bit words before being pushed.
Consider the stack as pile of plates of particular size (16 or 32). Is there a way you can push half the size plate .. No ? Even if you want to push the half the size, you would pad it to make the full size plate and then push it.
push
指令确实如此,但这并不是使用堆栈的唯一方法。 x86 还有 esp 寄存器来存储指向当前堆栈位置的指针。函数参数位于堆栈上,如果您检查一些反汇编,您将看到编译器如何将它们获取到那里。在 x86 的通常调用约定中,
char
参数每个占用 4 个字节。数组不能按值传递,因此如果可以的话,不会出现如何保存 char 数组的问题。自动变量也占用堆栈,但数组元素不会使用“push”单独保存到堆栈中。通常,该函数会在开始时为其所有自动变量腾出空间 - 查找涉及“esp”的“sub”指令。然后数组的开头位于距 esp 的已知偏移处,就像任何自动变量一样,编译器将使用此偏移量来生成对数组的访问。元素之间不需要填充,尽管在数组末尾之后可能会有一些填充,以保持堆栈指针正确对齐。
It's true of
push
instructions, but that's not the only way to use the stack. x86 also has the esp register to store a pointer to the current stack position.Function arguments go on the stack, if you check some disassembly you'll see how the compiler gets them there. In the usual calling convention for x86,
char
arguments occupy 4 bytes each. Arrays can't be passed by value, so the issue doesn't arise how a char array would be saved if they could.Automatic variables also occupy the stack, but the array elements aren't individually saved onto the stack using "push". Generally the function will make space for all its automatic variables at the start - look for a "sub" instruction involving "esp". Then the start of the array is at a known offset from esp, just like any automatic variable is, and the compiler will use this offset to generate accesses to the array. There's no need for padding between the elements, although there may be some after the end of the array in order to keep the stack pointer correctly aligned.