Reading Condition Codes (Cont.)

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

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
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 III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
Machine-Level Programming II Control Flow Sept. 13, 2001 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
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
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
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.
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.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
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.
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.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
A job ad at a game programming company
Machine-Level Programming 2 Control Flow
C function call conventions and the stack
CSCE 212 Computer Architecture
Conditional Branch Example
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming III: Procedures
Machine-Level Programming II Control Flow Sept. 9, 1999
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Chapter 3 Machine-Level Representation of Programs
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.
Instructor: David Ferry
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 2 Control Flow
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
Machine-Level Programming: Introduction
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Presentation transcript:

Reading Condition Codes (Cont.) SetX Instructions Set single byte based on combinations of condition codes One of 8 addressable byte registers Embedded within first 4 integer registers Does not alter remaining 3 bytes Typically use movzbl to finish job %eax %ah %al %edx %dh %dl %ecx %ch %cl %ebx %bh %bl %esi int gt (int x, int y) { return x > y; } %edi %esp Body %ebp movl 12(%ebp),%eax # eax = y cmpl %eax,8(%ebp) # Compare x : y setg %al # al = x > y movzbl %al,%eax # Zero rest of %eax Note inverted ordering!

Jumping jX Instructions Jump to different part of code depending on condition codes

Conditional Branch Example _max: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax cmpl %eax,%edx jle L9 movl %edx,%eax L9: leave ret Set Up int max(int x, int y) { if (x > y) return x; else return y; } Body Finish

Conditional Branch Example (Cont.) int goto_max(int x, int y) { int rval = y; int ok = (x <= y); if (ok) goto done; rval = x; done: return rval; } C allows “goto” as means of transferring control Closer to machine-level programming style Generally considered bad coding style movl 8(%ebp),%edx # edx = x movl 12(%ebp),%eax # eax = y cmpl %eax,%edx # x : y jle L9 # if <= goto L9 movl %edx,%eax # eax = x L9: # Done: Skipped when x  y

“Do-While” Loop Example C Code Goto Version int fact_do (int x) { int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; } int fact_goto(int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } Use backward branch to continue looping Only take branch when “while” condition holds

“Do-While” Loop Compilation Goto Version Assembly int fact_goto (int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop; return result; } _fact_goto: pushl %ebp # Setup movl %esp,%ebp # Setup movl $1,%eax # eax = 1 movl 8(%ebp),%edx # edx = x L11: imull %edx,%eax # result *= x decl %edx # x-- cmpl $1,%edx # Compare x : 1 jg L11 # if > goto loop movl %ebp,%esp # Finish popl %ebp # Finish ret # Finish Registers %edx x %eax result

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

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

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

Stack Operation Examples pushl %eax popl %edx 0x110 0x110 0x110 0x10c 0x10c 0x10c 0x108 123 0x108 123 0x108 123 0x104 213 0x104 213 %eax 213 %eax 213 %eax 213 %edx 555 %edx 555 %edx 213 555 %esp 0x108 %esp 0x104 0x108 %esp 0x108 0x104

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 Example from disassembly 804854e: 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

Procedure Call Example 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x104 0x108 %eip 0x804854e %eip 0x8048b90 0x804854e %eip is program counter

Procedure Return Example 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x108 0x104 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip is program counter

Call Chain Example Call Chain Code Structure Procedure amI recursive yoo(…) { • who(); } yoo who(…) { • • • amI(); } who amI amI amI(…) { • amI(); } amI amI Procedure amI recursive

Stack Frames Contents Management Pointers Local variables Return information Temporary space Management Space allocated when enter procedure “Set-up” code Deallocated when return “Finish” code Pointers Stack pointer %esp indicates stack top Frame pointer %ebp indicates start of current frame yoo who amI Frame Pointer %ebp proc Stack Pointer %esp Stack “Top”

Stack Operation Call Chain • Frame Pointer %ebp yoo yoo(…) { • Stack who(); } Stack Pointer %esp yoo

Stack Operation Call Chain • yoo who(…) { Frame • • • Pointer amI(); } Frame Pointer %ebp yoo who who Stack Pointer %esp

Stack Operation Call Chain • yoo amI(…) { • yoo amI(); who } Frame who Pointer %ebp who amI amI Stack Pointer %esp

Stack Operation Call Chain • yoo amI(…) { • yoo amI(); who } who amI Frame Pointer %ebp amI amI Stack Pointer %esp

Stack Operation Call Chain • yoo amI(…) { • yoo amI(); who } who amI Frame Pointer %ebp amI amI Stack Pointer %esp

Stack Operation Call Chain • yoo amI(…) { • yoo amI(); who } who amI Frame Pointer %ebp amI amI Stack Pointer %esp amI

Stack Operation Call Chain • yoo amI(…) { • yoo amI(); who } Frame who Pointer %ebp who amI amI Stack Pointer %esp amI amI

Stack Operation Call Chain • yoo who(…) { Frame • • • Pointer amI(); } Frame Pointer %ebp yoo who who Stack Pointer %esp amI amI amI

Stack Operation Call Chain • yoo amI(…) { • } yoo who Frame Pointer %ebp who amI amI amI Stack Pointer %esp amI amI

Stack Operation Call Chain • yoo who(…) { • • • amI(); } Frame Pointer %ebp yoo who who Stack Pointer %esp amI amI amI amI

Stack Operation Call Chain • Frame Pointer %ebp yoo yoo(…) { • who(); } Stack Pointer %esp yoo who amI amI amI amI

IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) Parameters for function about to call “Argument build” 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 Caller Frame Arguments Frame Pointer (%ebp) Return Addr Old %ebp Saved Registers + Local Variables Argument Build Stack Pointer (%esp)

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

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

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

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

swap Setup #3 Resulting Entering Stack Stack • %ebp • &zip2 yp &zip1 xp Rtn adr %esp Rtn adr Old %ebp %ebp t1 swap: pushl %ebp movl %esp,%ebp subl $16, %esp t0 %esp

Effect of swap Setup Entering Stack Resulting Stack • %ebp • Offset (relative to %ebp) &zip2 12 yp &zip1 8 xp Rtn adr %esp 4 Rtn adr Old %ebp %ebp t1 t0 movl 8(%ebp),%eax # get xp . . . Body %esp

swap Finish swap’s swap’s Stack Stack • %ebp • Offset Offset 12 yp 12 8 xp 8 xp 4 Rtn adr 4 Rtn adr %esp Old %ebp %ebp %esp leave ret

swap Finish swap’s Stack Exiting Stack %ebp • %ebp • Offset &zip2 12 yp &zip1 %esp 8 xp 4 Rtn adr %esp leave ret

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 Integer Registers Two have special uses %ebp, %esp Three managed as callee-save %ebx, %esi, %edi Old values saved on stack prior to using Three managed as caller-save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well Register %eax also stores returned value %eax Caller-Save Temporaries %edx %ecx %ebx Callee-Save Temporaries %esi %edi %esp Special %ebp

Pointer Code Recursive Procedure Top-Level Call void s_helper (int x, int *accum) { if (x <= 1) return; else { int z = *accum * x; *accum = z; s_helper (x-1,accum); } int sfact(int x) { int val = 1; s_helper(x, &val); return val; } Pass pointer to update location

Creating & Initializing Pointer Initial part of sfact _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1 _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1 _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1 _sfact: pushl %ebp # Save %ebp movl %esp,%ebp # Set %ebp subl $16,%esp # Add 16 bytes movl 8(%ebp),%edx # edx = x movl $1,-4(%ebp) # val = 1 8 x 4 Rtn adr Old %ebp %ebp -4 Temp. Space %esp val = 1 Unused -8 Using Stack for Local Variable Variable val must be stored on stack Need to create pointer to it Compute pointer as -4(%ebp) Push on stack as second argument -12 -16 int sfact(int x) { int val = 1; s_helper(x, &val); return val; }

Passing Pointer Calling s_helper from sfact Stack at time of call 8 x leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val • • • # Finish leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val • • • # Finish leal -4(%ebp),%eax # Compute &val pushl %eax # Push on stack pushl %edx # Push x call s_helper # call movl -4(%ebp),%eax # Return val • • • # Finish 4 Rtn adr Old %ebp %ebp -4 val =x! val = 1 &val -8 Unused -12 int sfact(int x) { int val = 1; s_helper(x, &val); return val; } -16 %esp x

Using Pointer Register %ecx holds x void s_helper (int x, int *accum) { • • • int z = *accum * x; *accum = z; } accum*x %edx accum x %eax accum*x %ecx x • • • movl %ecx,%eax # z = x imull (%edx),%eax # z *= *accum movl %eax,(%edx) # *accum = z Register %ecx holds x Register %edx holds pointer to accum Use access (%edx) to reference memory