machine programming – procedures and ia32 stack ceng334: introduction to operating systems...
TRANSCRIPT
![Page 1: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/1.jpg)
Machine Programming – Procedures and IA32 StackCENG334: 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.
![Page 2: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/2.jpg)
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)
gene
ral p
urpo
se
accumulate
counter
data
base
source index
destinationindex
stack pointer
basepointer
Origin(mostly obsolete)
![Page 3: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/3.jpg)
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
![Page 4: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/4.jpg)
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
![Page 5: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/5.jpg)
movl Operand Combinations
Cannot do memory-memory transfer with a single instruction
movl
Imm
Reg
Mem
RegMem
RegMem
Reg
Source Dest C Analog
movl $0x4,%eax temp = 0x4;
movl $-147,(%eax) *p = -147;
movl %eax,%edx temp2 = temp1;
movl %eax,(%edx) *p = temp;
movl (%eax),%edx temp = *p;
Src,Dest
![Page 6: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/6.jpg)
Simple Memory Addressing Modes Normal (R) Mem[Reg[R]]
Register R specifies memory address
movl (%ecx),%eax
Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset
movl 8(%ebp),%edx
![Page 7: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/7.jpg)
Using Simple Addressing Modes
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
swap:pushl %ebpmovl %esp,%ebppushl %ebx
movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Body
SetUp
Finish
![Page 8: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/8.jpg)
Using Simple Addressing Modes
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
swap:pushl %ebpmovl %esp,%ebppushl %ebx
movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Body
SetUp
Finish
![Page 9: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/9.jpg)
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)
Register Value%ecx yp%edx xp%eax t1%ebx t0
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
•••
Old %ebx-4
![Page 10: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/10.jpg)
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 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp 0x104
![Page 11: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/11.jpg)
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 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp 0x104
0x1200x120
![Page 12: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/12.jpg)
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x120
0x104movl 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
0x124
![Page 13: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/13.jpg)
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x124
0x120
0x104movl 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
456
![Page 14: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/14.jpg)
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
456
0x124
0x120
0x104movl 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
123
![Page 15: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/15.jpg)
456
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
456456
0x124
0x120
123
0x104movl 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
123
![Page 16: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/16.jpg)
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
456
0x124
0x120
0x104movl 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
123
123
![Page 17: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/17.jpg)
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]]
![Page 18: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/18.jpg)
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 GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
![Page 19: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/19.jpg)
IA32 Stack: Push
pushl Src Fetch operand at Src Decrement %esp by 4 Write operand at address given
by %esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
Stack Pointer: %esp-4
![Page 20: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/20.jpg)
IA32 Stack: Pop
Stack Pointer: %esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom” popl Dest
Read operand at address %esp Increment %esp by 4 Write operand to Dest
+4
![Page 21: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/21.jpg)
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 disassembly804854e: e8 3d 06 00 00 call 8048b90 <main>
8048553: 50 pushl %eax Return address = 0x8048553
Procedure return: ret Pop address from stack Jump to address
![Page 22: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/22.jpg)
%esp
%eip
%esp
%eip 0x804854e
0x108
0x108
0x10c
0x110
0x104
0x804854e
0x8048553
123
Procedure Call Example
0x108
0x10c
0x110
123
0x108
call 8048b90
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
0x8048b90
0x104
%eip: program counter
![Page 23: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/23.jpg)
%esp
%eip
0x104
%esp
%eip 0x80485910x8048591
0x1040x104
0x108
0x10c
0x110
0x8048553
123
Procedure Return Example
0x108
0x10c
0x110
123
ret
8048591: c3 ret
0x108
0x8048553
0x8048553
%eip: program counter
![Page 24: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/24.jpg)
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
![Page 25: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/25.jpg)
Call Chain Example
yoo(…){
••who();••}
who(…){
• • •amI();• • •amI();• • •}
amI(…){
••amI();••}
yoo
who
amI
amI
amI
ExampleCall Chain
amI
Procedure amI is recursive
![Page 26: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/26.jpg)
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
PreviousFrame
Stack “Top”
![Page 27: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/27.jpg)
Example
yoo(…){
••who();••}
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
![Page 28: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/28.jpg)
who(…){
• • •amI();• • •amI();• • •}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
![Page 29: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/29.jpg)
amI(…){
••amI();••}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
amI
![Page 30: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/30.jpg)
amI(…){
••amI();••}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
amI
amI
![Page 31: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/31.jpg)
amI(…){
••amI();••}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
amI
amI
amI
![Page 32: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/32.jpg)
amI(…){
••amI();••}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
amI
amI
![Page 33: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/33.jpg)
amI(…){
••amI();••}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
amI
![Page 34: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/34.jpg)
who(…){
• • •amI();• • •amI();• • •}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
![Page 35: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/35.jpg)
amI(…){
•••••}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
amI
![Page 36: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/36.jpg)
who(…){
• • •amI();• • •amI();• • •}
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
who
![Page 37: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/37.jpg)
Example
yoo(…){
••who();••}
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
![Page 38: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/38.jpg)
IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom)
“Argument build:”Parameters for function about to call
Local variablesIf 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
SavedRegisters
+Local
Variables
ArgumentBuild
Old %ebp
Arguments
CallerFrame
Frame pointer%ebp
Stack pointer%esp
![Page 39: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/39.jpg)
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 Varpushl $zip1 # Global Varcall swap• • •
&zip2
&zip1
Rtn adr %esp
ResultingStack•
••
Calling swap from call_swap
![Page 40: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/40.jpg)
Revisiting swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
swap:pushl %ebpmovl %esp,%ebppushl %ebx
movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Body
SetUp
Finish
![Page 41: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/41.jpg)
swap Setup #1
swap:pushl %ebpmovl %esp,%ebppushl %ebx
Resulting Stack
&zip2
&zip1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp
%ebp•••
%esp
![Page 42: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/42.jpg)
swap Setup #1
swap:pushl %ebpmovl %esp,%ebppushl %ebx
&zip2
&zip1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp
%ebp•••
%esp
![Page 43: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/43.jpg)
swap Setup #1
swap:pushl %ebpmovl %esp,%ebppushl %ebx
&zip2
&zip1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
Resulting Stack
![Page 44: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/44.jpg)
swap Setup #1
swap:pushl %ebpmovl %esp,%ebppushl %ebx
&zip2
&zip1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
![Page 45: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/45.jpg)
1284
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 ypmovl 8(%ebp),%edx # get xp. . .
Offset relative to %ebp
![Page 46: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/46.jpg)
swap Finish #1
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
Resulting Stack
Old %ebx
Observation: Saved and restored register %ebx
![Page 47: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/47.jpg)
swap Finish #2
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
Rtn adr
Old %ebp %ebp
•••
%espOld %ebx
![Page 48: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/48.jpg)
swap Finish #2
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
Resulting Stack
![Page 49: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/49.jpg)
swap Finish #2
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
![Page 50: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/50.jpg)
swap Finish #3
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
Resulting Stack
yp
xp
Rtn adr
%ebp•••
%esp
![Page 51: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/51.jpg)
swap Finish #4
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
Rtn adr
%ebp•••
%esp
![Page 52: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/52.jpg)
swap Finish #4
yp
xp
Rtn adr
Old %ebp %ebp
•••
%esp
swap’s Stack
Old %ebx
movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret
yp
xp
%ebp•••
%esp
Resulting Stack
Observation Saved & restored register %ebx Didn’t do so for %eax, %ecx, or %edx
![Page 53: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/53.jpg)
Disassembled swap080483a4 <swap>: 80483a4: 55 push %ebp 80483a5: 89 e5 mov %esp,%ebp 80483a7: 53 push %ebx 80483a8: 8b 55 08 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: 89 02 mov %eax,(%edx) 80483b4: 89 19 mov %ebx,(%ecx) 80483b6: 5b pop %ebx 80483b7: c9 leave 80483b8: c3 ret
8048409: e8 96 ff ff ff call 80483a4 <swap> 804840e: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
Calling Code
![Page 54: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/54.jpg)
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, %edxcall whoaddl %edx, %eax
• • •ret
who:• • •movl 8(%ebp), %edxaddl $91125, %edx
• • •ret
![Page 55: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/55.jpg)
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
![Page 56: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/56.jpg)
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-SaveTemporaries
Callee-SaveTemporaries
Special
![Page 57: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/57.jpg)
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 addressRed Hatv. 6.2~1920MBmemorylimit
FF
BF
7F
3F
C0
80
40
00
Stack
DLLs
TextData
Heap
Heap
08
![Page 58: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/58.jpg)
Linux Memory AllocationLinked
BF
7F
3F
80
40
00
Stack
DLLs
TextData
08
Some Heap
BF
7F
3F
80
40
00
Stack
DLLs
TextData
Heap
08
MoreHeap
BF
7F
3F
80
40
00
Stack
DLLs
TextDataHeap
Heap
08
Initially
BF
7F
3F
80
40
00
Stack
TextData
08
![Page 59: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/59.jpg)
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 0x0804856f
Stack Address 0xbffffc78
Initially
BF
7F
3F
80
40
00
Stack
TextData
08
![Page 60: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/60.jpg)
Dynamic Linking Example(gdb) print malloc $1 = {<text variable, no debug info>} 0x8048454 <malloc>(gdb) run Program exited normally.(gdb) print malloc $2 = {void *(unsigned int)} 0x40006240 <malloc>
Initially Code in text segment that invokes dynamic linker Address 0x8048454 should be read 0x08048454
Final Code in DLL region
Linked
BF
7F
3F
80
40
00
Stack
DLLs
TextData
08
![Page 61: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/61.jpg)
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 ... */}
![Page 62: Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are](https://reader030.vdocument.in/reader030/viewer/2022032702/56649cce5503460f94998a99/html5/thumbnails/62.jpg)
Example Addresses
$esp 0xbffffc78p3 0x500b5008p1 0x400b4008Final malloc 0x40006240p4 0x1904a640 p2 0x1904a538beyond 0x1904a524big_array 0x1804a520huge_array 0x0804a510main() 0x0804856fuseless() 0x08048560Initial malloc 0x08048454
BF
7F
3F
80
40
00
Stack
DLLs
TextDataHeap
Heap
08