Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.

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

CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
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.
Assembly Language for Intel-Based Computers
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
1 Machine-Level Representation of Programs Ⅱ. 2 Outline Data movement Data manipulation Control structure Suggested reading –Chap 3.4, 3.5, 3.6.
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”
II:1 x86 Assembly - Control. II:2 Alternate reference source Go to the source: Intel 64 and IA32 
1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Machine-Level Programming II: Control : Introduction.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University Machine Level Programming.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
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.
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified.
Microprocessor MA Rahim Khan Computer Engineering and Networks Department.
תרגול 5 תכנות באסמבלי, המשך
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
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.
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.
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.
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.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements class06.ppt.
Machine-Level Programming 2 Control Flow
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Basic Assembly Language
Assembly IA-32.
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
Machine-Level Representation of Programs II
Machine-Level Programming II: Control Flow
x86-64 Programming II CSE 351 Autumn 2017
Assembly Language: IA-32 Instructions
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Representation of Programs III
Today Control flow if/while/do while/for/switch
Machine-Level Programming 2 Control Flow
Homework Reading Machine Projects Labs PAL, pp
Carnegie Mellon Ithaca College
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
Carnegie Mellon Ithaca College
CS201- Lecture 8 IA32 Flow Control
x86-64 Programming III CSE 351 Winter 2019
Credits and Disclaimers
Presentation transcript:

Assembly תרגול 7 תכנות באסמבלי, המשך

Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant only for the most recent operation leal does not alter any condition code In logical operations, CF and OF are set to 0 For shift operations, OF is set to 0, CF is the last shifted out bit

Setting Condition Codes

Setting Condition Codes (cont.) Explicit Setting by Compare Instruction cmpl Src2,Src1  cmpl 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  OF set if two’s complement overflow (a>0 && b 0 && (a-b)>0)

Setting Condition Codes (cont.) Explicit Setting by Test instruction testl Src2,Src1  Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask  testl b,a like computing a&b without setting destination  ZF set when a&b == 0  SF set when a&b < 0

Accessing the Condition Codes

Why does it work? Let’s take setl b,a for example  If there is no overflow and a≥b  SF=0, OF=1  If there is no overflow and a<b  SF=1, OF=0  If there is a negative overflow (a>b)  SF=1, OF=1  If there is a positive overflow (a<b)  SF=0, OF=1

a < b in assembly Translate the line: return (a<b); Suppose a is in %edx, b is in %eax: cmpl %eax,%edx # compare a to b setl %al # set %al to 0 or 1 movzbl %al,%eax # set %eax to 0 or 1

Jump Instructions

Unconditional Jump Direct jump jmp L1 Indirect jump  jmp *%eax  jmp *(%eax)

Conditional Jump Can’t use indirect jump Use it to implement  if conditions  loops  switch statements

Goto in C

If Condition in Assembly

Do-While Loops

Do-While Loops in Assembly

While Loops

Exercise InitiallyVariableRegister %eax %ebx %ecx %edx

Exercise’s Solution InitiallyVariableRegister aa%eax bb%ebx 0i%ecx aresult%edx

Exercise’s Solution Note the optimization done by the compiler!

For Loops

Exercise

Exercise’s Solution Note the optimization done by the compiler!

Switch Statements in C

Switch Statements in Assembly Building the jump table:

Switch Statements in Assembly

Exercise

Q & A Q: What were the values of the case labels in the switch statement body? What cases had multiple labels in the C code? A: The case labels had values -2,0,1,2,3,4 The case with the labels 2 and 3