180 likes | 501 Views
Simple Subroutine Linkage. Function Calls. Function calls implemented as jumps. Generality. Function called from more than one place can't hardcode return jump. Infinite loop…. JAL. jal : Jump And Link Store "next" instruction to $ ra register
E N D
Function Calls • Function callsimplemented as jumps
Generality • Function called from more than one place can't hardcode return jump Infinite loop…
JAL • jal: Jump And Link • Store "next" instruction to $ra register • Next instruction is one after branch delay currentAddress+8
JR • jr : Jump Register • Jump to address stored in register
Calling Convention • Calling Convention • Agreement for howsubroutines work • How we pass info • What registers wecan use • … • Differs betweenplatforms,compilers, etc…
Simple Linkage • Simple Linkage Convention: • jal/jr • Registers usage:
Simple function • Read an integer: intgetInt(); readIntsSimple.mas
With parameter • Read an integer with parameter: intgetInt(char* prompt); readIntsParameter.mas
Multi-Paramer Function • Read an integer with parameter: intaddThree(int x, int y, int z); readIntsSumFunction.mas
Nested Function Calls • An attempt tocall one functionfrom another:
Nested Function Calls • What we got
Nested Workaround • One possible workaround… • Store our return address before calling other function
Nested Workaround • Limitations: • What if other function messes with registers? • Still can't handle recursive functions