Carnegie Mellon Ithaca College

Slides:



Advertisements
Similar presentations
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
Advertisements

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.
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.
Computer Architecture
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.
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.
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
Credits and Disclaimers
Machine-Level Programming I: Basics
CSCE 212 Computer Architecture
Conditional Branch Example
Computer Architecture
Assembly Programming III CSE 410 Winter 2017
Assembly Programming III CSE 351 Spring 2017
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming III: Procedures
Machine-Level Programming II: Control
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:
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Assembly Programming III CSE 351 Spring 2017
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Carnegie Mellon Wrap-up of Machine-Level Programming II: Control : Introduction to Computer Systems Sept. 18, 2018.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
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 Representation of Programs III
Machine-Level Programming 2 Control Flow
x86 Programming III CSE 351 Autumn 2016
Carnegie Mellon Ithaca College
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
x86-64 Programming II CSE 351 Summer 2018
x86-64 Programming III CSE 351 Autumn 2018
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
x86-64 Programming II CSE 351 Autumn 2018
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.
Carnegie Mellon Ithaca College
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
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Credits and Disclaimers
Credits and Disclaimers
Credits and Disclaimers
Presentation transcript:

Carnegie Mellon Ithaca College Machine-Level Programming IV Control Comp 21000: Introduction to Computer Organization & Systems Systems book chapter 3*

Today Control: Condition codes Conditional branches Loops Ithaca College Today Control: Condition codes Conditional branches If-then-else statements Conditional moves Loops Switch Statements

Conditional Branch Example (Old Style) Ithaca College Conditional Branch Example (Old Style) Generation barr> gcc –Og -S –fno-if-conversion control.c absdiff: cmpq %rsi, %rdi # x:y jle .L4 movq %rdi, %rax subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax 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

Expressing with Goto Code Ithaca College Expressing with Goto Code 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; } Generally considered bad coding style

Expressing with Goto Code Ithaca College Expressing with Goto Code C allows goto statement Jump to position designated by label absdiff: cmpq %rsi, %rdi # x:y jle .L4 movq %rdi, %rax subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax 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; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value

Expressing with Goto Code Ithaca College Expressing with Goto Code C allows goto statement Jump to position designated by label absdiff: cmpq %rsi, %rdi # x:y jle .L4 movq %rdi, %rax subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax 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; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value

Expressing with Goto Code Ithaca College Expressing with Goto Code C allows goto statement Jump to position designated by label absdiff: cmpq %rsi, %rdi # x:y jle .L4 movq %rdi, %rax subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax 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; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value

Expressing with Goto Code Ithaca College Expressing with Goto Code C allows goto statement Jump to position designated by label absdiff: cmpq %rsi, %rdi # x:y jle .L4 movq %rdi, %rax subq %rsi, %rax ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax 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; } Register Use(s) %rdi Argument x %rsi Argument y %rax Return value

Example 2 Copy ifStmt.c from home/barr/Student/comp210/examples/ gcc –O0 –o ifStmt –fno-if-conversion ifStmt.c gdb ifStmt In gdb: disass testIf1 int testIf1(int x, int y) { int result; int ntest = (x >= y); if (ntest) goto else; result = x * 4; goto Done; else: result = y * 4; Done: return result; } int testIf1(int x, int y) { int result; if (x < y) result = x * 4; else result = y * 4; return result; }

Example 2 continued… Register Use(s) %rdi Argument x %rsi Argument y %rax Return value pushq %rbp movq %rsp, %rbp movq %rdi, -18(%rbp) movq %rsi, -20(%rbp) movq -18(%rbp), %rax cmpq -20(%rbp), %rax jge LBBO_2 shlq $2, %rax movq %rax, -8(%rbp) jmp LBB0_3 LBB0_2: movq -20(%rbp), %rax LBB0_3: movq -8(%rbp), %rax popq %rbp retq int testIf1 (int x, int y) { int result; int ntest = (x >= y); if (ntest) goto else; result = x * 4; goto Done; else: result = y * 4; Done: return result; } LBBO_2 is 0x40054a <testIf1+36> LBBO_3 is 0x400556 <testIf1+48>

Setting up the runtime stack Example 2 continued… Register Use(s) %rdi Argument x %rsi Argument y %rax Return value pushq %rbp movq %rsp, %rbp movq %rdi, -18(%rbp) movq %rsi, -20(%rbp) movq -18(%rbp), %rax cmpq -20(%rbp), %rax jge LBBO_2 shlq $2, %rax movq %rax, -8(%rbp) jmp LBB0_3 LBB0_2: movq -20(%rbp), %rax LBB0_3: movq -8(%rbp), %rax popq %rbp retq Setting up the runtime stack int testIf1 (int x, int y) { int result; int ntest = (x >= y); if (ntest) goto else; result = x * 4; goto Done; else: result = y * 4; Done: return result; } if x - y >= 0 jump calculate x=x*4 and jump Calculate y=y*4 LBBO_2 is 0x40054a <testIf1+36> LBBO_3 is 0x400556 <testIf1+48> Set up the return value

General Conditional Expression Translation (Using Branches) Ithaca College General Conditional Expression Translation (Using Branches) C Code val = Test ? Then_Expr : Else_Expr; val = x>y ? x-y : y-x; Test Is an expression returning an integer = 0 interpreted as false ≠ 0 interpreted as true Create separate code regions for then & else expressions Execute appropriate one Goto Version ntest = !Test; if (ntest) goto Else; val = Then_Expr; goto Done; Else: val = Else_Expr; Done: . . .