1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.

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

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
MACHINE-LEVEL PROGRAMMING II: ARITHMETIC & CONTROL.
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
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.
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
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.
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.
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified.
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.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
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.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Spring 2016Assembly Review 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);
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 2 Control Flow
Samira Khan University of Virginia Feb 2, 2017
Credits and Disclaimers
CSCE 212 Computer Architecture
Credits and Disclaimers
Conditional Branch Example
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming III: Procedures
Machine-Level Programming II: Control
Machine-Level Programming II: Control Flow
x86-64 Programming III & The Stack CSE 351 Winter 2018
Carnegie Mellon Machine-Level Programming II: Control : Introduction to Computer Systems 6th Lecture, Sept. 13, 2018.
Machine-Level Programming III: Switch Statements and IA32 Procedures
Instructors: Majd Sakr and Khaled Harras
x86-64 Programming III & The Stack CSE 351 Winter 2018
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:
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
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming II: Control Flow
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
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
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Carnegie Mellon Ithaca College
Credits and Disclaimers
Credits and Disclaimers
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Credits and Disclaimers
Credits and Disclaimers
Presentation transcript:

1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective, 3 rd ed.”, Randy Bryant & David O’Hallaron, 2015

2 Today Complete addressing mode, address computation (leal) Arithmetic operations x86-64 Control: Condition codes Conditional branches and moves Loops

3 C Code int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } Goto Version int pcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } int pcount_do(unsigned x) { int 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

4 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 rep; 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

5 C Code do Body while ( Test ); do Body while ( Test ); Goto Version loop: Body if ( Test ) goto loop loop: Body if ( Test ) goto loop General “Do-While” Translation Body: Test returns integer  = 0 interpreted as false  ≠ 0 interpreted as true { Statement 1 ; Statement 2 ; … Statement n ; }

6 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:

7 C Code long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } Jump to Middle Version 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; } 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; } While Loop Example #1 Compare to do-while version of function Initial goto starts loop at test

8 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:

9 C Code long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } Do-While Version long pcount_goto_dw (unsigned long x) { long result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if(x) goto loop; done: return result; } long pcount_goto_dw (unsigned long x) { long result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if(x) goto loop; done: return result; } While Loop Example #2 Compare to do-while version of function Initial conditional guards entrance to loop

10 C Code “For” Loop Example Is this code equivalent to other versions? #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; }

11 “For” Loop Form for ( Init ; Test ; Update ) Body General Form for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit;} { unsigned bit = (x >> i) & 0x1; result += bit;} Init Test Update Body

12 “For” Loop  While Loop for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version

13 For-While Conversion 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; } 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 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; } { unsigned bit = (x >> i) & 0x1; result += bit; } Init Test Update Body

14 “For” Loop  …  Goto for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version Init ; if (! Test ) goto done; do Body Update while( Test ); done: Init ; if (! Test ) goto done; do Body Update while( Test ); done: Init ; if (! Test ) goto done; loop: Body Update if ( Test ) goto loop; done: Init ; if (! Test ) goto done; loop: Body Update if ( Test ) goto loop; done:

15 C Code “For” Loop Do-While Conversion Initial test can be optimized away 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; } 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; } Goto Version long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } Init ! Test Body Update Test

16 C Code “For” Loop Do-While Conversion Initial test can be optimized away 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; } 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; } Goto Version long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } Init ! Test Body Update Test What if WSIZE < 0? Then compiler will recognize that the loop will never run and will eliminate code

17 “For” Loop Conversion Example Assembly Version mov %eax,-0xc(%rbp) mov -0xc(%rbp),%eax add %rax,-0x8(%rbp) addl $0x1,-0x10(%rbp) mov -0x10(%rbp),%eax cmp $0x1f,%eax jbe 0x mov -0x8(%rbp),%rax pop %rbp retq mov %eax,-0xc(%rbp) mov -0xc(%rbp),%eax add %rax,-0x8(%rbp) addl $0x1,-0x10(%rbp) mov -0x10(%rbp),%eax cmp $0x1f,%eax jbe 0x mov -0x8(%rbp),%rax pop %rbp retq _pcount_for: push %rbp mov %rsp,%rbp mov %edi,-0x14(%rbp) movq $0x0,-0x8(%rbp) movl $0x0,-0x10(%rbp) jmp 0x mov -0x10(%rbp),%eax mov -0x14(%rbp),%edx mov %eax,%ecx shr %cl,%edx mov %edx,%eax and $0x1,%eax _pcount_for: push %rbp mov %rsp,%rbp mov %edi,-0x14(%rbp) movq $0x0,-0x8(%rbp) movl $0x0,-0x10(%rbp) jmp 0x mov -0x10(%rbp),%eax mov -0x14(%rbp),%edx mov %eax,%ecx shr %cl,%edx mov %edx,%eax and $0x1,%eax

18 Summary Today  Complete addressing mode, address computation ( leal )  Arithmetic operations  Control: Condition codes  Conditional branches & conditional moves  Loops Next Time  Switch statements  Stack  Call / return  Procedure call discipline