Carnegie Mellon Ithaca College

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

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.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Controlling Program Flow. – 2 – Control Flow Computers execute instructions in sequence. Except when we change the flow of control Jump and Call instructions.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
X86_64 programming Tutorial #1 CPSC 261. X86_64 An extension of the IA32 (often called x86 – originated in the Intel 8086 processor) instruction set to.
תרגול 5 תכנות באסמבלי, המשך
CSE 351 x86 Calling Conventions, Control Flow, & Lab 2 April 23, 2015.
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.
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.
Carnegie Mellon Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Carnegie Mellon.
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
Machine-Level Programming I: Basics
CSCE 212 Computer Architecture
Homework Reading Labs PAL, pp
Assembly Language for Intel-Based Computers, 5th Edition
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
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.
Controlling Program Flow
x86-64 Programming II CSE 351 Winter 2018
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
Instructor: David Ferry
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Processor Architecture: The Y86-64 Instruction Set Architecture
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Processor Architecture: The Y86-64 Instruction Set Architecture
Machine-Level Programming 2 Control Flow
University of Gujrat Department of Computer Science
Homework Reading Machine Projects Labs PAL, pp
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
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
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 II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Get To Know Your Compiler
Machine-Level Programming III: Arithmetic Comp 21000: Introduction to Computer Organization & Systems March 2017 Systems book chapter 3* * Modified slides.
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Winter 2019
Carnegie Mellon Ithaca College
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
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
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 Loops Switch Statements

Processor State (x86-64, Partial) Ithaca College Processor State (x86-64, Partial) Information about currently executing program Temporary data ( %rax, … ) Location of runtime stack ( %rsp ) Location of current code control point ( %rip, … ) Status of recent tests ( CF, ZF, SF, OF ) Registers %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp Instruction pointer %rip Current stack top CF ZF SF OF Condition codes

Condition Codes (Implicit Setting) Ithaca College Condition Codes (Implicit Setting) Single bit registers CF Carry Flag (for unsigned) SF Sign Flag (for signed) ZF Zero Flag OF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmetic operations Example: addq Src,Dest ↔ t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) Not set by leaq instruction

Note that these flags are set just like we talked about in chapter 2! Condition Codes Examples: assume %rax contains 20 addl $10, %rax CF  0 ZF  0 SF  0 OF  0 subl $50, %rax CF  0 SF  1 Note that these flags are set just like we talked about in chapter 2!

Condition Codes Single Bit Registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Also implicitly Set By logical Operations xorl Src,Dest C analog: t = a ^ b (a = Src, b = Dest) CF set to 0 ZF set if t == 0 SF set if t < 0 OF set to 0 For shift operations: CF set to last bit shifted out

Condition Codes (Implicit Setting) There are no Boolean types in assembly language. There is no concept of Boolean operators in assembly language. So what is there? Bits All we can do is look at bits. What those bits mean is determined by the context of the program. Instructions set flags. Other instructions take action based on the bits in those flags. Don’t think about it as comparison as in the C language sense.

Condition Codes (Explicit Setting: Compare) Ithaca College Condition Codes (Explicit Setting: Compare) Explicit Setting by Compare Instruction cmpq Src2, Src1 cmpq b,a like computing a-b without setting destination CF set if carry out from most significant bit (used for unsigned comparisons) 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) || (a<0 && b>0 && (a-b)>0) Note: notice that the operands are in reverse order; b is the first operand , but we compute a – b! Note 2: there are also cmpw and cmpb instructions.

Comparison and Boolean In C if (a < b ) Which is the same as if (a – b < 0 ) a – b < 0 creates a Boolean value In assembly cmpq %rsi, %rdi Assuming that %rdi contains a and %rsi contains b Doesn’t create a Boolean value Does set condition codes ** we’ll talk about the if part later!

Example %rax 0000 %rcx 0010 %rdx %rbx %rsi %rdi %rsp %rbp cmpq %rax, %rcx execute %rcx - %rax then set condition codes NOTHING SAVED! Condition codes CF ZF SF OF ?? ?? ?? ??

Example %rax 0000 %rcx 0010 %rdx %rbx %rsi %rdi %rsp %rbp cmpq %rax, %rcx execute %rcx - %rax then set condition codes NOTHING SAVED! Condition codes CF ZF SF OF

Condition Codes (Explicit Setting: Test) Explicit Setting by Test instruction testl/testq Src2, Src1 testl b,a like computing a&b without setting destination Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask CF set to 0 ZF set when a&b == 0 SF set when a&b < 0 OF set to 0 Note: typically the same operand is repeated to test whether it is negative, zero, or positive: testl %eax, %eax sets the condition codes depending on the value in %eax Note 2: there are also testw and testb instructions. Remember that bitwise operations are more efficient than arithmetic