Hack64 Wiki
Other Titles
Hack64 Wiki
Other Titles
This is an old revision of the document!
The Nintendo 64 uses a MIPS architecture NEC VR4300i, and applications created for the system are all compiled with C or C++. For the assembly developer looking to hack games that use C, knowing the calling convention used is one of the most important things you can know.
The MIPS o32
ABI determines 3 things: The register names and uses, the allocation of the stack, and how to effectively call functions using those.
Four registers are used for passing arguments to a function: a0
, a1
, a2
, and a3
. These arguments can hold any value up to 32 bits each, which includes pointer addresses.
C Function | Equivalent ASM |
---|---|
myFunc(0, 1, 0x12345678, &myPointer); | li a0, 0 li a1, 1 li a2, 0x12345678 la a3, myPointer ; Loads an address value jal myFunc ; make sure 'li' and 'la' instructions ; don't fall into a delay slot, ; otherwise only half of the value ; will load. nop |
If a function returns a value, it will be stored in v0
.
C Function | Equivalent ASM |
---|---|
myFunc(myReturningFunc()); | jal myReturningFunc nop move a0, v0 ; move v0 to a0 jal myFunc nop |
The ABI specifies four argument registers, so the natural thing to wonder is where a to put a fifth argument, sixth argument, and so on. The answer is to put them on the Stack. The general layout of the stack is as shown:
Stack Offset | Purpose |
---|---|
0x00 | (RESERVED) Stores a0 |
0x04 | (RESERVED) Stores a1 |
0x08 | (RESERVED) Stores a2 |
0x0C | (RESERVED) Stores a3 |
0x10 | Stores the return address for non-leaf functions |
0x14 | Fifth 32-bit argument |
0x18 | Sixth 32-bit argument |
0x1C | Seventh 32 bit argument |
…. | Every subsequent 32-bit argument will be on a multiple of 4 on the stack |
NOTE: on most N64 games, too much stack is allocated, so the layout looks more like this:
Stack Offset | Purpose |
---|---|
0x00 | (RESERVED) Stores a0 |
0x04 | (RESERVED) Stores a1 |
0x08 | (RESERVED) Stores a2 |
0x0C | (RESERVED) Stores a3 |
0x10 | Unused |
0x14 | Stores the return address for non-leaf functions |
0x18 | Fifth 32-bit argument |
0x1C | Sixth 32-bit argument |
…. | …. |