/* -------------------------------------------------------------- Show how the program stack "magically" handles multiple functions that store parameters and local variables on program stack -------------------------------------------------------------- */ .global main, Stop, CodeEnd, DataStart, DataEnd .global A, B /* -------------------------------------------------- Begin of the program instructions -------------------------------------------------- */ .text main: /* ========================================= Call A( ) with 2 parameters ========================================= */ mov r0, #2222 // Pass 2222 as parameter 2 on stack push {r0} mov r0, #1111 // Pass 1111 as parameter 1 on stack push {r0} bl A // Call function A add sp, sp, #8 // Clean up parameters on the stack // When A returns, you will see the return value in r0 = 9999 nop nop Stop: nop /* =============================== Leaf function =============================== */ A: // When run in EGTAPI - you will see {1111, 2222} on stack /* **************************************** Prelude: build stack frame **************************************** */ push {lr} // Save return address in LR push {fp} // Save Frame Pointer in FP mov fp, sp // Initialize my own FP sub sp, sp, #12 // I create 3 local variable in stack nop // These instructions in A( ) can use stack nop // to access parameters and local variables /* ========================================= Call B( ) with 3 parameters ========================================= */ mov r0, #5555 // Pass 5555 as parameter 3 on stack push {r0} mov r0, #4444 // Pass 4444 as parameter 2 on stack push {r0} mov r0, #3333 // Pass 3333 as parameter 1 on stack push {r0} bl B // Call function B add sp, sp, #12 // Clean up parameters on the stack nop // We are back in A( ) nop // We will now return to main( ) /* ==================================================== We can use a register to return the return value ==================================================== */ mov r0, #9999 // Pass return value in register r0 /* ************************************************ Postlude: break down stack frame ************************************************ */ mov sp, fp // De-allocate the local variables pop {fp} // Restore old FP pop {pc} // Return B: // When run in EGTAPI - you will see {3333,4444,5555} on stack /* **************************************** Prelude: build stack frame **************************************** */ push {lr} // Save return address in LR push {fp} // Save Frame Pointer in FP mov fp, sp // Initialize my own FP sub sp, sp, #8 // I create 2 local variable in stack nop // These instructions in B( ) can use stack nop // to access parameters and local variables /* ==================================================== We can use a register to return the return value ==================================================== */ mov r0, #8888 // Pass return value in register r0 /* ************************************************ Postlude: break down stack frame ************************************************ */ mov sp, fp // De-allocate the local variables pop {fp} // Restore old FP pop {pc} // Return CodeEnd: nop /* -------------------------------------------------- Begin of the permanent program variables -------------------------------------------------- */ .data DataStart: DataEnd: .end