Download presentation
Presentation is loading. Please wait.
1
Understanding Program Address Space
2
Overview Address space of a program Understanding stack layout
3
Address Space An address space is the set of (virtual) memory regions accessible to a program Text region – the program code (usually read only) Data, heap regions – static, dynamic variables Stack regions – used for function and system calls Let’s see where variables are located in program.c Address space stack text data SP PC run intro/program.c, and use intro/gdbinit-program to see the values of variables
4
Review of Program Data int var; int * p = &var;
Type of variable determines size and alignment of memory A program variable is a symbolic name for some data stored in memory int var; int * p = &var; Address of variable is the index of memory "array" where data is stored, e.g., if data is stored at memory[i], then address is i, value of variable is the contents of memory[i] Pointer is a variable whose value is the address of a memory location, e.g., suppose value of pointer variable is i, dereferencing pointer, i.e., *p, gives memory[i]
5
Review of Program Execution
argc, argv parameters of main() int a = 5; int main(int argc) { int b =10, c =0; c = f(b+c); } int f(int x) int y = x; return y; ret val return value of main() ret addr address where main() will return activation frame of main() prev fp fp (frame ptr) other regs b pc c sp (stack ptr) The exact locations of parameters, locals, return address, etc., in the activation frame depends on hardware, compiler, compiler options, etc. In example above, the order is parameters, return val & addr, frame pointer, locals (prfl or powerful) fp is used to access the stack variables use debugger to show intro/program.c a = 5; data (globals, heap) main(), f() text (code)
6
Function Call and Return
argc, argv int a = 5; int main(int argc) { int b =10, c =0; c = f(b+c); } int f(int x) int y = x; return y; ret val ret addr activation frame of main() prev fp other regs b c x = b+c parameter of f() ret value return value of f() activation frame of f() ret addr address in main() to return to prev fp fp (frame ptr) on function call: push fp // in C: sp--; *sp = fp; // the fp value in main() is saved in prev fp in f() in the figure above fp = sp // this helps track the sp value at the start of the function the result is that the set of prev fp in all the activation frames form a link list of previous stack pointer values, so that on function return we can figure out the location of the activation frame of the caller function (see below) on function return, the reverse happens: sp = fp pop fp // fp is assigned to prev fp (fp = *sp; sp++) // now the stack pointer points to the ret address, and the ret instruction jumps back to the caller using the ret addr // the caller then pulls out the return value from the stack pointer, and then pops the arguments it had pushed on the stack for the callee. // at this point, the stack pointer points to the location before the callee was invoked. other regs y sp (stack ptr) pc a = 5; data (globals, heap) main(), f() text (code)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.