I have read this in a couple of places, but without fully understanding why. I understand why all instructions have to be word aligned to 4 bytes in MIPS32.
But then I read that a stack frame needs to be a multiple of 8, because the stack pointer needs to be double word aligned. Why?
I understand everything in the attached image, except the reason for adding empty space to pad the stack to a multiple of 8 bytes.
EDIT: One source for where I read this was here, page 3, bullet 4. The other was Computer Organization & Design, 5th Edition, by Patterson and Hennessey, Appendix A.6, under the Procedure Call Example. However, it's pretty clear to me now I was mistaken to assume they were talking about MIPS32.
The MIPS stack In MIPS machines, part of main memory is reserved for a stack. — The stack grows downward in terms of memory addresses. — The address of the top element of the stack is stored (by convention) in the “stack pointer” register, $sp. MIPS does not provide “push” and “pop” instructions.
So to align something in memory means to rearrange data (usually through padding) so that the desired item's address will have enough zero bytes.
Memory AlignmentThe MIPS assembler allows users to control data alignment using the . align directive. If the programmer does not use the directive, then the data will be automatically aligned in memory at the proper boundaries. Since all instructions are the same size (word), they must be aligned in memory.
The MIPS architecture can only access data types in memory that are evenly aligned with their size.
See MIPS Run by Dominic Sweetman says on page 320:
At the point where a subroutine is called, sp must be eight-byte-aligned, matching the alignment of the largest basic types - a
long long
integer, or a floating-point double. The eight-byte alignment is not required by 32-bit MIPS integer hardware, but it is essential for compatibility...
Thus, if you never try to push a double
to the stack, you can very well live with 4-byte alignment on a 32-bit system. Whether your OS can, is another question, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With