This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cpu_abi [2021/10/16 16:54] someone2639 [Functions With More Than Four Arguments] clarified stack frame |
cpu_abi [2021/10/16 17:31] someone2639 [Functions With Floating/Decimal Arguments] asm corrections |
||
---|---|---|---|
Line 89: | Line 89: | ||
The ''addiu'' instruction determines how much gets allocated to the stack, and the return address is placed at the top of this region. | The ''addiu'' instruction determines how much gets allocated to the stack, and the return address is placed at the top of this region. | ||
+ | ==== Functions With Floating/Decimal Arguments ==== | ||
+ | |||
+ | When a function takes floating point arguments, the ABI assumes you're using float registers more often and gives each of them their own purpose so you dont have to use general purpose registers too much. | ||
+ | |||
+ | ^ C Function ^ Equivalent ASM ^ | ||
+ | | <code c> | ||
+ | float three_input_adder(float a, float b, float c) { | ||
+ | return a + b + c; | ||
+ | } | ||
+ | void myFunc(void) { | ||
+ | float x = three_input_adder(1.0f, 3.0f, 4.0f); | ||
+ | } | ||
+ | </code> | <code> | ||
+ | glabel three_input_adder | ||
+ | lwc1 f4, 0x10(sp) | ||
+ | add.s f0, f12, f14 | ||
+ | add.s f0, f0, f4 ; return value gets stored in f0 | ||
+ | jr ra | ||
+ | nop | ||
+ | glabel myFunc | ||
+ | addiu sp, -0x18 | ||
+ | sw ra, 0x14(sp) | ||
+ | | ||
+ | li.s f12, 1.0 ; f12 holds the first argument | ||
+ | li.s f14, 3.0 ; f14 holds the second argument | ||
+ | li.s f4, 4.0 | ||
+ | swc1 f4, 0x10(sp) ; subsequent float arguments go on the stack | ||
+ | jal three_input_adder | ||
+ | nop | ||
+ | | ||
+ | mfc1 t0, f0 ; f0 has the result | ||
+ | | ||
+ | lw ra, 0x14(sp) | ||
+ | jr ra | ||
+ | addiu sp, 0x18 | ||
+ | </code> | | ||