Carnegie Mellon Ithaca College

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 17, 2006.
Flow Control Instructions
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
Conditional Processing If … then … else While … do; Repeat … until.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
תרגול 5 תכנות באסמבלי, המשך
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control Carnegie Mellon.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
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
Homework Reading Labs PAL, pp
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Architecture
Assembly IA-32.
Assembly Programming III CSE 410 Winter 2017
Assembly Programming III CSE 351 Spring 2017
More on logical instruction and
Assembly Language Programming Part 2
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II: Control
Machine-Level Representation of Programs II
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.
x86-64 Programming II CSE 351 Winter 2018
Assembly Programming III CSE 351 Spring 2017
Instructor: David Ferry
Machine-Level Programming: Control Flow
3.5 Conditional Codes Spring, 2015 Euiseong Seo
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
ECE 3430 – Intro to Microcomputer Systems
Homework Reading Machine Projects Labs PAL, pp
x86 Programming III CSE 351 Autumn 2016
Flow Control Instructions
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Summer 2018
x86-64 Programming III CSE 351 Autumn 2018
x86-64 Programming II CSE 351 Autumn 2018
Machine-Level Programming II: Control Flow
EECE.3170 Microprocessor Systems Design I
Assembly Language for Intel 8086 Jump Condition
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
x86-64 Programming II CSE 351 Winter 2019
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
Carnegie Mellon Ithaca College
Credits and Disclaimers
Computer Architecture and Assembly Language
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

Jumping A jump instruction causes execution to jump to a new address Really just puts the argument of the jump into the EIP Jump destinations are usually indicated by a label A label marks a place in code 1 xorq %rax, %rax 2 jmp .L1 3 movq (%rax), %rdx 4 .L1: popq %rdx The instruction jmp .L1 causes program to skip line 3 jmp is an unconditional jump instruction Note that we wouldn’t really use the code this way!

Jumping or A jump instruction has two types of arguments Direct jumps. Target is part of the instruction Use a label Indirect jumps. Target read from a register or memory. Use a ‘*’ followed by an operand specifier jmp *%eax or jmp *(%eax) Any addressing mode can be used Note that jmp is an unconditional jump instruction

Jumping Other jump instructions are conditional Either jump or continue executing at next instruction in the code Depends on some combination of the condition codes Conditional jumps can only be direct Assembler changes labels into actual memory addresses See section 3.6.3 of the book Not very important now; will be very important in chapter 7

Jumping jX Instructions Ithaca College Jumping jX Instructions Jump to different part of code depending on condition codes jX Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned)

Reading condition codes (cont) Assume a is in %rdx and b in %rax cmpq %rax, %rdx jl SUM Goal: jump if a is less than b First instruction sets the condition code. cmpq computes a – b a < b then a – b < 0 Second instruction checks to see if the compq proved that a – b < 0 Checks (SF^OF) Goal: jump if a < b or a – b < 0 jl LABEL ; if (SF^OF) %RIP  LABEL

Reading condition codes (cont) Assume a is in %rdx and b in %rax cmpq %rax, %rdx jl SUM How does this work? cmpq computes a – b If a < b then a – b < 0 If TRUE, then a – b will be negative AND there will be no overflow If there is positive overflow (a – b is large), we have a – b < 0 but OF is set If there is negative overflow (a – b is very small), we have a – b > 0 but OF is set In either case the sign flag will indicate the opposite of the sign of the true difference. Hence, use exclusive-or of the OF and SF jl LABEL ; if (SF^OF) %RIP  LABEL CF ZF SF OF Condition codes SF ^ OF

jl jump less (Signed) SF^OF cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds 50 %cl holds 20 50 – 20 00110010 + 11101100 = SF = OF = OF ^ SF is 50 < 20 Or 50 – 20 < 0 %al holds 50 %cl holds 70 50 – 70 00110010 + 10111010 = SF = OF = OF ^ SF is 50 < 70 Or 50 – 70 < 0 00011110 11101100 1 1 True False

jl jump less (Signed) SF^OF cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds –50 %cl holds –20 –50 – –20 11001110 + 00010100 = SF = OF = OF ^ SF is –50 < –20 Or 50 – 20 < 0 %al holds –20 %cl holds –50 –20 – –50 11101100 + 00110010 = SF = OF = OF ^ SF is 50 < 70 Or 50 – 70 < 0 11100010 00011110 1 1 False True

jl jump less (Signed) SF^OF cmpq %cl, %al Jl LABEL %cl and %al are 8 bits %al holds 127 %cl holds – 5 127– – 5 = 01111111 + 00000101 = SF = OF = OF ^ SF is 127< – 5 Or 127– – 5 < 0 %al holds – 128 %cl holds + 5 – 128– 5 = 10000000 + 11111011 = SF = OF = OF ^ SF is -128< 5 Or – 128– 5 < 0 10000100 01111011 1 1 1 underflow overflow 1 True False

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