Shift and Rotate Instructions.

Slides:



Advertisements
Similar presentations
Integer Arithmetic: Multiply, Divide, and Bitwise Operations
Advertisements

Machine Instructions 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
80x86 Instruction Set Dr. Qiang Lin.
1 Lecture 7 Integer Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
Shift and Rotate Instructions
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
Integer Arithmetic Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa [Adapted from slides of Dr. Kip Irvine:
Integer Arithmetic COE 205 Computer Organization and Assembly Language
Introduction to Computer Engineering by Richard E. Haskell Shift and Rotate Instructions Module M16.2 Section 10.3.
Assembly Language for x86 Processors 6th Edition
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.
Sahar Mosleh California State University San MarcosPage 1 CPU Flags and Boolean Instructions.
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.
Department of Computer Science and Software Engineering
Lecture 5 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
Sahar Mosleh California State University San MarcosPage 1 Review.
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.
Integer Arithmetic Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language for x86 Processors 6th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
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.
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.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may.
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
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.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
Computer Architecture and Assembly Language
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for x86 Processors 7th Edition
Microprocessor Systems Design I
Chapter 3 Bit Operations
EE3541 Introduction to Microprocessors
Instruction System - Bit Manipulation Instruction
Machine control instruction
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
Arithmetic and Logic Chapter 5
UNIT: 2 INSTRUCTION SET OF 8086.
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/11/17
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
Microprocessor and Assembly Language
Computer Organization and Assembly Language
CS-401 Computer Architecture & Assembly Language Programming
Presentation transcript:

Shift and Rotate Instructions

Along with the bitwise instructions, from the previous lectures, shift instructions are among the most characteristic of assembly language. Shift means to move bits right and left inside an operand. All the following instructions affect the overflow and the carry flag. SHL Shift left SHR Shift right SAL Shift arithmetic left SAR Shift arithmetic right ROL Rotate left ROR Rotate right RCL Rotate carry left RCR Rotate carry right SHLD double precision shift left SHRD double precision shift right

Logical Shift vs Arithmetic Shift There are two basic ways to shift the bits in a number. The first called a logical shift, fills the newly created bit position with Zero. In the following diagram, a byte is logically shifted one position to the right. Note that bit 7 is assigned 0 For example, if we do a single logical right shift on the binary value 11001111, it becomes 01100111 CF

The other type of shift is called an arithmetic shift. The newly created bit position is filled with a copy of the original numbers sign bit. For example, the binary value 11001111, has a 1 in the sign bit. When shifted, arithmetically 1 bit to the right, it becomes 11100111. CF

The first operand is the destination and the second is shift count. SHL Instruction The SHL instruction performs a logical left shift on the destination operand, filling the lowest bit with 0. The highest bit is moved to the carry flag, and the bit that was in the carry flag is lost. The first operand is the destination and the second is shift count. SHL destination, count The following lists the types of operands permitted by this instruction SHL reg, imm8 SHL mem, imm8 CF

Example In the following instructions, BL is shifted once to the left. The highest bit is copied into the carry flag and the lowest bit position is cleared. mov bl, 8FH ; BL = 10001111b Shl b1, 1 ; BL = 00011110b, CF = 1

Fast Multiplication One of the best uses of SHL is for performing high-speed multiplication by powers of 2. Shifting any operand by n bits multiplies the operand by 2^n. For example, shifting 5 left 1 bit yields the product 5*2 mov dl, 5 shl d1, 1 Before 00000101 = 5 After 00001010 = 10 If we shift 10 by 2 bits, the result is the same as multiplying 10 by 2^2. mov dl, 10 SHL d1, 2 ; ( 10 * 4) = 40

SHR Instruction The SHR instruction performs a logical right shift on the destination operand, filling the highest bit with 0. The lowest bit is moved to the carry flag, and the bit that was in the carry flag is lost. SHR uses the same instruction format as SHL. In the following example, the 0 from the lowest bit in AL is copied into the carry flag, and the highest bit in AL is cleared. mov al, 0D0h ; AL = 11010000b shr al, 1 ; AL = 01101000b CF

Fast Division Logically shifting any unsigned operand right by n bits divides the operand by 2^n. For example, we divide 32 by 2^1, producing 16 mov dl, 32 shr dl, 1 Before 00100000 = 32 After 00010000 = 16 In the following example, 64 is divided by 2^3. mov al, 01000000b ; AL = 64 shr al, 3 ; divide by 8, al = 00001000b Division of signed numbers by shifting is accomplished using SAR instruction because it preserves the number’s sign bit.

SAL and SAR instructions SAL (Shift Arithmetic Left) is identical to SHL instruction. The SAR (Shift Arithmetic Right) instruction performs a right arithmetic shift on its destination operand. The syntax and operands for SHR and SAR are identical to those for SHL and SHR. The shift may be repeated, based on the counter in the second operand. SAR destination, count CF

Example The following example, shows how SAR duplicates the sign bit al is negative before and after it is shifted to the right. mov al, 0F0h ; al = 11110000b (-16) sar al, 1 ; al = 11111000b (-8) CF = 0 Signed Division: You can divide a signed operand by a power of 2 using the SAR instruction. In the following example, -128 is divided by 2^3. The quotient is -16 mov dl, -128 ; dl = 10000000b sar dl, 3 ; dl = 11110000b

ROL Instruction The ROL (rotate left instruction) shifts each bit to the left. Also, the highest bit is copied both into the carry flag and into the lowest bit. The instruction format is the same as for the SHL instruction Bit rotation differs from bit shifting in that the former does not lose any bits. A bit that is rotated of one end of a number appears again at the other end. CF

You can use ROL to exchange the upper (4-7 bit) and lower In the following example, the high bit is copied into both the carry flag and bit position zero mov al, 40h ; AL = 01000000b rol al, 1 ; AL = 10000000b ; CF = 0 rol al, 1 ; AL = 00000001b; CF = 1 rol al, 1 ; AL = 00000010b; CF = 0 You can use ROL to exchange the upper (4-7 bit) and lower (bits 0-3) halves of the byte. mov al, 26h rol al, 4 ; AL = 62h

ROR Instruction The ROR instruction shifts each bit to the right. Also the lowest bit is copied into the flag and into the highest bit at the same time The instruction format is the same as for SHL In the following example, the lowest bit is copied into the carry flag and into the highest bit of the result mov al, 01h ; AL = 00000001b ror al, 1 ; AL = 10000000b, CF = 1 ror al, 1 ; AL = 01000000b, CF = 0 CF

RCL and RCR instructions The RCL (Rotate Carry left) instruction shifts each bit to the left copies the carry flag to least significant bit (LSB), and copies the most significant bit (MSB) into the carry flag. If you think of the carry flag as just an extra bit added to the high end of the number, then RCL becomes a simple rotate left operation. CF

In the following example, the CLC instruction clears the carry flag. The first RCL instruction moves the high bit of bl into the carry flag and shifts all other bits to the left The second RCL instruction moves the carry flag into the lowest bit position CLC ; CF = 0 mov bl, 88h ; CF, BL = 0 10001000b rcl bl, 1 ; CF, BL = 1 00010000b rcl b1, 1 ; CF, BL = 0 00100001b

Recover a bit from the carry flag RCL can recover a bit that has previously been shifted into the carry flag. The following example, checks the lowest bit of testval by shifting its lowest bit into the carry flag. Then RCL restores the number to its original value. .data testval byte 01101010b .code shr testval, 1 ; shift LSB into the carry flag jc quit ; exit if carry flag set rcl testval, 1 ; else restore the number

RCR Instruction The RCR (Rotate Carry Right) instruction shifts each bit to the right, copies the right flag into the most significant bit, and copies the least significant bit into the carry flag. As in the case of RCL, it helps to visualize the integer in this figure as a 9-bit value, with the carry flag to the right of the least significant bit CF

In the following example, STC sets the carry flag before rotating the carry flag into the MSB, and rotating the LSB into the carry flag. STC ; CF = 1 mov ah, 10h ; CF, Ah = 00010000 1 rcr ah, 1 ; CF, Ah = 10001000 0