Conditional Branch Example

Slides:



Advertisements
Similar presentations
University of Amsterdam Computer Systems – Control in C Arnoud Visser 1 Computer Systems New to C?
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
MACHINE-LEVEL PROGRAMMING II: ARITHMETIC & CONTROL.
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 II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Y86 Processor State Program Registers
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control : Introduction to Computer Systems 5 th Lecture, Sep. 7, 2010 Carnegie Mellon.
1 Seoul National University Machine-Level Programming II: Arithmetic & Control.
II:1 x86 Assembly - Control. II:2 Alternate reference source Go to the source: Intel 64 and IA32 
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.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
About the Slides for Introduction to Computer Systems : Introduction to Computer Systems 0th Lecture, Sep. 1, 2015 Markus Püschel ETH Zurich (with.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
University of Washington x86 Programming II The Hardware/Software Interface CSE351 Winter 2013.
1 Assembly Language: Function Calls Jennifer Rexford.
Carnegie Mellon About the New Slides for Introduction to Computer Systems /18-243, spring 2009 Markus Püschel Electrical and Computer Engineering.
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 II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Carnegie Mellon Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Carnegie Mellon.
1 Binghamton University Machine-Level Programming II: Arithmetic & Control CS220: Computer Systems II.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements class06.ppt.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
X86 Assembly, and C-to-assembly
Aaron Miller David Cohen Spring 2011
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II Control Flow Sept. 9, 1999
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Machine-Level Programming II: Control Flow
Computer Architecture adapted by Jason Fritts then by David Ferry
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
Homework Reading Machine Projects Labs PAL, pp
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
CS201- Lecture 8 IA32 Flow Control
Carnegie Mellon Ithaca College
Credits and Disclaimers
Compiler Construction CS 606 Sohail Aslam Lecture 1.
Presentation transcript:

Conditional Branch Example absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 int absdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; Setup Body1 Note labels Finish Body2 x86

Conditional Branch Example (Cont.) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } int absdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; Lets look at the code a little differently to make it easier to look at the assembly version. C allows “goto” as means of transferring control Closer to machine-level programming style Generally considered bad coding style x86

Conditional Branch Example (Cont.) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 Loading x and y into registers. int x %edx int y %eax x86

Conditional Branch Example (Cont.) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 Performing the comparison. Goto else. int x %edx int y %eax x86

Conditional Branch Example (Cont.) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 Edx = edx-eax. int x %edx int y %eax x86

Conditional Branch Example (Cont.) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 int x %edx int y %eax x86

Conditional Branch Example (Cont.) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; Exit: return result; Else: result = y-x; goto Exit; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L7 subl %eax, %edx movl %edx, %eax .L8: leave ret .L7: subl %edx, %eax jmp .L8 int x %edx int y %eax x86

General Conditional Expression Translation if (Test) val = Then-Expr; else val = Else-Expr; C Code val = Test ? Then-Expr : Else-Expr; result = x>y ? x-y : y-x; Test is expression returning integer = 0 interpreted as false 0 interpreted as true Create separate code regions for then & else expressions Execute appropriate one How might you make this more efficient? Goto Version More efficient if we had conditional move. It would avoid using a branch instruction. nt = !Test; if (nt) goto Else; val = Then-Expr; Done: . . . Else: val = Else-Expr; goto Done; x86

Conditionals: x86-64 Conditional move instruction cmovC src, dest int absdiff( int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; absdiff: # x in %edi, y in %esi movl %edi, %eax # eax = x movl %esi, %edx # edx = y subl %esi, %eax # eax = x-y subl %edi, %edx # edx = y-x cmpl %esi, %edi # x:y cmovle %edx, %eax # eax=edx if <= ret cmov instructions available in IA32 since 1995, but not used by gcc unless –march=686 flag used. They are used regularly in x86-64. Conditional move instruction cmovC src, dest Move value from src to dest if condition C holds More efficient than conditional branching (simple control flow) But overhead: both branches are evaluated x86

PC Relative Addressing 0x100 cmp r2, r3 0x1000 0x102 je 0x70 0x1002 0x104 … 0x1004 … … … 0x172 add r3, r4 0x1072 PC relative branches are relocatable Absolute branches are not x86