IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.

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 – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
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 I: Introduction January 29, 2002 Topics Assembly Programmer’s Execution Model Accessing Information –Registers –Memory Arithmetic.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Machine-Level Programming I: Introduction Sept. 10, 2002 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction
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 I: Introduction Jan 27, 2004 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Introduction Apr. 10, 2006 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction Jan. 22, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming III: Procedures Jan 30, 2003
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Machine-Level Programming I: Introduction Apr. 14, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction Sept. 14, 2004 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Machine-Level Programming I: Introduction Sept. 10, 2007 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
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.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming – Introduction Today Assembly programmer’s exec model Accessing information Arithmetic operations.
Machine-Level Programming I: Topics History of Intel Processors – short...incomplete Assembly Programmer’s Execution Model Accessing Information Registers.
Y86 Processor State Program Registers
II:1 X86 Assembly - Data. II:2 Admin Quiz?  What happened?  Make-up options? Several missing from lab Attendance at lab is required Passing grade without.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations X86.1.ppt CS 105.
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.
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 I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-Level Programming I: Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS 105 “Tour of.
Machine-Level Programming I: Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS 105 “Tour of.
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.
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.
– 1 – IEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic.
Machine-Level Programming I: Introduction Jan. 30, 2001 Topics Assembly Programmer’s Execution Model Accessing Information –Registers –Memory Arithmetic.
Machine-Level Programming I: IA32 Assembly Language Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
X86 Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
A job ad at a game programming company
X86 Assembly - Data.
CS 3214 Computer Systems Lecture 2 Godmar Back.
Reading Condition Codes (Cont.)
Machine-Level Programming I:
Machine-Level Programming I:
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming 1 Introduction
asum.ys A Y86 Programming Example
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
Instructor: David Ferry
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Programming I: Introduction Feb. 1, 2000
Machine-Level Programming I: Introduction
X86 Assembly - Data.
Machine-Level Programming: Introduction
Machine-Level Programming I:
Machine-Level Programming I:
“Way easier than when we were students”
Machine-Level Programming I: Introduction Sept. 10, 2002
Presentation transcript:

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

IA32 Stack Pushing Pushing – 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 Popping Popping – popl Dest – Read operand at address given by %esp – Increment %esp by 4 – Write to Dest Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” +4

%esp %eax %edx %esp %eax %edx %esp %eax %edx 0x x108 0x10c 0x110 0x Stack Operation Examples 0x108 0x10c 0x x1080x104 pushl %eax 0x108 0x10c 0x x popl %edx 0x

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 value – Address of instruction beyond call Procedure return: – ret Pop address from stack; Jump to address

%esp %eip %esp %eip 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 is program counter

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

Procedure Set-up/Finish func: pushl %ebp movl %esp,%ebp. movl %ebp,%esp popl %ebp ret Body Set Up Finish

Addr Computation Instruction leal Src, Dest Src is address mode expression Set Dest to address denoted by expression Uses Computing address without doing memory reference E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y k = 1, 2, 4, or 8.

Using leal for Arith. Expressions int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } arith: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax movl 12(%ebp),%edx leal (%edx,%eax),%ecx leal (%edx,%edx,2),%edx sall $4,%edx addl 16(%ebp),%ecx leal 4(%edx,%eax),%eax imull %ecx,%eax movl %ebp,%esp popl %ebp ret Body Set Up Finish

Understanding arith int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } movl 8(%ebp),%eax# eax = x movl 12(%ebp),%edx# edx = y leal (%edx,%eax),%ecx# ecx = x+y (t1) leal (%edx,%edx,2),%edx# edx = 3*y sall $4,%edx# edx = 48*y (t4) addl 16(%ebp),%ecx# ecx = z+t1 (t2) leal 4(%edx,%eax),%eax# eax = 4+t4+x (t5) imull %ecx,%eax# eax = t5*t2 (rval) y x Rtn adr Old % ebp %ebp Offset Stack z 16

Understanding arith int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } # eax = x movl 8(%ebp),%eax # edx = y movl 12(%ebp),%edx # ecx = x+y (t1) leal (%edx,%eax),%ecx # edx = 3*y leal (%edx,%edx,2),%edx # edx = 48*y (t4) sall $4,%edx # ecx = z+t1 (t2) addl 16(%ebp),%ecx # eax = 4+t4+x (t5) leal 4(%edx,%eax),%eax # eax = t5*t2 (rval) imull %ecx,%eax

Example: 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

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 RegisterVariable %ecxyp %edxxp %eaxt1 %ebxt0 yp xp Rtn adr Old % ebp %ebp Offset Old % ebx -4

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 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 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 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 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 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 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 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 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 0x x104

Another Example int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } logical: pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax xorl 12(%ebp),%eax sarl $17,%eax andl $8185,%eax movl %ebp,%esp popl %ebp ret Body Set Up Finish movl 8(%ebp),%eaxeax = x xorl 12(%ebp),%eaxeax = x^y(t1) sarl $17,%eaxeax = t1>>17(t2) andl $8185,%eaxeax = t2 & = 8192, 2 13 – 7 = 8185