CPSC 2310 - DAY 15 OCTOBER 7, 2016 ================================================================================ stack - automatically allocated variables (local variables subdivided into stack frames) heap - dynamically allocated variables initialized data(data) -global and static variables that are initialized by the programmer. includes rodata - read only data uninitialized data (bss, acronym for Block Started by Symbol) - global and static variables that are initialized to zero or do not have explicit initialization in source code text - program code/ instructions. RUN TIME ENVIRONMENT FOR A PROGRAM ---------------------------------- LOW ADDRESSES | | | | | TEXT | <- PROGRAM +------------+ | | | | <- VARIABLES DECLARED ONCE PER PROGRAM | DATA | +------------+ | | | | <- DYNAMICALLY ALLOCATED MEMORY (EX. MALLOC) | HEAP | +------------+ | | | | v | | ... | | ^ | | | | STACK ->+------------+ POINTER | | | | <- SPACE SAVED FOR PROCEDURE INFORMATION | STACK | +------------+ HIGH ADDRESSES Memory Stack - typical form of load for accessing a local variable Stack pointer points to the top of the stack. Stack matches the last in first out (LIFO) behavior of nested procedure calls Stack is located in high addresses in memory and grows toward small addresses, so creating space for local variables decrements the stack pointer Subroutines must make room for local variables. ARM SUBROUTINES --------------- reasons for subroutines: .repeat same code, or similar code with slightly different parameters .hide design decisions or design complexity .partition off code likely to change .provide for seperate compilation .provide for reuse Open vs. Closed Subroutines open subroutine - macro .resolved before assembly by textual substitution closed subroutine .branch execute return at run time .one copy of body which accesses its formal parameters. Three main concepts involved: .Transferring control from calling program to a subroutine and back .Passing parameter values to a subroutine and results back from the subroutine .Writing subroutine code that is independent of the calling program In addition, sometimes a subroutine must allocate space for local variables. Transferring control from calling program to a subroutine and back. .Necessary to save the return address, then branch to the subroutine .This can be accomplished in ARM using the branch and link instruction bl subr_name .To transfer control back to the calling program, you can use the branch and exchange instruction bx Alternatively, you can pop the lr register into the pc. lr - link register pc - holds the next instruction to execute ANYTIME A FUNCTION CALLS ANOTHER FUNCTION, YOU MUST SAVE THE LR