/* -------------------------------------------------------------- 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 f, k /* -------------------------------------------------- Begin of the program instructions -------------------------------------------------- */ .text main: /* ========================================= Pass k to f( ) by value ========================================= */ movw r0, #:lower16:k movt r0, #:upper16:k // r0 = address of k ldr r0, [r0] // r0 = value of k push {r0} // Pass k by value (pass k's value) bl f // Call function f add sp, sp, #4 // Clean up parameters on the stack nop nop Stop: nop /* ================================================ Function f that gets a parameter by value Stack frame of f( ): FP--> +-----------+ | old FP | FP + 0 +-----------+ | ret addr | FP + 4 +-----------+ | param a | FP + 8 +-----------+ ================================================ */ f: /* **************************************** 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, #0 // I create 0 local variable in stack /* ======================================= a = a + 1 ======================================= */ ldr r0, [fp, #8] // r0 = a add r0, r0, #1 // r0 = a + 1 str r0, [fp, #8] // a = a + 1 /* ************************************************ 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: k: .4byte 7 DataEnd: .end