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

Slides:



Advertisements
Similar presentations
University of Amsterdam Computer Systems – Control in C Arnoud Visser 1 Computer Systems New to C?
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
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 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.
1 Introduction to x86 Assembly, part II or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from
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.
1 Seoul National University Machine-Level Programming II: Arithmetic & Control.
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
Assembly Programmer’s View
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
About the Slides for Introduction to Computer Systems : Introduction to Computer Systems 0th Lecture, Sep. 1, 2015 Markus Püschel ETH Zurich (with.
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.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic & Control / : Introduction to Computer Systems 6 th Lecture, Sep. 11, 2014 Carnegie.
תרגול 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.
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.
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.
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Samira Khan University of Virginia Feb 2, 2017
Conditional Branch Example
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
Introduction to Assembly Language IA32
Machine-Level Programming II: Control Flow
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
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 III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
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.
Carnegie Mellon Ithaca College
Credits and Disclaimers
Presentation transcript:

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

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 Registers: %edxx %ecxresult movl$0, %ecx# result = 0.L2:# loop: movl%edx, %eax andl$1, %eax# t = x & 1 addl%eax, %ecx# result += t shrl%edx# x >>= 1 jne.L2# If !0, goto loop 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; }

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 C CodeGoto Version “While” Loop Example Is this code equivalent to the do-while version?  Must jump out of loop if test fails int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; } int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }

7 AssemblyGoto Version “While” Loop Example Note that the jmp instruction goes to the test, it doesn’t implement the initial if statement itself. _pcount_do: 00001d30pushl%ebp 00001d31movl%esp, %ebp 00001d33subl$16, %esp 00001d36movl8(%ebp), %eax 00001d39movl%eax, -4(%ebp) 00001d3cmovl$0, -16(%ebp) 00001d43jmp0x1d5e 00001d45movl-4(%ebp), %eax 00001d48andl$1, %eax 00001d4bmovl-16(%ebp), %ecx 00001d4eaddl%ecx, %eax 00001d50movl%eax, -16(%ebp) 00001d53movl-4(%ebp), %eax 00001d56shrl%eax 00001d5ecmpl$0, %eax 00001d61jne0x1d d63movl-16(%ebp), %eax 00001d72addl$16, %esp 00001d75popl%ebp 00001d76ret _pcount_do: 00001d30pushl%ebp 00001d31movl%esp, %ebp 00001d33subl$16, %esp 00001d36movl8(%ebp), %eax 00001d39movl%eax, -4(%ebp) 00001d3cmovl$0, -16(%ebp) 00001d43jmp0x1d5e 00001d45movl-4(%ebp), %eax 00001d48andl$1, %eax 00001d4bmovl-16(%ebp), %ecx 00001d4eaddl%ecx, %eax 00001d50movl%eax, -16(%ebp) 00001d53movl-4(%ebp), %eax 00001d56shrl%eax 00001d5ecmpl$0, %eax 00001d61jne0x1d d63movl-16(%ebp), %eax 00001d72addl$16, %esp 00001d75popl%ebp 00001d76ret int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; } int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }

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 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 “For” Loop Example Is this code equivalent to other versions? #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; }

10 “For” Loop Form for ( Init ; Test ; Update ) Body General Form for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } i = 0 i < WSIZE i++ { unsigned mask = 1 << i; result += (x & mask) != 0; } { unsigned mask = 1 << i; result += (x & mask) != 0; } Init Test Update Body

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

12 “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:

13 C Code “For” Loop Conversion Example Initial test can be optimized away #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } Goto Version int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result; } int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } 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

14 “For” Loop Conversion Example Redundant instructions have been removed, so addresses are not correct. Assembly Version 00001d43cmpl$0, %eax 00001d46setne%al 00001d49andb$1, %al 00001d4bmovzbl%al, %eax 00001d4emovl-20(%ebp), %edx 00001d51addl%edx, %eax 00001d53movl%eax, -20(%ebp) 00001d56movl-16(%ebp), %eax 00001d59addl$1, %eax 00001d62cmpl$31, %eax 00001d65jbe0x1d2c 00001d67movl-20(%ebp), %eax 00001d76addl$24, %esp 00001d79popl%ebp 00001d7aret 00001d43cmpl$0, %eax 00001d46setne%al 00001d49andb$1, %al 00001d4bmovzbl%al, %eax 00001d4emovl-20(%ebp), %edx 00001d51addl%edx, %eax 00001d53movl%eax, -20(%ebp) 00001d56movl-16(%ebp), %eax 00001d59addl$1, %eax 00001d62cmpl$31, %eax 00001d65jbe0x1d2c 00001d67movl-20(%ebp), %eax 00001d76addl$24, %esp 00001d79popl%ebp 00001d7aret _pcount_for: 00001d10pushl%ebp 00001d11movl%esp, %ebp 00001d13subl$24, %esp 00001d16movl8(%ebp), %eax 00001d19movl%eax, -4(%ebp) 00001d1cmovl$0, -20(%ebp) 00001d23movl$0, -16(%ebp) 00001d2ajmp0x1d5f 00001d2cmovl-16(%ebp), %eax 00001d2fmovl%eax, %ecx 00001d31movl$1, %eax 00001d36shll%cl, %eax 00001d38movl%eax, -24(%ebp) 00001d3bmovl-4(%ebp), %eax 00001d3emovl-24(%ebp), %edx 00001d41andl%edx, %eax _pcount_for: 00001d10pushl%ebp 00001d11movl%esp, %ebp 00001d13subl$24, %esp 00001d16movl8(%ebp), %eax 00001d19movl%eax, -4(%ebp) 00001d1cmovl$0, -20(%ebp) 00001d23movl$0, -16(%ebp) 00001d2ajmp0x1d5f 00001d2cmovl-16(%ebp), %eax 00001d2fmovl%eax, %ecx 00001d31movl$1, %eax 00001d36shll%cl, %eax 00001d38movl%eax, -24(%ebp) 00001d3bmovl-4(%ebp), %eax 00001d3emovl-24(%ebp), %edx 00001d41andl%edx, %eax

15 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