Logical and Bit Operations Chapter 9 S. Dandamudi.

Slides:



Advertisements
Similar presentations
Machine Instructions Operations
Advertisements

Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
1 IKI10230 Pengantar Organisasi Komputer Kuliah no. 05.c: Logical Operations Sumber: 1. Paul Carter, PC Assembly Language 2. Hamacher. Computer Organization,
Computer Organization & Assembly Language
80x86 Instruction Set Dr. Qiang Lin.
Introduction to Assembly Here we have a brief introduction to IBM PC Assembly Language –CISC instruction set –Special purpose register set –8 and 16 bit.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
Logical, Shift, and Rotate Operations CS208. Logical, Shift and Rotate Operations  A particular bit, or set of bits, within the byte can be set to 1.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers
Logical and Shift operations A particular bit, or set of bits, within the byte is set to 1 or 0 depending on conditions encountered during the execution.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Shift and Rotate Instructions
Chapter 4 Basic Instructions. 4.1 Copying Data mov Instructions mov (“move”) instructions are really copy instructions, like simple assignment statements.
Binary Operations Math/Logical. Binary Math Decimal Addition Example ) Add = 15 Write down 5, carry ) Add 3 +
Introduction to Computer Engineering by Richard E. Haskell Shift and Rotate Instructions Module M16.2 Section 10.3.
Unit-1 PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE Advance Processor.
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Types of Registers (8086 Microprocessor Based)
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
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.
Overview of Assembly Language Chapter 4 S. Dandamudi.
Boolean and Comparison Instructions Operation Description ANDAND Destination, Source OROR Destination, Source XORXOR Destination, Source NOTNOT Destination.
Department of Computer Science and Software Engineering
Copyright 2000ELEC 242 Arithmetic Instructions1 Arithmetic Instructions Arithmetic and logical instructions modify the contents of the Flag (Status) register.
Arithmetic Flags and Instructions
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Arithmetic and Logic Instructions
EEL 3801 Part V Conditional Processing. This section explains how to implement conditional processing in Assembly Language for the 8086/8088 processors.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
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.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Arithmetic Flags and Instructions Chapter 7 S. Dandamudi.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Microprocessor & Assembly Language
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Chapter four – The 80x86 Instruction Set Principles of Microcomputers 2016年3月17日 2016年3月17日 2016年3月17日 2016年3月17日 2016年3月17日 2016年3月17日 1 Chapter Four.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Bitwise and Logical Manipulations Assembly Language Programming University of Akron Dr. Tim Margush.
Boolean, Shift and Rotate instructions Dr.Hadi AL Saadi.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
Microprocessor Systems Design I
Microprocessor Systems Design I
Chapter 3 Bit Operations
Microprocessor Systems Design I
EE3541 Introduction to Microprocessors
Instruction System - Bit Manipulation Instruction
Machine control instruction
More on logical instruction and
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
UNIT: 2 INSTRUCTION SET OF 8086.
Shift & Rotate Instructions)
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Shift, Multiply, and Divide
Chapter 5 Arithmetic and Logic Instructions
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Shift and Rotate Instructions.
CS-401 Computer Architecture & Assembly Language Programming
Presentation transcript:

Logical and Bit Operations Chapter 9 S. Dandamudi

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 2 Outline Logical instructions  AND  OR  XOR  NOT  TEST Shift instructions  Logical shift instructions  Arithmetic shift instructions Rotate instructions  Rotate without carry  Rotate through carry Logical expressions in high-level languages  Representation of Boolean data  Logical expressions  Bit manipulation  Evaluation of logical expressions Bit instructions  Bit test and modify instructions  Bit scan instructions Illustrative examples

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 3 Logical Instructions Logical instructions operate on bit-by-bit basis Five logical instructions:  AND  OR  XOR  NOT  TEST All logical instructions affect the status flags

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 4 Logical Instructions (cont’d) Since logical instructions operate on a bit-by-bit basis, no carry or overflow is generated Logical instructions  Clear carry flag (CF) and overflow flag (OF)  AF is undefined Remaining three flags record useful information  Zero flag  Sign flag  Parity flag

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 5 Logical Instructions (cont’d) AND instruction Format and destination,source Usage  To support compound logical expressions and bitwise AND operation of HLLs  To clear one or more bits of a byte, word, or doubleword  To isolate one or more bits of a byte, word, or doubleword

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 6 Logical Instructions (cont’d) OR instruction Format or destination,source Usage  To support compound logical expressions and bitwise OR operation of HLLs  To set one or more bits of a byte, word, or doubleword  To paste one or more bits of a byte, word, or doubleword

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 7 Logical Instructions (cont’d) XOR instruction Format xor destination,source Usage  To support compound logical expressions of HLLs  To toggle one or more bits of a byte, word, or doubleword  To initialize registers to zero »Example: xor AX,AX

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 8 Logical Instructions (cont’d) NOT instruction Format not destination Usage  To support logical expressions of HLLs  To complement bits »Example: 2’s complement of an 8-bit number not AL inc AL

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 9 Logical Instructions (cont’d) TEST instruction Format test destination,source  TEST is a non-destructive AND operation »Result is not written in destination »Similar in spirit to cmp instruction Usage  To test bits »Example: test AL,1 jz even_number ; else odd number

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 10 Shift Instructions Two types of shift instructions  Logical shift instructions »shl (SHift Left) »shr (SHift Right) »Another interpretation: –Logical shift instructions work on unsigned binary numbers  Arithmetic shift instructions »sal (Shift Arithmetic Left) »sar (Shift Arithmetic Right) »Another interpretation: –Arithmetic shift instructions work on signed binary numbers

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 11 Shift Instructions (cont’d) Effect on flags  Auxiliary flag (AF): undefined  Zero flag (ZF) and parity flag (PF) are updated to reflect the result  Carry flag »Contains the last bit shifted out  Overflow flag »For multibit shifts –Undefined »For single bit shifts –OF is set if the sign bit has changed as a result of the shift –Cleared otherwise

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 12 Logical Shift Instructions General format shl destination,count shr destination,count destination can be an 8-, 16-, or 32-bit operand located either in a register or memory

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 13 Logical Shift Instructions (cont’d) Two versions shl/shr destination,count shl/shr destination,CL  First format directly specifies the count value »Count value should be between 0 and 31 »If a greater value is specified, Pentium takes only the least significant 5 bits as the count value  Second format specifies count indirectly through CL »CL contents are not changed »Useful if count value is known only at the run time as opposed at assembly time –Ex: Count is received as an argument in a procedure call

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 14 Logical Shift Instructions (cont’d) Usage  Bit manipulation ; AL contains the byte to be encrypted mov AH,AL shl AL,4 ; move lower nibble to upper shr AH,4 ; move upper nibble to lower or AL,AH ; paste them together ; AL has the encrypted byte  Multiplication and division »Useful to multiply (left shift) or divide (right shift) by a power of 2 »More efficient than using multiply/divide instructions

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 15 Arithmetic Shift Instructions Two versions as in logical shift sal/sar destination,count sal/sar destination,CL

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 16 Double Shift Instructions Double shift instructions work on either 32- or 64- bit operands Format  Takes three operands shld dest,src,count ; left-shift shrd dest,src,count ; right-shift  dest can be in memory or register  src must be a register  count can be an immediate value or in CL as in other shift instructions

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 17 Double Shift Instructions (cont’d) src is not modified by the doubleshift instruction Only dest is modified Shifted out bit goes into the carry flag

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 18 Rotate Instructions A problem with the shift instructions  Shifted out bits are lost  Rotate instructions feed them back Two types of rotate instructions  Rotate without carry »Carry flag is not involved in the rotate process  Rotate through carry »Rotation involves the carry flag

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 19 Rotate Without Carry General format rol destination,count ror destination,count count can be an immediate value or in CL (as in shift)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 20 Rotate Through Carry General format rcl destination,count rcr destination,count count can be an immediate value or in CL (as in shift)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 21 Rotate Through Carry (cont’d) Only two instructions that take CF into account »This feature is useful in multiword shifts Example: Shifting 64-bit number in EDX:EAX  Rotate version mov ECX,4 ; 4 bit shift shift_left: shl EAX,1 ; moves leftmost bit of EAX to CF rcl EDX,1 ; CF goes to rightmost bit of EDX loop shift_left  Double shift version shld EDX,EAX,4 ; EAX is unaffected by shld shl EAX,4

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 22 Logical Expressions in HLLs Representation of Boolean data  Only a single bit is needed to represent Boolean data  Usually a single byte is used »For example, in C –All zero bits represents false –A non-zero value represents true Logical expressions  Logical instructions AND, OR, etc. are used Bit manipulation  Logical, shift, and rotate instructions are used

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 23 Evaluation of Logical Expressions Two basic ways  Full evaluation »Entire expression is evaluated before assigning a value »PASCAL uses full evaluation  Partial evaluation »Assigns as soon as the final outcome is known without blindly evaluating the entire logical expression »Two rules help: –cond1 AND cond2  If cond1 is false, no need to evaluate cond2 –cond1 OR cond2  If cond1 is true, no need to evaluate cond2

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 24 Evaluation of Logical Expressions (cont’d) Partial evaluation  Used by C Useful in certain cases to avoid run-time errors Example if ((X > 0) AND (Y/X > 100))  If x is 0, full evaluation results in divide error  Partial evaluation will not evaluate (Y/X > 100) if X = 0 Partial evaluation is used to test if a pointer value is NULL before accessing the data it points to

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 25 Bit Instructions Bit Test and Modify Instructions  Four bit test instructions  Each takes the position of the bit to be tested InstructionEffect on the selected bit bt (Bit Test)No effect bts (Bit Test and Set)selected bit  1 btr (Bit Test and Reset)selected bit  0 btc selected bit  NOT(selected bit) (Bit Test and Complement)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 26 Bit Instructions (cont’d) All four instructions have the same format We use bt to illustrate the format bt operand,bit_pos  operand is word or doubleword »Can be in memory or a register  bit_pos indicates the position of the bit to be tested »Can be an immediate value or in a 16- or 32-bit register Instructions in this group affect only the carry flag »Other five flags are undefined following a bit test instruction

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 27 Bit Scan Instructions These instructions scan the operand for a 1 bit and return the bit position in a register Two instructions bsf dest_reg,operand ;bit scan forward bsr dest_reg,operand ;bit scan reverse »operand can be a word or doubleword in a register or memory »dest_reg receives the bit position –Must be a 16- or 32-bit register  Only ZF is updated (other five flags undefined) –ZF = 1 if all bits of operand are 0 –ZF = 0 otherwise (position of first 1 bit in dest_reg )

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 9: Page 28 Illustrative Examples Example 1  Multiplication using shift and add operations »Multiplies two unsigned 8-bit numbers –Uses a loop that iterates 8 times Example 2  Same as Example 1 (efficient version) »We loop only for the number of 1 bits –Uses bit test instructions Example 3  Conversion of octal to binary Last slide