assemblymipsstack-memorystack-pointer

Why does the Stack Pointer in MIPS Typically Start at 0x7FFFFFFC but not 0x80000000?


According to Patterson & Hennessy's Computer Organization and Design (MIPS Edition),

  1. the stack pointer $sp is typically initialized to 0x7FFFFFFC.
    enter image description here

  2. the stack pointer $sp is always pointing at the most recently allocated address.
    enter image description here

So, if I want to push 1 word, say 0xCAFEBABE, in the stack pointer, I will

  1. subtract the stack pointer by 4 (because the stack pointer is pointing at the allocated address, I shall allocate more for new data).
  2. store 0xCAFEBABE to the address pointed by the stack pointer.

Now, the bytes at 0x7FFFFFF8, 0x7FFFFFF9, 0x7FFFFFFA, 0x7FFFFFFB will be 0xCA, 0xFE, 0xBA, 0xBE (order depending on the byte order). But the bytes at 0x7FFFFFFC, 0x7FFFFFFD, 0x7FFFFFFE, 0x7FFFFFFF will never be used!

I think this wastes 1 word in the memory. If the initial value of $sp were 0x80000000, that 1 word would not be wasted.


Solution

  • https://www.dyncall.org/docs/manual/manualse11.html:

    Multiple revisions of the MIPS Instruction set exist, namely MIPS I, MIPS II, MIPS III, MIPS IV, MIPS32 and MIPS64. Nowadays, MIPS32 and MIPS64 are the main ones used for 32-bit and 64-bit instruction sets, respectively. Given MIPS processors are often used for embedded devices, several add-on extensions exist for the MIPS family, for example:

    Unfortunately, there is actually no such thing as ”The MIPS Calling Convention”. Many possible conventions are used by many different environments such as O32[38], O64[39], N32[40], N64[40], EABI[41] and NUBI[42].

    https://courses.cs.washington.edu/courses/cse410/09sp/examples/MIPSCallingConventionsSummary.pdf:

    The value of the stack pointer is required to be multiple of 8 at all times.