CS201- Lecture 8 IA32 Flow Control

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: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
תרגול 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.
University of Washington Roadmap 1 car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Car c = new Car(); c.setMiles(100);
Lecture 7 Flow of Control Topics Finish Lecture 6 slides Lab 02 = datalab comments Assembly Language flow of control Test 1 – a week from wednesday February.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Carnegie Mellon Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Carnegie Mellon.
Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming IV Control Comp 21000: Introduction.
Machine-Level Programming 2 Control Flow
Samira Khan University of Virginia Feb 2, 2017
Conditional Branch Example
Assembly Programming III CSE 410 Winter 2017
Assembly Programming III CSE 351 Spring 2017
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II: Control
Machine-Level Representation of Programs II
Switch & Procedures & The Stack I CSE 351 Winter 2017
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming II: Control Flow
x86-64 Programming III & The Stack CSE 351 Winter 2018
x86 Programming II CSE 351 Autumn 2016
x86-64 Programming II CSE 351 Autumn 2017
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Controlling Program Flow
Assembly Programming IV CSE 351 Spring 2017
x86-64 Programming III & The Stack CSE 351 Winter 2018
x86-64 Programming II CSE 351 Winter 2018
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Assembly Programming III CSE 351 Spring 2017
Instructor: David Ferry
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Assembly Programming IV CSE 410 Winter 2017
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Carnegie Mellon Wrap-up of Machine-Level Programming II: Control : Introduction to Computer Systems Sept. 18, 2018.
Machine-Level Programming 2 Control Flow
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
Machine-Level Programming 2 Control Flow
x86-64 Programming III & The Stack CSE 351 Autumn 2017
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Summer 2018
x86-64 Programming III CSE 351 Autumn 2018
x86-64 Programming II CSE 351 Autumn 2018
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
x86-64 Programming II CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3* * Modified slides from.
x86-64 Programming III CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Credits and Disclaimers
Credits and Disclaimers
CS201- Lecture 7 IA32 Data Access and Operations Part II
Presentation transcript:

CS201- Lecture 8 IA32 Flow Control Raoul Rivas Portland state university

Announcements CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Processor State Information about currently executing program Temporary data ( %rax, … ) Location of runtime stack ( %rsp ) Location of current code control point ( %rip, … ) Status of recent tests ( CF, ZF, SF, OF ) Registers %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Instruction pointer %rip Current stack top CF ZF SF OF Condition codes CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Condition Codes Single bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmetic operations Example: addq Src,Dest ↔ t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) Not set by leaq instruction CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Compare Explicit Setting by Compare Instruction cmpq Src2, Src1 cmpq 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 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Jump jX Instructions Jump to different part of code depending on condition codes jX Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

C Goto Statement C allows goto statement Jump to position designated by label long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j (long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

C Goto Statement CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Conditional Branches long absdiff_j (long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x:y jle .L4 movq %rdi, %rax subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax Register Use(s) %rdi Argument x %rsi Argument y %rax Return value CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Conditional Branches Recipe C Code val = Test ? Then_Expr : Else_Expr; val = x>y ? x-y : y-x; Goto Version ntest = !Test; if (ntest) goto Else; val = Then_Expr; goto Done; Else: val = Else_Expr; Done: . . . Create separate code regions for then & else expressions Execute appropriate one CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Conditional Move Conditional Move Instructions (CMOVxx) Why? C Code Instruction supports: if (Test) Dest  Src Supported in post-1995 x86 processors GCC tries to use them But, only when known to be safe Why? Branches are very disruptive to instruction flow through pipelines Modern Processors try to Predict the outcome of the Branch (Taken or Not Taken) Easy for Loops. Hard for If/Else Conditional moves do not require control transfer C Code val = Test ? Then_Expr : Else_Expr; Goto Version result = Then_Expr; eval = Else_Expr; nt = !Test; if (nt) result = eval; return result; CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Conditional Move Example long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value absdiff: movq %rdi, %rax # x subq %rsi, %rax # result = x-y movq %rsi, %rdx subq %rdi, %rdx # eval = y-x cmpq %rsi, %rdi # x:y cmovle %rdx, %rax # if <=, result = eval ret CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Do-While Loop C Code Goto Version long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Count number of 1’s in argument x (“popcount”) Use conditional branch to either continue looping or to exit loop CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Do-While Loop Translation C Code Goto Version do Body while (Test); loop: Body if (Test) goto loop CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Do-While Loop long pcount_goto (unsigned long x) { long result = 0; result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop rep; ret CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

While Translation “Jump-to-middle” translation Used with -Og Goto Version goto test; loop: Body test: if (Test) goto loop; done: While version while (Test) Body CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

While Translation C Code Jump to Middle Version long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; long pcount_goto_jtm (unsigned long x) { long result = 0; goto test; loop: result += x & 0x1; x >>= 1; test: if(x) goto loop; return result; } CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Optimized While Translation While version “Do-while” conversion Used with –O1 while (Test) Body Goto Version Do-While Version if (!Test) goto done; loop: Body if (Test) goto loop; done: if (!Test) goto done; do Body while(Test); done: CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

for (Init; Test; Update ) For Loop Init General Form i = 0 for (Init; Test; Update ) Body Test i < WSIZE #define WSIZE 8*sizeof(int) long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) unsigned bit = (x >> i) & 0x1; result += bit; } return result; Update i++ Body { unsigned bit = (x >> i) & 0x1; result += bit; } CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

For to While Conversion For Version for (Init; Test; Update ) Body While Version Init; while (Test ) { Body Update; } CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

For to While Conversion Init long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) unsigned bit = (x >> i) & 0x1; result += bit; i++; } return result; i = 0 Test i < WSIZE Update i++ Body { unsigned bit = (x >> i) & 0x1; result += bit; } CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Switch Statement Multiple case labels Fall through cases Missing cases long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; case 5: case 6: w -= z; default: w = 2; } return w; Switch Statement Multiple case labels Here: 5 & 6 Fall through cases Here: 2 Missing cases Here: 4 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Switch to If/Else Conversion long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ … default: w = 2; } return w; Convert each case to an If/Else long switch_eg (long x, long y, long z) { long w = 1; If (x==1) w = y*z; Else If (x==2) w = y/z; … Else w=2; return w; } CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Jump Table Optimization Jump Targets Switch Form Jump Table Targ0: Code Block switch(x) { case val_0: Block 0 case val_1: Block 1 • • • case val_n-1: Block n–1 } jtab: Targ0 Targ1 Targ2 Targ1: Code Block 1 • Targ2: Code Block 2 Targn-1 • Translation goto *JTab[x]; Targn-1: Code Block n–1 Pointer to Functions! CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Switch Optimization Example Jump table switch(x) { case 1: // .L3 w = y*z; break; case 2: // .L5 w = y/z; /* Fall Through */ case 3: // .L9 w += z; case 5: case 6: // .L7 w -= z; default: // .L8 w = 2; } .section .rodata .align 8 .L4: .quad .L8 # x = 0 .quad .L3 # x = 1 .quad .L5 # x = 2 .quad .L9 # x = 3 .quad .L8 # x = 4 .quad .L7 # x = 5 .quad .L7 # x = 6 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Switch Optimization Example Jump table long switch_eg(long x, long y, long z) { long w = 1; switch(x) { . . . } return w; .section .rodata .align 8 .L4: .quad .L8 # x = 0 .quad .L3 # x = 1 .quad .L5 # x = 2 .quad .L9 # x = 3 .quad .L8 # x = 4 .quad .L7 # x = 5 .quad .L7 # x = 6 Setup: Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z %rax Return value switch_eg: movq %rdx, %rcx cmpq $6, %rdi # x:6 ja .L8 # Use default jmp *.L4(,%rdi,8) # goto *JTab[x] Indirect jump CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Switch Optimization Example Table Structure Each target requires 8 bytes Base address at .L4 Jumping Direct: jmp .L8 Jump target is denoted by label .L8 Indirect: jmp *.L4(,%rdi,8) Start of jump table: .L4 Must scale by factor of 8 (addresses are 8 bytes) Fetch target from effective Address .L4 + x*8 Only for 0 ≤ x ≤ 6 Jump table .section .rodata .align 8 .L4: .quad .L8 # x = 0 .quad .L3 # x = 1 .quad .L5 # x = 2 .quad .L9 # x = 3 .quad .L8 # x = 4 .quad .L7 # x = 5 .quad .L7 # x = 6 CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)

Conclusion Most instruction modify condition codes Side effect sometimes useful: Overflow check, avoid comparisons Compare and Test instruction used to set condition codes explicitly JMP – Unconditional Jump Conditional Jumps based on codes Structured programming is translated to assembly using conditional jumps and labels Large switches are implemented using jump tables Indirect Jump CS201: Computer Science Programming Raoul Rivas (Based on slides from Bryant and O’Hallaron)