/* -------------------------------------------------------------- Program file used to demo (1) Pass by value (a.k.a.: pass by copy) (2) Pass by reference -------------------------------------------------------------- */ .global main, Stop, CodeEnd, DataStart, DataEnd .global f, k /* -------------------------------------------------- Begin of the program instructions -------------------------------------------------- */ .text main: /* ========================================= Pass k to f( ) by value or by ref ========================================= */ // Pass k by value movw r0, #:lower16:k movt r0, #:upper16:k // r0 = addr of var k push {r0} bl f add sp, sp, #4 Stop: nop /* ================================================ Function f that gets a parameter 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 /* ======================================= F() executes: a = a + 1 ======================================= */ ldr r0, [fp,#8] // r0 = addr of the param ldr r1, [r0] // r1 = value of the param add r1, r1, #1 str r1, [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: k: .4byte 7 DataEnd: .end