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开发者_StackOverflow中文版 and those are saved onto the stack as C consists of functions which use the call stack, right? So what am i getting wrong?
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.
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.
精彩评论