1 / 13

Run-time Environment for a Program

Run-time Environment for a Program. different logical parts of a program during execution stack - automatically allocated variables (local variables, subdivided into stack frames - one per procedure invocation) heap - dynamically allocated variables data rodata – read only data

hue
Download Presentation

Run-time Environment for a Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Run-time Environment for a Program • different logical parts of a program during execution • stack - automatically allocated variables (local variables, subdivided into stack frames - one per procedure invocation) • heap - dynamically allocated variables • data • rodata – read only data • bss – uninitialized data • text - program code / instructions

  2. Run-time Environment for a Program int g = 1; @ g in data int main() { inti; @ ialloc. on stack ... } low addresses Program Variables declared once per program program’s layout in memory Dynamically allocated variable, e.g., malloc(); new; C pointers Space for saved procedure information Stack pointer high addresses (note: some memory diagrams may be reversed, with high memory at top)

  3. Memory Stack • stack matches the last in first out (LIFO) behavior of nested procedure calls (including recursion) • storage for automatic variables, that is, for local variables that are allocated at the beginning of a subroutine call and that are discarded at the end of the call • The collection of information related to a specific instance of calling a subroutine is termed a "stack frame" • -- a generic stack frame consists of parameters, • return address, registers to save, and local variables • Stack pointer (sp) • points to top of stack

  4. sp– stack pointer Run-time Environment for a Program lower addresses Current stack frame – locals addressed with addresses sp+k older stack frames – represent call chain from main() base of stack higher addresses • 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

  5. Memory Stack • Recall that ARM has the following load and store instructions: • ldr – load word • ldrh – load halfword • ldrb – load byte • ldm – load multiple words • str – store word • strh– store halfword • strb– store byte • stm – store multiple words

  6. Memory Stack Loads and stores typical form of load for accessing a local variable (i.e. a local variable): ldrrn, [sp, #4] typical form of store for accessing a local variable: strrs, [sp, #4]

  7. Memory Stack Loads and stores (example) intmain(){ int a = 0; a = a + 1; } add sp, sp, #-4 @ space for local variable add r5, sp, #0 @ a's address is sp mov r4, #0 @ a = 0; next three instructions str r4, [r5] @ perform a = a + 1; ldr r4, [r5] @ 1. read a's value (from the stack) add r4, r4, #1 @ 2. add one str r4, [r5] @ 3. store new value back into a @ (into the stack) mov r0, #0 add r5, r5, #4 @ restore the stack pointer mov sp, r5 pop {r5} bx lr

  8. Memory Stack Loads and stores ldm – load multiple words syntax: ldm{<cond>}<address-mode> <rn>{!}, <reg-list>{^} ldmia – load, increment after ldmia r9, {r0-r3} @ register 9 holds the @ base address. @ “ia” says increment @ the address after each @ value has been loaded @ from memory

  9. Memory Stack • Loads and stores • ldmia r9, {r0-r3} • has the same effect as four separate ldr instructions, or • ldr r0, [r9] • ldrr1, [r9, #4] • ldrr2, [r9, #8] • ldrr3, [r9, #12] • Note: at the end of the ldmiainstruction, register r9 has not been changed. If you wanted to change r9, you could simply use • ldmia r9!, {r0-r3, r12}

  10. Memory Stack • Loads and stores • If you want to load data into registers r0 through r3 and r12, you could simply use • ldmia r9, {r0-r3, r12} • If you want to load data into registers, r0 – r3, r5, r14, you can use • ldmia r9, {r5, r3, r0-r2, r14} • ldmib, ldmda, ldmdb work similar to ldmia • Stores work in an analogous manner to load instructions

  11. Memory Stack Pointers a pointer is the address of an object in memory a pointer is 64bits in length (on our current machines) a pointer can be in a register or in a memory word example int main(){ int a; int *pa = &a; *pa = 5; return 0; }

  12. Memory Stack Pointers .global main .type main, %function main: push {r5} add sp, sp, #-12 add r5, sp, #0 @ &a in r5 mov r4, r5 @ &a in r4 str r4, [r5, #4] @ &a in pa ([sp+4]) mov r3, #5 @ 5 in r3 str r3, [r4, #0] @ 5 in a ([sp]) mov r4, #0 mov r0, r4 add r5, r5, #8 movsp, r5 pop {r5} bxlr a: sp sp+4 pa:

  13. Memory Stack Extended example with printfstmts int main(){ int a = 0; int *pa = &a; printf("address of a = 0x%08x and contents of a = 0x%08x\n", &a,a); printf("address of pa = 0x%08x and contents of pa = 0x%08x\n", &pa,pa); *pa = 5; printf("address of a = 0x%08x and contents of a = 0x%08x\n", &a,a); printf("address of pa = 0x%08x and contents of pa = 0x%08x\n", &pa,pa); return 0; } [23:18:44] rlowe@dragon7:~/cs231-su13/subs [130] ./a.out address of a = 0xf6fffac8 and contents of a = 0x00000000 address of pa = 0xf6fffacc and contents of pa = 0xf6fffac8 address of a = 0xf6fffac8 and contents of a = 0x00000005 address of pa = 0xf6fffacc and contents of pa = 0xf6fffac8

More Related