Search code examples
assemblystackmipsmips32

Why does the MIPS stack pointer need to be kept double word aligned?


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.

Stack Frame

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.


Solution

  • 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.