Machine-Level Programming 2 Control Flow

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

MACHINE-LEVEL PROGRAMMING II: ARITHMETIC & CONTROL.
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Machine-Level Programming II: Control Flow Sept. 12, 2002 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
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.
– 1 – Homework 1 Count 25 Mean 73.8 Std Min 22 Max 96 Mode 66 Median80.5.
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 
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
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.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
תרגול 5 תכנות באסמבלי, המשך
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.
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.
Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.
Machine-Level Programming II Control Flow Sept. 14, 2000 Topics Condition Codes –Setting –Testing Control Flow –If-then-else –Varieties of Loops –Switch.
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
Conditional Branch Example
Recitation 2 – 2/4/01 Outline Machine Model
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II Control Flow Sept. 9, 1999
Machine-Level Programming II: Control
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Machine-Level Programming II: Control Flow
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
3.5 Conditional Codes Spring, 2015 Euiseong Seo
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 Representation of Programs II
Carnegie Mellon Ithaca College
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
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
CS201- Lecture 8 IA32 Flow Control
Presentation transcript:

Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements

Processor State (IA32, Partial) Information about currently executing program Temporary data Location of current code control point Location of runtime stack Status of recent tests %eax %edx %ecx %ebx %esi %edi %esp %ebp General purpose registers Current stack top Current stack frame %eip Instruction pointer Condition codes CF ZF SF OF

Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Implicitly Set By Arithmetic Operations addl Src,Dest addq Src,Dest C analog: t = a + b (a = Src, b = Dest) CF set if carry out from most significant bit Used to detect unsigned overflow ZF set if t == 0 SF set if t < 0 OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) Not set by lea, inc, or dec instructions

Setting Condition Codes (cont.) Explicit Setting by Compare Instruction cmpl Src2,Src1 cmpq Src2,Src1 cmpl b,a like computing a-b without setting destination CF set if carry out from most significant bit Used for unsigned comparisons ZF set if a == b SF set if ( a - b ) < 0 OF set if two’s complement overflow ( a>0 && b<0 && (a - b)<0 ) || ( a<0 && b>0 && (a - b)>0 )

Setting Condition Codes (cont.) Explicit Setting by Test instruction testl Src2,Src1 testq Src2,Src1 Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask testl b,a like computing a&b without setting destination ZF set when a&b == 0 SF set when a&b < 0

Reading Condition Codes SetX Instructions Set single byte based on combinations of condition codes

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 %edi int gt ( int x, int y ) { return x > y; } %esp %ebp Body 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 int absdiff(int x, int y) { int result; if (x > y) { result = x - y; } else { result = y - x; } return result; 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 Set Up Body1 Finish Body2

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; } C allows “goto” as means of transferring control Closer to machine-level programming style Generally considered bad coding style # x in %edx, y in %eax cmpl %eax, %edx # Compare x:y jle .L7 # <= Goto Else subl %eax, %edx # x-= y movl %edx, %eax # result = x .L8: # Exit: Body1 .L7: # Else: subl %edx, %eax # result = y-x jmp .L8 # Goto Exit Body2

General Conditional Expression Translation C Code val = Test ? Then-Expr : Else-Expr; val = x > y ? x - y : y - x; Goto Version Test is expression returning integer = 0 interpreted as false 0 interpreted as true Create separate code regions for then & else expressions Execute appropriate one nt = !Test; if (nt) goto Else; val = Then-Expr; Done: . . . Else: val = Else-Expr; goto Done;

General Form with Conditional Move C Code val = Test ? Then-Expr ? Else-Expr; Both values get computed Overwrite then-value with else-value if condition doesn’t hold Conditional Move Version val = Then-Expr; vale = Else-Expr; val = vale if !Test;

Limitations of Conditional Move val = Then-Expr; vale = Else-Expr; val = vale if !Test; Don’t use when: Then-Expr or Else-Expr has side effect Then-Expr or Else-Expr requires significant computation int xgty = 0, xltey = 0; int absdiff_se( int x, int y ) { int result; if ( x > y ) { xgty++; result = x - y; } else { xltey++; result = y - x; } return result;

Implementing Loops IA32 All loops translated into form based on “do-while” IA32 compiler developed for machine where all operations costly

“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 Registers %edx x %eax result 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

General “Do-While” Translation Goto Version C Code do Body while (Test); loop: Body if (Test) goto loop Body can be any C statement Typically compound statement: Test is expression returning integer = 0 interpreted as false 0 interpreted as true { Statement1; Statement2; … Statementn; }

“While” Loop Example #1 C Code First Goto Version int fact_while(int x) { int result = 1; while ( x > 1 ) { result *= x; x = x - 1; }; return result; } int fact_while_goto(int x) { int result = 1; loop: if (!( x > 1 )) goto done; result *= x; x = x - 1; goto loop; done: return result; } Is this code equivalent to the do-while version? Must jump out of loop if test fails

Alternative “While” Loop Translation Second Goto Version C Code int fact_while( int x ) { int result = 1; while ( x > 1 ) { result *= x; x = x - 1; }; return result; } int fact_while_goto2( int x ) { int result = 1; if (!( x > 1 )) goto done; loop: result *= x; x = x - 1; if ( x > 1 ) goto loop; done: return result; } Historically used by GCC Uses same inner loop as do-while version Guards loop entry with extra test

General “While” Translation C Code while (Test) Body Do-While Version Goto Version if (!Test) goto done; do Body while(Test); done: if (!Test) goto done; loop: Body if (Test) goto loop; done:

New Style “While” Loop Translation Goto Version C Code int fact_while( int x ) { int result = 1; while ( x > 1 ) { result *= x; x = x - 1; }; return result; } int fact_while_goto3( int x ) { int result = 1; goto middle; loop: result *= x; x = x - 1; middle: if ( x > 1 ) goto loop; return result; } Recent technique for GCC Both IA32 & x86-64 First iteration jumps over body computation within loop

Jump-to-Middle While Translation C Code while (Test) Body Avoids duplicating test code Unconditional goto incurs no performance penalty for loops compiled in similar fashion Goto Version goto middle; loop: Body middle: if (Test) goto loop;

Jump-to-Middle Example int fact_while( int x ) { int result = 1; while ( x > 1 ) { result *= x; x--; }; return result; } Most common strategy for recent IA32 & x86-64 code generation # x in %edx, result in %eax jmp L34 # goto Middle L35: # Loop: imull %edx, %eax # result *= x decl %edx # x-- L34: # Middle: cmpl $1, %edx # x:1 jg L35 # if >, goto Loop

“For” “While” “Do-While” For Version While Version for (Init; Test; Update ) Body Init; while (Test ) { Body Update ; } Do-While Version Goto Version Init; if (!Test) goto done; do { Body Update ; } while (Test) done: Init; if (!Test) goto done; loop: Body Update ; if (Test) goto loop; done:

“For” “While” (Jump-to-Middle) For Version While Version for (Init; Test; Update ) Body Init; while (Test ) { Body Update ; } Goto Version Init; goto middle; loop: Body Update ; middle: if (Test) goto loop; done:

Summarizing C Control Assembler Control Compiler Standard Techniques if-then-else do-while while, for switch Assembler Control Conditional jump Conditional move Indirect jump Compiler Must generate assembly code to implement more complex control Standard Techniques IA32 loops converted to do-while form Large switch statements use jump tables Conditions in CISC CISC machines generally have condition code registers

Quiz Time Write the C that produced the following assembly language code This function takes a single integer argument located at 0x8(%ebp) 00000000 <mystery1>: 0: 55 push %ebp 1: 31 c0 xor %eax,%eax 3: 89 e5 mov %esp,%ebp 5: 8b 4d 08 mov 0x8(%ebp),%ecx 8: 85 c9 test %ecx,%ecx a: 7e 0c jle 18 <mystery1+0x18> c: 31 d2 xor %edx,%edx e: 89 f6 mov %esi,%esi 10: 8d 04 50 lea (%eax,%edx,2),%eax 13: 42 inc %edx 14: 39 d1 cmp %edx,%ecx 16: 75 f8 jne 10 <mystery1+0x10> 18: 5d pop %ebp 19: c3 ret

Quiz Time Write the C that produced the following assembly language code This function takes a single integer argument located at 0x8(%ebp) 00000000 <mystery2>: 0: 55 push %ebp 1: 31 c0 xor %eax,%eax 3: 89 e5 mov %esp,%ebp 5: 8b 4d 08 mov 0x8(%ebp),%ecx 8: 85 c9 test %ecx,%ecx a: 7e 0c jle 18 <mystery2+0x18> c: 31 d2 xor %edx,%edx e: 89 f6 mov %esi,%esi 10: 8d 04 50 lea (%eax,%edx,2),%eax 13: 42 inc %edx 14: 39 d1 cmp %edx,%ecx 16: 75 f8 jne 10 <mystery2+0x10> 18: 5d pop %ebp 19: c3 ret