|
|
If the subroutine is not a leaf function, then its values in all or any register can be lost when it invokes another subroutine !!
But if the subroutine does not call any subroutine, it can safely use all register to store the parameters and local variable.
|
|
Example: a method that returns an int typed value
public int sum(int x, int y) { ... return (return_value); } |
|
Therefore:
|
|
/* ---------------------------------------------- sumSquares(a,b): returns (a*a + b*b) ---------------------------------------------- */ int sumSquares( int a, int b ) { return (a*a + b*b); } void main( ) { int x, y, z; z = sumSquares(x, y); } |
|
/* ------------------------------------------------- Pass parameter x by copying its value in reg r0 ------------------------------------------------- */ movw r0, #:lower16:x movt r0, #:upper16:x ldr r0, [r0] /* ------------------------------------------------- Pass parameter y by copying its value in reg r1 ------------------------------------------------- */ movw r1, #:lower16:y movt r1, #:upper16:y ldr r1, [r1] /* ---------------------------------------------------------------- call z = sumSquares(x,y): agreed inputs: r0 = x, r1 = y |
/* ---------------------------------------------------------------- Function sumSquares(a,b): agreed inputs: r0 = a, r1 = b agreed return: r0 = return value ---------------------------------------------------------------- */ sumSquares: // When sumSquares begin, we will have: r0 = a, r1 = b |
How to run the program:
|