1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.

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

1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, Jan 29, 2015 Carnegie.
MACHINE-LEVEL PROGRAMMING II: ARITHMETIC & CONTROL.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, May 28, 2015 Carnegie.
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
1 Introduction to x86 Assembly, part II or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from
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 
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
Controlling Program Flow. – 2 – Control Flow Computers execute instructions in sequence. Except when we change the flow of control Jump and Call instructions.
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, Sep. 11, 2014 Carnegie.
תרגול 5 תכנות באסמבלי, המשך
University of Washington x86 Programming II The Hardware/Software Interface CSE351 Winter 2013.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
University of Washington This week Lab 1 due 11pm today  Frustrating? Awesome? Both? Neither? Lab 2 out later today, due 11pm Wednesday July 17  Disassembly,
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.
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 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
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
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming II: Control Flow
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.
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:
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
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
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.
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Credits and Disclaimers
Presentation transcript:

1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon Slides adapted from Bryant and O’Hallaron

2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today: How control flow is done Condition codes Conditional branches Loops Switch Statements

3 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Processor State (x86-64, Partial) Registers Condition codes (single bit registers) %rip CFZFSFOF %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp

4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Condition Codes (implicit setting) Implicitly set by arithmetic operations Example: addq Src, Dest  t = a + b CF (Carry Flag) set if unsigned overflow ZF (Zero Flag) set if t == 0 SF (Sign Flag) set if t < 0 (as signed) OF (OverFlow Flag) set if signed overflow (a>0 && b>0 && t =0) Not set by leaq

5 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Condition Codes (Explicit Setting: Compare) Explicit set by Compare Instruction  cmpq b,a like computing a-b without setting destination  CF set if unsigned overflow  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)

6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Condition Codes (Explicit Setting: Test) Explicit set by Test instruction  testq b, a (like computing a&b without setting destination)  ZF set when a&b == 0  SF set when a&b < 0

7 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Reading Condition Codes SetX Instructions  Set low-order byte of destination register to 0/1 based on condition codes  Does not alter remaining 7 bytes SetXConditionDescription seteZF Equal / Zero setne~ZF Not Equal / Not Zero setsSF Negative setns~SF Nonnegative setg~(SF^OF)&~ZF Greater (Signed) setge~(SF^OF) Greater or Equal (Signed) setl(SF^OF) Less (Signed) setle(SF^OF)|ZF Less or Equal (Signed) seta~CF&~ZF Above (unsigned) setbCF Below (unsigned)

8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp x86-64 Integer Registers %al %bl %cl %dl %sil %dil %spl %bpl %r8b %r9b %r10b %r11b %r12b %r13b %r14b %r15b %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp

9 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition cmpq %rsi, %rdi # Compare x:y setg %al # Set when > movzbl %al, %eax # Zero rest of %eax ret Carnegie Mellon Reading Condition Codes (Cont.) SetX Example: int gt (long x, long y) { return x > y; } int gt (long x, long y) { return x > y; } RegisterUse(s) %rdi Argument x %rsi Argument y %eax Return value

10 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today Control: Condition codes Conditional branches Loops Switch Statements

11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Jumping jX Instructions: jump to different part of code jXConditionDescription jmp1 Unconditional jeZF Equal / Zero jne~ZF Not Equal / Not Zero jsSF 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) jbCF Below (unsigned)

12 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Conditional Branch Example (Old Style) long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; 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 ret gcc –Og -S –fno-if-conversion control.c RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Equivalent goto code long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } Machine code is essentially doing control flow with goto statement 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; } 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; } Do not write this normally

14 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Another way: Using Conditional Moves Conditional Move Instructions  cmovX instruction if (Test) Dest  Src  Supported in post-1995 x86 processors Why?  Branches (control transfer) can slow down execution  Conditional moves do not require control transfer

15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Conditional Move Example absdiff: movq %rdi, %rax # x subq %rsi, %rax # result = x-y movq %rsi, %rdx subq %rdi, %rdx # rdx = y-x cmpq %rsi, %rdi # x:y cmovle %rdx, %rax # if <=, result = rdx ret long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } RegisterUse(s) %rdi Argument x %rsi Argument y %rax Return value

16 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Expensive Computations Bad Cases for Conditional Move Both values get computed Only makes sense when computations are very simple val = Test(x) ? Hard1(x) : Hard2(x); Risky Computations Both values get computed May have undesirable effects val = p ? *p : 0; Computations with side effects Both values get computed Must be side-effect free val = x > 0 ? x*=7 : x+=3;

17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Today Control: Condition codes Conditional branches Loops Switch Statements

18 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon C Code long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } Goto Version long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } “Do-While” Loop Example Count number of 1’s in argument x (“popcount”) Use conditional branch to either continue looping or to exit loop

19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Goto Version “Do-While” Loop Compilation 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 ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } RegisterUse(s) %rdi Argument x %raxresult

20 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon C Code do … while ( Test ); do … while ( Test ); Goto Version loop: … if ( Test ) goto loop loop: … if ( Test ) goto loop General “Do-While” Translation

21 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon While version while ( Test ) Body while ( Test ) Body General “While” Translation #1 “Jump-to-middle” translation Used with -Og Goto Version goto test; loop: Body test: if ( Test ) goto loop; done: goto test; loop: Body test: if ( Test ) goto loop; done:

22 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon While version while ( Test ) Body while ( Test ) Body Do-While Version if (! Test ) goto done; do Body while( Test ); done: if (! Test ) goto done; do Body while( Test ); done: General “While” Translation #2 “Do-while” conversion Used with –O1 Goto Version if (! Test ) goto done; loop: Body if ( Test ) goto loop; done: if (! Test ) goto done; loop: Body if ( Test ) goto loop; done:

23 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon “For” Loop for ( Init ; Test ; Update ) Body long pcount_for(unsigned long x) { long result = 0; for (int i = 0; i < 8*sizeof(int); i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } long pcount_for(unsigned long x) { long result = 0; for (int i = 0; i < 8*sizeof(int); i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; }

24 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon “For” Loop  While Loop for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version

25 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Summary Today  Control: Condition codes  Conditional branches & conditional moves  Loops  Switch statements