Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.

Slides:



Advertisements
Similar presentations
Number Bases Informatics INFO I101 February 9, 2004 John C. Paolillo, Instructor.
Advertisements

Computer Organization & Assembly Language
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
Table 1. Software Hierarchy Levels.. Essential Tools An assembler is a program that converts source-code programs into a machine language (object file).
CS2422 Assembly Language & System Programming September 28, 2006.
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Ch. 5 from Yu & Marut. Registers 14(16-bit) registers: 1.Data reg. – to hold data for an op. 2.Address reg – to hold addr of an instruction or data.
Arithmetic for Computers
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
CSC 221 Computer Organization and Assembly Language
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
11.1/36 Repeat: From Bits and Pieces Till Strings.
Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
LAB Flag Bits and Register
ICS312 Set 9 Logic & Shift Instructions. Logic & Shift Instructions Logic and Shift Instructions can be used to change the bit values in an operand. The.
Lecture 4 ( Assembly Language).
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Boolean and Comparison Instructions Operation Description ANDAND Destination, Source OROR Destination, Source XORXOR Destination, Source NOTNOT Destination.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Sahar Mosleh California State University San MarcosPage 1 Review.
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Arithmetic Flags and Instructions
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Logic Conditional Processing. Status flags - review The Zero flag is set when the result of an operation equals zero. The Carry flag is set when an instruction.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 4: Data Transfers, Addressing, and Arithmetic Lecture 15: ADD, SUB, NEG and how they.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Sahar Mosleh California State University San MarcosPage 1 Assembly language and Digital Circuit By Sahar Mosleh California State University San Marcos.
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
Sahar Mosleh California State University San MarcosPage 1 Finite State Machine.
3.4 Addressing modes Specify the operand to be used. To generate an address, a segment register is used also. Immediate addressing: the operand is a number.
CHAPTER 6 ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS.
Introduction to Computer Organization and Assembly Language
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Comparison Instructions Test instruction –Performs an implied AND operation between each of the bits in 2 operands. Neither operand is modified. (Flags.
Microprocessor & Assembly Language
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
Multiplication and Division instructions Dr.Hadi AL Saadi.
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
Data Transfers, Addressing, and Arithmetic
ICS312 SET 7 Flags.
The FLAGS Register An x bit means an unidentified value 9/12/2018
Assembly Language for Intel-Based Computers, 5th Edition
More on logical instruction and
University of Gujrat Department of Computer Science
4.2 Arithmetic Instructions
Introduction to Assembly Language
Lecture 4 ( Assembly Language).
Data Transfers, Addressing, and Arithmetic
Shift & Rotate Instructions)
University of Gujrat Department of Computer Science
Shift & Rotate Instructions)
Chapter 5 Arithmetic and Logic Instructions
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Number Systems and Circuits for Addition
Shift and Rotate Instructions.
Ch. 5 – Intel 8086 – study details from Yu & Marut
Part IV The FLAGS Register
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions

Sahar Mosleh California State University San MarcosPage 2 The CPU flags Each instruction affects the CPU flags. The zero flag is set when the result of an operation equal zero. The carry flag is set when an instruction generates a result that is too large (or too small) for the destination operand. The sign flag is a copy of the high bit of the destination operand indicating that it is negative if set and positive if clear. The overflow flag is set when an instruction generates an invalid signed result. The parity flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand

Sahar Mosleh California State University San MarcosPage 3 Zero and Sign Flags: The zero flag is set when the destination operand of an arithmetic instruction is assigned a value of zero. example: mov cx,1 sub cx,1; cx = 0, ZF = 1 mov ax, 0FFFFh Inc ax; ax = 0, ZF = 1 Inc ax; ax = 1, ZF = 0 The sign flag is set when the result of an arithmetic operation is negative

Sahar Mosleh California State University San MarcosPage 4 Example: mov cx,0 sub cx,1; CX = -1, SF=1 add cx,2; CX= 1, SF = 0 Carry Flag: Carry flag is significant only when the CPU performs unsigned arithmetic. The result of an unsigned addition operation is too large (or too small) for the destination operand, the carry flag is set. Example: mov al, 0FFh add al,1; al = 00,CF= 1

Sahar Mosleh California State University San MarcosPage 5 The following diagram shows what happens at the bit level On the other hand, if we add 1 to 00FFh in ax, the sum easily fits into 16 bits and the carry flag is cleared Carry:

Sahar Mosleh California State University San MarcosPage 6 mov ax, 00FFh add ax,1 ;CF = 0, ax = 0100h If we add 1 to FFFFh in the ax register, a carry is generated out of the high bit of ax. mov ax, 0FFFFh add ax1; CF = 1, ax = 0000h If we subtract large unsigned integer from a smaller one, the carry flag is set and the value in al is invalid. mov al, 1 sub al, 2; CF = 1

Sahar Mosleh California State University San MarcosPage 7 The Overflow flag: The overflow flag is relevant only when performing signed arithmetic. It is set when an arithmetic operation generates a signed result that can not fit in the destination operand. Example: mov al, +127 add al, 1; OF = 1 Similarly, mov al, -128 sub al, 1; OF = 1

Sahar Mosleh California State University San MarcosPage 8 Example: When adding the binary values and , there is no carry from bit 6 to bit 7 but there is a carry from bit 7 into the carry flag Overflow has occurred as displayed in the following   CF = 1 Overflow has occurred if: Two positive operands were added and their sum is negative Two negative operands were added and their sum is positive Overflow never occurs, when the sign of two addition operands are different.

Sahar Mosleh California State University San MarcosPage 9 Neg Instruction and flag This can produce an invalid result if the destination operand can not be stored correctly Example, if we move -128 to al and try to negate it, the value can not stored in al. This causes the overflow flag to be set, and an invalid value to be moved to al mov al, -128; al = b neg al; al = b, OF=1 On the other hand 1f +127 is negated, the result is valid and the overflow flag is clear. moval, +127; al = b neg al; al = b, OF = 0

Sahar Mosleh California State University San MarcosPage 10 Boolean and comparison instruction We are going to begin the study of the conditional processing by working at the binary level, using the four basic operations from the boolean algebra AND, OR, XOR, and NOT. These operations are used in the design of the computer hardware and software. The IA-32 instruction set contains the AND, OR, XOR, NOT, TEST, and BTop instruction which directly implement boolean operations between bytes, words and doublewords.

Sahar Mosleh California State University San MarcosPage 11 OperationDescription ANDBoolean AND operation between a source operand and the destination operand ORBoolean OR operation between a source operand and the destination operand XORBoolean XOR operation between a source operand and the destination operand NOTBoolean NOT operation on a destination operand TESTImplies boolean AND operation between a source and destination operand, setting the CPU flags appropriately BT, BTC, BTR, BTSCopy bit n from the source operand to the carry flag and complement/reset/set the same bit in the destination operand

Sahar Mosleh California State University San MarcosPage 12 The AND instruction performs a boolean (bitwise) AND operation between each pair of matching bits in 2 operands and place the result in the destination operand AND destination, source The following operand combination are permitted AND reg, reg AND reg, mem AND reg, imm AND mem, reg AND mem, imm

Sahar Mosleh California State University San MarcosPage 13 The operand can be 8, 16, or 32 bits, and they must be the same size. The following truth table labels the input bits x and y. The third column shows the value of expression x AND y XYX AND Y

Sahar Mosleh California State University San MarcosPage 14 The AND instruction is often used to clear selected bits and preserve others. In the following example, the upper 4 bits are cleared and the lower 4 bits are unchanged mova1, b AND anda1, b The lower 4 bits might contain useful information while we don’t care about the upper 4 bits This technique is bit extraction because the lower 4 bits are pulled from AL The AND instruction always clears the overflow and carry flag. It modifies the sign, zero, parity flag according to the value of the destination operand

Sahar Mosleh California State University San MarcosPage 15 Example: Converting characters to upper case: The AND instruction provides an easy way to translate a letter from a lower case to upper case. If we compare the ASCII code for A and a, it becomes clear that only bit 5 is different = 61h (‘a’) = 41h (‘A’) The rest of the alphabetic characters have the same relationship. If we AND any character with binary, all bits are unchanged except for the bit 5 which is clear.

Sahar Mosleh California State University San MarcosPage 16 The OR instruction performs a boolean (bitwise) OR operation between each pair of matching bits in 2 operands and place the result in the destination operand. OR destination, source The following operand combination are permitted. OR reg, reg OR reg, mem OR reg, imm OR mem, reg OR mem, imm

Sahar Mosleh California State University San MarcosPage 17 The operand can be 8, 16, or 32 bits, and they must be the same size. The following truth table labels the input bits x and y. The third column shows the value of expression x OR y XYX OR Y

Sahar Mosleh California State University San MarcosPage 18 The OR instruction is often used to set selected bits and preserve others. In the following example, 3Bh is ORed with 0Fh. The lower 4 bits of the result are set and the high 4 bits are unchanged OR The OR instruction can be used to convert a byte containing an integer between 0 and 9 into an ASCII digit. To do this, you must set bits 4 and 5 if for example AL=05h, you can OR it with 30h to convert it to ASCII code for the digit 5 (35h)

Sahar Mosleh California State University San MarcosPage 19 Example: h OR h h, ‘5’ The assembly language instruction to do this are as follows: mov d1, 5; binary value or d1, 30h; convert to ASCII Flags: The OR instruction always clears the carry and overflow flags. It modifies the sign, zero, and parity flag according to the value of the destination operand

Sahar Mosleh California State University San MarcosPage 20 The XOR instruction performs a boolean (bitwise) XOR operation between each pair of matching bits in 2 operands and place the result in the destination operand. XOR destination, source The following operand combination are permitted. XOR reg, reg XOR reg, mem XOR reg, imm XOR mem, reg XOR mem, imm

Sahar Mosleh California State University San MarcosPage 21 The operand can be 8, 16, or 32 bits, and they must be the same size. The following truth table labels the input bits x and y. The third column shows the value of expression x XOR y XY X  Y

Sahar Mosleh California State University San MarcosPage 22 Special Quality for XOR is that it reverses itself when applied twice to the same operand X  Y (X + Y) + YYX This reversible property of XOR makes it ideal tool for a simple form of data encryption Flag: The XOR instruction always clears the overflow and carry flags. It modifies the sign, zero, and parity flags according to the value of the destination operand