#include using namespace std; /* ==================================================== Function with a parameter that is passed by value ==================================================== */ void f(int a) // Var a is passed by value < a = a + 1; > int main(int argc, char *argv[]) < int k = 7; f(k); >
The main( ) function will pass the value (= content) of the variable k to the function f( ) .
This is how it is done in ARM assembler code :
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 // Pass k by value (pass k's value) bl f // Call function f add sp, sp, #4 // Clean up parameters on the stack /* ----------------------------------------------------------- */ .data k: .4byte 7 // This is the variable k
/* ================================================ 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 // Save return address in LR push // 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 // Restore old FP pop // Return
#include using namespace std; /* ==================================================== Function with a parameter that is passed by value ==================================================== */ void f(int & a) // Var a is passed by reference < a = a + 1; > int main(int argc, char *argv[]) < int k = 7; f(k); >
The main( ) function will pass the address of the variable k to the function f( ) .
This is how it is done in ARM assembler code :
main: /* ========================================= Pass k to f( ) by value ========================================= */ movw r0, #:lower16:k movt r0, #:upper16:k // r0 = address of k push // Pass k by reference (pass k's address) bl f // Call function f add sp, sp, #4 // Clean up parameters on the stack /* ----------------------------------------------------------- */ .data k: .4byte 7 // This is the variable k
/* ================================================ 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 // Save return address in LR push // 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 = address of (original variable) a ldr r1, [r0] // r1 = value in variable a add r1, r1, #1 // r0 = a + 1 str r1, [r0] // orignal a = a + 1 . /* ************************************************ Postlude: break down stack frame ************************************************ */ mov sp, fp // De-allocate the local variables pop // Restore old FP pop // Return