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

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: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
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.
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.
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.
1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.
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.
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
Instruction Set Architecture
Credits and Disclaimers
Machine-Level Programming I: Basics
CSCE 212 Computer Architecture
Credits and Disclaimers
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.
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.
Arrays CSE 351 Winter
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.
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
x86-64 Programming III & The Stack CSE 351 Autumn 2017
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
x86 Programming III CSE 351 Autumn 2016
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
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: Control Flow Sept. 12, 2007
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides.
Machine-Level Programming I: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Program Optimization CSE 238/2038/2138: Systems Programming
Carnegie Mellon Ithaca College
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
Getting Started Download the tarball for this session. It will include the following files: driver 64-bit executable driver.c C driver source bomb.h declaration.
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
CS201- Lecture 7 IA32 Data Access and Operations Part II
Presentation transcript:

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

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

“Do-While” Loop Example C Code Goto Version 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; 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

“Do-While” Loop Compilation Goto Version long pcount_goto (unsigned long x) { long result = 0; loop: 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

General “Do-While” Translation C Code Goto Version do Body while (Test); loop: Body if (Test) goto loop Body: Test returns integer = 0 interpreted as false ≠ 0 interpreted as true { Statement1; Statement2; … Statementn; }

General “While” Translation #1 “Jump-to-middle” translation Used with –O0 or -Og Goto Version goto test; loop: Body test: if (Test) goto loop; done: While version while (Test) Body

While Loop Example #1 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; } Compare to do-while version of function Initial goto starts loop at test

General “While” Translation #2 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:

While Loop Example #2 C Code Do-While Version long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } 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; } Compare to do-while version of function Initial conditional guards entrance to loop

“For” Loop Example C Code 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; Is this code equivalent to other versions?

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

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

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

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

“For” Loop Do-While Conversion Goto Version C Code 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 (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) unsigned bit = (x >> i) & 0x1; result += bit; } return result; Init !Test Body Update Test Initial test can be optimized away; i always < WSIZE since WSIZE is a constant

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

“For” Loop Conversion Example Assembly Version _pcount_for: 0x040052d <+0>: push %rbp 0x040052e <+1>: mov %rsp,%rbp 0x0400531 <+4>: mov %rdi,-0x28(%rbp) 0x0400535 <+8>: movq $0x0,-0x8(%rbp) 0x040053d <+16>: movq $0x0,-0x10(%rbp) 0x0400545 <+24>: jmp 0x400569 <pcount_for+60> 0x0400547 <+26>: mov -0x10(%rbp),%rax 0x040054b <+30>: mov -0x28(%rbp),%rdx 0x040054f <+34>: mov %eax,%ecx 0x0400551 <+36>: shr %cl,%rdx 0x0400554 <+39>: mov %rdx,%rax 0x0400557 <+42>: and $0x1,%eax 0x040055a <+45>: mov %eax,-0x14(%rbp) 0x040055d <+48>: mov -0x14(%rbp),%eax 0x0400560 <+51>: add %rax,-0x8(%rbp) 0x0400564 <+55>: addq $0x1,-0x10(%rbp) 0x0400569 <+60>: cmpq $0x3f,-0x10(%rbp) 0x040056e <+65>: jbe 0x400547 <pcount_for+26> 0x0400570 <+67>: mov -0x8(%rbp),%rax 0x0400574 <+71>: pop %rbp 0x0400575 <+72>: retq Note that the compiler changed 64 to 63 and < to <=

Summary Today Next Time 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