Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

Machine Programming I: Basics
Machine-Level Programming I: Basics
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Assembly Language Alan L. Cox Some slides adapted from CMU slides.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Machine-Level Programming I: Introduction Sept. 10, 2002 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
1 Machine-Level Programming I: Basics Computer Systems Organization Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Introduction to x86 Assembly or “How does someone hack my laptop?”
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
1 Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2015 Instructor: John Barr * Modified slides.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
A job ad at a game programming company
X86 Assembly - Data.
Reading Condition Codes (Cont.)
Instruction Set Architecture
Credits and Disclaimers
C function call conventions and the stack
CSCE 212 Computer Architecture
IA32 Processors Evolutionary Design
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming 1 Introduction
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming III: Procedures Sept 18, 2001
X86 Assembly - Data.
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
“Way easier than when we were students”
Credits and Disclaimers
Presentation transcript:

Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are adapted from the ones prepared by R.E. Bryant, D.R. O’Hallaron of Carnegie-Mellon Univ.

Integer Registers (IA32) %eax %ecx %edx %ebx %esi %edi %esp %ebp %ax %cx %dx %bx %si %di %sp %bp %ah %ch %dh %bh %al %cl %dl %bl 16-bit virtual registers (backwards compatibility) general purpose accumulate counter data base source index destination index stack pointer base pointer Origin (mostly obsolete)

Moving Data: IA32 Moving Data  movx Source, Dest  x in { b, w, l }  movl Source, Dest: Move 4-byte “long word”  movw Source, Dest: Move 2-byte “word”  movb Source, Dest: Move 1-byte “byte” Lots of these in typical code %eax %ecx %edx %ebx %esi %edi %esp %ebp

Moving Data: IA32 Moving Data movl Source, Dest: Operand Types  Immediate: Constant integer data  Example: $0x400, $-533  Like C constant, but prefixed with ‘$’  Encoded with 1, 2, or 4 bytes  Register: One of 8 integer registers  Example: %eax, %edx  But %esp and %ebp reserved for special use  Others have special uses for particular instructions  Memory: 4 consecutive bytes of memory at address given by register  Simplest example: (%eax)  Various other “address modes” %eax %ecx %edx %ebx %esi %edi %esp %ebp

movl Operand Combinations Cannot do memory-memory transfer with a single instruction movl Imm Reg Mem Reg Mem Reg Mem Reg SourceDestC Analog movl $0x4,%eaxtemp = 0x4; movl $-147,(%eax)*p = -147; movl %eax,%edxtemp2 = temp1; movl %eax,(%edx)*p = temp; movl (%eax),%edxtemp = *p; Src,Dest

Simple Memory Addressing Modes Normal(R)Mem[Reg[R]]  Register R specifies memory address movl (%ecx),%eax DisplacementD(R)Mem[Reg[R]+D]  Register R specifies start of memory region  Constant displacement D specifies offset movl 8(%ebp),%edx

Using Simple Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body Set Up Finish

Using Simple Addressing Modes void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body Set Up Finish

Understanding Swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx Stack (in memory) RegisterValue %ecxyp %edxxp %eaxt1 %ebxt0 yp xp Rtn adr Old % ebp %ebp Offset Old % ebx -4

Understanding Swap movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp0x104

movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp0x104 0x120

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x120 0x104 movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 0x124

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 0x124 0x120 0x104 movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 456

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 0x104 movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 123

456 Understanding Swap 0x120 0x124 Rtn adr %ebp Offset -4 Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x x104 movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx

Understanding Swap 0x120 0x124 Rtn adr %ebp Offset Address 0x124 0x120 0x11c 0x118 0x114 0x110 0x10c 0x108 0x104 0x100 yp xp %eax %edx %ecx %ebx %esi %edi %esp %ebp 456 0x124 0x120 0x104 movl 12(%ebp),%ecx# ecx = yp movl 8(%ebp),%edx# edx = xp movl (%ecx),%eax# eax = *yp (t1) movl (%edx),%ebx# ebx = *xp (t0) movl %eax,(%edx)# *xp = eax movl %ebx,(%ecx)# *yp = ebx 123

Complete Memory Addressing Modes Most General Form D(Rb,Ri,S)Mem[Reg[Rb]+S*Reg[Ri]+ D]  D: Constant “displacement” 1, 2, or 4 bytes  Rb: Base register: Any of 8 integer registers  Ri:Index register: Any, except for %esp  Unlikely you’d use %ebp, either  S: Scale: 1, 2, 4, or 8 (why these numbers?) Special Cases (Rb,Ri)Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri)Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S)Mem[Reg[Rb]+S*Reg[Ri]]

IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Register %esp contains lowest stack address = address of “top” element Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom”

IA32 Stack: Push pushl Src  Fetch operand at Src  Decrement %esp by 4  Write operand at address given by %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer: %esp -4

IA32 Stack: Pop Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” popl Dest  Read operand at address %esp  Increment %esp by 4  Write operand to Dest +4

Procedure Control Flow Use stack to support procedure call and return Procedure call: call label  Push return address on stack  Jump to label Return address:  Address of instruction beyond call  Example from disassembly e:e8 3d call 8048b :50 pushl %eax  Return address = 0x Procedure return: ret  Pop address from stack  Jump to address

%esp %eip %esp %eip 0x804854e 0x108 0x10c 0x110 0x104 0x804854e 0x Procedure Call Example 0x108 0x10c 0x x108 call 8048b e:e8 3d call 8048b :50 pushl %eax 0x8048b90 0x104 %eip: program counter

%esp %eip 0x104 %esp %eip 0x x104 0x108 0x10c 0x110 0x Procedure Return Example 0x108 0x10c 0x ret :c3 ret 0x108 0x %eip: program counter

Stack-Based Languages Languages that support recursion  e.g., C, Pascal, Java  Code must be “Reentrant”  Multiple simultaneous instantiations of single procedure  Need some place to store state of each instantiation  Arguments  Local variables  Return pointer Stack discipline  State for given procedure needed for limited time  From when called to when return  Callee returns before caller does Stack allocated in Frames  state for single procedure instantiation

Call Chain Example yoo(…) { who(); } who(…) { amI(); amI(); } amI(…) { amI(); } yoo who amI Example Call Chain amI Procedure amI is recursive

Frame for proc Frame Pointer: %ebp Stack Frames Contents  Local variables  Return information  Temporary space Management  Space allocated when enter procedure  “Set-up” code  Deallocated when return  “Finish” code Stack Pointer: %esp Previous Frame Stack “Top”

Example yoo(…) { who(); } yoo who amI yoo %ebp %esp Stack

who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack who

amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI

amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI

amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI

amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI

amI(…) { amI(); } Example yoo who amI yoo %ebp %esp Stack who amI

who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack who

amI(…) { } Example yoo who amI yoo %ebp %esp Stack who amI

who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack who

Example yoo(…) { who(); } yoo who amI yoo %ebp %esp Stack

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom)  “Argument build:” Parameters for function about to call  Local variables If can’t keep in registers  Saved register context  Old frame pointer Caller Stack Frame  Return address  Pushed by call instruction  Arguments for this call Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame Frame pointer %ebp Stack pointer %esp

Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } call_swap: pushl $zip2# Global Var pushl $zip1# Global Var call swap &zip2 &zip1 Rtn adr %esp Resulting Stack Calling swap from call_swap

Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Body Set Up Finish

swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp

swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp

swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack

swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp

swap Setup #1 &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack Old %ebx movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp... Offset relative to %ebp

swap Finish #1 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack Old %ebx Observation: Saved and restored register %ebx

swap Finish #2 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp Old %ebx

swap Finish #2 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp Resulting Stack

swap Finish #2 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp %esp

swap Finish #3 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Resulting Stack yp xp Rtn adr %ebp %esp

swap Finish #4 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr %ebp %esp

swap Finish #4 yp xp Rtn adr Old %ebp %ebp %esp swap’s Stack Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp %ebp %esp Resulting Stack Observation  Saved & restored register %ebx  Didn’t do so for %eax, %ecx, or %edx

Disassembled swap a4 : 80483a4: 55 push %ebp 80483a5: 89 e5 mov %esp,%ebp 80483a7: 53 push %ebx 80483a8: 8b mov 0x8(%ebp),%edx 80483ab: 8b 4d 0c mov 0xc(%ebp),%ecx 80483ae: 8b 1a mov (%edx),%ebx 80483b0: 8b 01 mov (%ecx),%eax 80483b2: mov %eax,(%edx) 80483b4: mov %ebx,(%ecx) 80483b6: 5b pop %ebx 80483b7: c9 leave 80483b8: c3 ret : e8 96 ff ff ff call 80483a e: 8b 45 f8 mov 0xfffffff8(%ebp),%eax Calling Code

Register Saving Conventions When procedure yoo calls who :  yoo is the caller  who is the callee Can Register be used for temporary storage?  Contents of register %edx overwritten by who yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $91125, %edx ret

Register Saving Conventions When procedure yoo calls who :  yoo is the caller  who is the callee Can register be used for temporary storage? Conventions  “Caller Save”  Caller saves temporary in its frame before calling  “Callee Save”  Callee saves temporary in its frame before using

IA32/Linux Register Usage %eax, %edx, %ecx  Caller saves prior to call if values are used later %eax  also used to return integer value %ebx, %esi, %edi  Callee saves if wants to use them %esp, %ebp  special %eax %edx %ecx %ebx %esi %edi %esp %ebp Caller-Save Temporaries Callee-Save Temporaries Special

Linux Memory Layout Stack  Runtime stack (8MB limit) Heap  Dynamically allocated storage  When call malloc, calloc, new DLLs  Dynamically Linked Libraries  Library routines (e.g., printf, malloc )  Linked into object code when first executed Data  Statically allocated data  E.g., arrays & strings declared in code Text  Executable machine instructions  Read-only Upper 2 hex digits of address Red Hat v. 6.2 ~1920M B memory limit FF BF 7F 3F C Stack DLLs Text Data Heap 08

Linux Memory Allocation Linked BF 7F 3F Stack DLLs Text Data 08 Some Heap BF 7F 3F Stack DLLs Text Data Heap 08 More Heap BF 7F 3F Stack DLLs Text Data Heap 08 Initially BF 7F 3F Stack Text Data 08

Text & Stack Example (gdb) break main (gdb) run Breakpoint 1, 0x804856f in main () (gdb) print $esp $3 = (void *) 0xbffffc78 Main  Address 0x804856f should be read 0x f Stack  Address 0xbffffc78 Initially BF 7F 3F Stack Text Data 08

Dynamic Linking Example (gdb) print malloc $1 = { } 0x (gdb) run Program exited normally. (gdb) print malloc $2 = {void *(unsigned int)} 0x Initially  Code in text segment that invokes dynamic linker  Address 0x should be read 0x Final  Code in DLL region Linked BF 7F 3F Stack DLLs Text Data 08

Memory Allocation Example char big_array[1<<24]; /* 16 MB */ char huge_array[1<<28]; /* 256 MB */ int beyond; char *p1, *p2, *p3, *p4; int useless() { return 0; } int main() { p1 = malloc(1 <<28); /* 256 MB */ p2 = malloc(1 << 8); /* 256 B */ p3 = malloc(1 <<28); /* 256 MB */ p4 = malloc(1 << 8); /* 256 B */ /* Some print statements... */ }

Example Addresses $esp0xbffffc78 p3 0x500b5008 p1 0x400b4008 Final malloc0x p40x1904a640 p20x1904a538 beyond 0x1904a524 big_array 0x1804a520 huge_array 0x0804a510 main()0x f useless() 0x Initial malloc0x BF 7F 3F Stack DLLs Text Data Heap 08