1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.

Slides:



Advertisements
Similar presentations
BINARY & HEX I/O. Binary input : read in a binary number from keyboard, followed by a carriage return. character strings of 1’s & 0’ we need to convert.
Advertisements

Integer Arithmetic: Multiply, Divide, and Bitwise Operations
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,
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
1 Multiplication, Division, and Numerical Conversions Chapter 6.
80x86 Instruction Set Dr. Qiang Lin.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
8086 : INSTRUCTION SET By, Pramod Sunagar Assistant Professor
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
Flow Control 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
Assembly Language for Intel-Based Computers
Assembly Language for x86 Processors 6th Edition
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
11.1/36 Repeat: From Bits and Pieces Till Strings.
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.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Overview of Assembly Language Chapter 4 S. Dandamudi.
Department of Computer Science and Software Engineering
Arithmetic Flags and Instructions
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
Logical and Bit Operations Chapter 9 S. Dandamudi.
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.
Chapter 7: Integer Arithmetic. 2 Chapter Overview Shift and Rotate Instructions Shift and Rotate Applications Multiplication and Division Instructions.
Lecture 12 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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.
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.
1 Chapter 6 Conditional Processing Assembly Language for Intel-Based Computers, 3rd edition Kip R. Irvine.
CS2422 Assembly Language and System Programming 0 Week 9 Data Transfers, Addressing, and Arithmetic.
Computer Architecture and Assembly Language
Data Transfers, Addressing, and Arithmetic
Microprocessor Systems Design I
ICS312 SET 7 Flags.
Chapter 3 Bit Operations
The FLAGS Register An x bit means an unidentified value 9/12/2018
EE3541 Introduction to Microprocessors
Instruction System - Bit Manipulation Instruction
Machine control instruction
Assembly Language Programming Part 2
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
Arithmetic and Logic Chapter 5
Data Transfers, Addressing, and Arithmetic
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Shift, Multiply, and Divide
Arithmetic and Logic Chapter 5
Chapter 5 Arithmetic and Logic Instructions
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Shift and Rotate Instructions.
Ch. 5 – Intel 8086 – study details from Yu & Marut
Presentation transcript:

1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook

2 Logic Instructions  Syntax for AND, OR, XOR, and TEST instructions: op-code destination, source  They perform the Boolean bitwise operation and store the result into destination.  TEST is just an AND but the result is not stored. TEST affects the flags just like AND does.  Both operands must be of the same type  either byte, word or dword  Both operands cannot be mem  again: mem to mem operations are forbidden  They clear (ie: put to zero) CF and OF  They affect SF and ZF according to the result of the operation (as usual)

3 Logic Instructions (cont.)  The source is often an imm operand called a bit mask: used to fix certain bits to 0 or 1  To clear a bit we use an AND since:  0 AND b = 0 (b is cleared)  1 AND b = b (b is conserved)  Ex: to clear the sign bit of AL without affecting the others, we do: AND al,7Fh ;msb of AL is cleared  since 7Fh = b

4 Logic Instructions (cont.)  To set (i.e: fix to 1) certain bits, we use OR:  1 OR b = 1 (b is set)  0 OR b = b (b is conserved)  To set the sign bit of AH, we do: OR ah,80h  To test if ECX=0 we can do: OR ecx,ecx  since this does not change the number in ECX and set ZF=1 if and only if ECX=0

5 Logic Instructions (cont.)  XOR can be used to inverse certain bits:  b XOR 1 = NOT(b) (b is complemented)  b XOR 0 = b (b is conserved)  Ex: to initialize a register to 0 we can use: XOR ax,ax  Since b XOR b = 0 (b is cleared)  This instruction uses only 2 bytes of space.  The next instruction uses 3 bytes of space: MOV ax,0  Compilers prefer the XOR method

6 Logic Instructions (cont.)*  To convert from upper case letter to lower case we can use the usual method: ADD dl,20h  But 20h = b and bit #5 is always 0 for chars from ‘A’ (41h) to ‘Z’ (5Ah). Uppercase (41h-5Ah) A-ZLowercase (61h-Ah) a-z 4X X6X X 5X X 7X X  Hence, adding 20h gives the same result as setting this bit #5 to 1. Thus: OR dl,20h ;converts from upper to lower case AND dl,0DFh;converts from lower to upper case  since DFh = b

7 Logic Instructions (cont.)  To invert all the bits (ones complement), we use: NOT destination  does not affect any flag and destination cannot be an imm operand  Recall that to perform twos complement, we use NEG destination  affect SF and ZF according to result  CF is set to 1 unless the result is 0  OF=1 iff there is a signed overflow

8 Exercise 1  Use only one instruction among AND, OR, XOR, and TEST to do the following task:  (A) Convert the ASCII code of a decimal digit ('0‘ to '9‘) contained in AL to its numerical value.  (B) Fix to 1 the odd numbered bits in EAX (ie: the bits numbered 1, 3, 5…) without changing the even numbered bits.  (C) Clear to 0 the most significant bit and the least significant bit of BH without changing the other bits.  (D) Inverse the least significant bit of EBX without changing the other bits.

9 Shifting Bits to the Left  To shift 1 bit to the left we use: SHL dest,1  each bit is shifted one position to the left  the lsb (least significant bit) is filled with 0  the msb (most significant bit) is moved into CF (so the previous content of CF is lost)  dest can be either byte, word or dword  Example: mov bx, 80h ; BX = 0080h shl bl, 1 ; BX = 0000h, CF=1 (only BL is affected)

10 Shifting Multiple Times to the Left  Two forms are permitted: SHL dest, CL ; CL = number of shifts SHL dest, imm8  SHL affects SF and ZF according to the result  CF contains the last bit shifted out mov bh, 82h ;BH = b shl bh, 2 ;BH = b, CF=0  Effect on OF for all shift and rotate instructions (left and right):  For any single-bit shift/rotate: OF=1 iff the shift or rotate changes the sign bit  For multiple-bit shift/rotate: the effect on OF is undefined  Hence, sign overflows are signaled only for single-bit shifts and rotates

11 Fast Multiplication  Each left shift multiplies by 2 the operand for both signed and unsigned interpretations. Ex: mov ax, 4 ;AX = 0004h mov bx, -1 ;BX = FFFFh shl ax, 2 ;AX = 0010h = 16 shl bx, 3 ;BX = FFF8h = -8  Multiplication by shifting is very fast. Try to factor your multiplier into powers of 2:  BX * 36 = BX * (32 + 4) = BX*32 + BX*4  So add (BX shifted by 5) to (BX shifted by 2)

12 Shifting bits to the right  To shift to the right use either: SHR dest, CL ;value of CL = number of shifts SHR dest, imm8  the msb of dest is filled with 0  the lsb of dest is moved into CF  Each single-bit right shift divides the unsigned value by 2. Ex: mov bh,13 ;BH = b = 13 shr bh,2 ;BH = b = 3 (div by 4),CF=0  (the remainder of the division is lost)

13 Arithmetic Shift SAR  Is needed to divide the signed value by 2: SAR dest, CL ;value of CL = number of shifts SAR dest, imm8  the msb of dest is filled with its previous value (so the sign is preserved)  the lsb of dest is moved into CF mov ah, -15 ;AH = b sar ah, 1 ;AH = b = -8  the result is rounded to the smallest integer (-8 instead of -7…)  in contrast: shr ah, 1 ;gives ah = b = 78h

14 Rotate (without the CF)  ROL rotates the bits to the left (same syntax)  CF gets a copy of the msb  ROR rotates the bits to the right (same syntax)  CF gets a copy of the lsb  CF reflect the action of the last rotate

15 Examples of ROL mov ah,40h ;ah = b rol ah,1 ;ah = b, CF = 0 rol ah,1 ;ah = b, CF = 1 rol ah,1 ;ah = b, CF = 0 mov ax,1234h ;ax = b rol ax,4 ;ax = 2341h rol ax,4 ;ax = 3412h rol ax,4 ;ax = 4123h

16 Rotate with CF  RCL rotates to the left with participation of CF  RCR rotates to the right with participation of CF

17 Ex: inverting the content of AL*  Ex: whenever AL = b we want to have AL = b mov ecx, 8;number of bits to rotate start: shl al, 1;CF = msb of AL rcr bl, 1;push CF into msb of BL loop start;repeat for 8 bits mov al, bl;store result into AL

18 Exercise 2  Give the binary content of AX immediately after the execution of the each instruction below (Consider that AX = b before each of these instructions):  (A) SHL AL,2 ; AX =  (B) SAR AH,2 ; AX =  (C) ROR AX,4 ; AX =  (D) ROL AX,3 ; AX =  (E) SHL AL,8 ; AX =

19 Application: Binary Output  To display the binary number in EAX: MOV ECX,32 ; count 32 binary chars START: ROL EAX,1 ;CF gets msb JC ONE ;if CF =1 MOV EBX, ’0’ JMP DISP ONE: MOV EBX,’1’ DISP: PUTCH EBX LOOP START

20 Application: Binary Input  To load EAX with the numerical value of a binary string (ex: ) entered at the keyboard: xor ebx, ebx ;clear ebx to hold entry next: getch cmp eax, 10 ;end of input line reached? je exit ;yes then exit and al, 0Fh ;no, convert to binary value shl ebx, 1 ;make room for new value or bl, al ;put value in ls bit jmp next exit: mov eax,ebx ;eax holds binary value  In AL we have either 30h or 31h (ASCII code of ‘0’ and ‘1’)  Hence, AND AL,0Fh converts AL to either 0h or 1h  Hence, OR BL,AL possibly changes only the lsb of BL

21 Algorithm for Hex Output  To display in hexadecimal the content of EAX Repeat 8 times { ROL EAX, 4 ;the ms 4bits goes into ls 4bits MOV DL, AL AND DL, 0Fh ;DL contains num value of 4bits If DL < 10 then convert to ‘0’..’9’ else convert to ‘A’..’F’ } end Repeat  The complete ASM coding is left to the reader

22 Algorithm for Hex Input  To load EAX with the numerical value of the hexadecimal string entered at the keyboard: XOR EBX, EBX ;EBX will hold result While (input char != ) DO { convert char into numerical value left shift EBX by 4 bits insert value into lower 4 bits of EBX } end while MOV EAX,EBX  The complete ASM coding is left to the reader