Boolean, Shift and Rotate instructions Dr.Hadi AL Saadi.

Slides:



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

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.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
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
CS2422 Assembly Language and System Programming Conditional Processing Department of Computer Science National Tsing Hua University.
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, 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
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.
11.1/36 Repeat: From Bits and Pieces Till Strings.
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.
Lecture 5 Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
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
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.
CT215: Assembly Language Programming
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
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.
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.
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.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
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.
Assembly Language for Intel-Based Computers, 4 th Edition Week 10: Conditional Processing Slides modified by Dr. Osama Younes.
CS2422 Assembly Language and System Programming 0 Week 13 & 14 Codes in Assembly Language.
Assembly Language for Intel-Based Computers, 5th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Practical Session 2.
EE3541 Introduction to Microprocessors
Instruction System - Bit Manipulation Instruction
Machine control instruction
More on logical instruction and
Assembly Language Programming Part 2
INSTRUCTION SET OF 8086 PAWAN KUMAR SINGH.
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
Microprocessor and Assembly Language
Computer Organization and Assembly Language
Shift and Rotate Instructions.
Presentation transcript:

Boolean, Shift and Rotate instructions Dr.Hadi AL Saadi

Boolean,SHIFT and Rotate Instructions AND Instruction OR Instruction XOR Instruction NOT Instruction BT Instruction Applications TEST Instruction SHL,SHR, SAR and SAL instructions ROR,ROL, RCR and RCL instructions

Logic, Shift and Rotate Instructions: AND, OR and XOR instructions: Syntax: AND destination,source OR destination,source XOR destination,source Result stored in destination, which must be: Reg. or memory location source may be: constant, Reg., or Mem. Location. But Mem. not allowed. Effect on Flags: S,Z, and P indicate result, and CF=OF=0, A is undefined

AND Instruction Performs a Boolean AND operation between each pair of matching bits in two operands Syntax: AND destination, source AND

OR Instruction Performs a Boolean OR operation between each pair of matching bits in two operands Syntax: OR destination, source OR

XOR Instruction Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands Syntax: XOR destination, source XOR XOR is a useful way to toggle (invert) the bits in an operand.

NOT Instruction Performs a Boolean NOT operation on a single destination operand Syntax: NOT destination NOT

Bit-Mapped Sets Binary bits indicate set membership Efficient use of storage Also known as bit vectors

Bit-Mapped Set Operations Set Complement mov ax,SetX not ax Set Intersection mov ax,setX and ax,setY Set Union mov ax,setX or ax,setY Selectivity modify bits in destination, by constructing a source bit pattern (MASK), by masking use of their properties, thus 1. AND Inst. Clear specific bit (0 mask), while keep others (1 mask) 2. OR Inst. Set specific bits (1 mask), while keep others ( 0 mask) 3. XOR inst. Complement specific bits (1 mask), while keep others ( 0 mask)

mov al,'a'; AL = b and al, b; AL = b Task: Convert the character in AL to upper case. Solution: Use the AND instruction to clear bit 5. Applications mov al,6; AL = b or al, b; AL = b Task: Convert a binary decimal byte into its equivalent ASCII decimal digit. Solution: Use the OR instruction to set bits 4 and 5. The ASCII digit '6' = b

Task: Jump to label L1 if bits 0, 1, and 3 in AL are all set. Solution: Clear all bits except bits 0, 1,and 3. Then compare the result with binary. and al, b; clear unwanted bits cmp al, b; check remaining bits je L1; all set? jump to L1 Task: Jump to a label if an integer is even. Solution: AND the lowest bit with a 1. If the result is Zero, the number was even. mov ax,wordVal and ax,1; low bit set? jz EvenValue; jump if Zero flag set Applications

or al,al jnz IsNotZero; jump if not zero Task: Jump to a label if the value in AL is not zero. Solution: OR the byte with itself, then use the JNZ (jump if not zero) instruction. Applications and al, b; clear unwanted bits cmp al, b; check remaining bits je L1; all set? jump to L1 Task: Jump to label L1 if bits 0, 1, and 3 in AL are all set. Solution: Clear all bits except bits 0, 1,and 3. Then compare the result with binary.

mov ah,1 Int 21h ; get character in ; AL cmp al,'0'; ZF = 0 jb ID1 cmp al,'9'; ZF = 0 ja ID1 test ax,0 ; ZF = 1 ID1: Write a code to receives a character in AL. Sets the Zero flag if the character is a decimal digit.

Your turn... Write code that jumps to label L1 if either bit 4, 5, or 6 is set in the BL register. Write code that jumps to label L1 if bits 4, 5, and 6 are all set in the BL register. Write code that jumps to label L2 if AL has even parity. Write code that jumps to label L3 if AX is negative. Write code that jumps to label L4 if the expression (BX – CX) is greater than zero.

TEST Instruction Performs a nondestructive AND operation between each pair of matching bits in two operands Effect on Flags: S,Z, P reflect the result, CF=OF, and AF unchanged Syntax : TEST destination, source TEST instruction can be used to examine individual bits in operand, thus the MASK must contain 1’s at tested bit – positions, and 0’s elsewhere test al, b jnz ValueFound Example: jump to a label if neither bit 0 nor bit 1 in AL is set. test al, b jz ValueNotFound Example: Jump to label BELLOW if AL contains even number, Solution : even number have 0 in 1sb, thus MASK= b=1d TEST AL,1 ; is an even ? JZ BELLOW ; yes, go to BELLOW

Encrypting a String KEY = 239; can be any byte value BUFMAX = 128.data buffer BYTE BUFMAX+1 DUP(0) bufSize DWORD BUFMAX.code mov ecx,bufSize; loop counter mov esi,0; index 0 in buffer L1: xor buffer[esi],KEY; translate a byte inc esi; point to next byte loop L1 The following loop uses the XOR instruction to transform every character in a string into a new value.

String Encryption Program Tasks: Input a message (string) from the user Encrypt the message Display the encrypted message Decrypt the message Display the decrypted message Enter the plain text: Attack at dawn. Cipher text: «¢¢Äîä-Ä¢-ïÄÿü-Gs Decrypted: Attack at dawn.

BT (Bit Test) Instruction Copies bit n from an operand into the Carry flag Syntax: BT bitBase, n bitBase may be r/m16 or r/m32 n may be r16, r32, or imm8 Example: jump to label L1 if bit 9 is set in the AX register: bt AX,9; CF = bit 9 jc L1; jump if Carry

Shift and Rotate Instructions Logical vs Arithmetic Shifts SHL Instruction SHR Instruction SAL and SAR Instructions ROL Instruction ROR Instruction RCL and RCR Instructions

SHIFT and Rotate Instruction:  Shift and Rotate instructions, shift Bits in destination by 1 or more bit-position, either to Left or to Right.  Shift inst. : bit-shifted out are lost.  Rotate Inst. Bits Shifted-out from one-end Re-input at the other-end  There are two possible forms: Opcode destination,1 ;single shift or Rotate Opcode destination,CL; CL contain number (N) of bit-positions to be Shifted destination is 8 or 16 bit Reg. or Memory location.

Logical Shift A logical shift fills the newly created bit position with zero:

Arithmetic Shift An arithmetic shift fills the newly created bit position with a copy of the number’s sign bit:

SHL Instruction The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with 0. Operand types for SHL: SHL reg,imm8 SHL mem,imm8 SHL reg,CL SHL mem,CL (Same for all shift and rotate instructions)

Fast Multiplication mov dl,5 shl dl,1 Shifting left 1 bit multiplies a number by 2 mov dl,5 shl dl,2; DL = 20 Shifting left n bits multiplies the operand by 2 n For example, 5 * 2 2 = 20

SHR Instruction The SHR (shift right) instruction performs a logical right shift on the destination operand. The highest bit position is filled with a zero. mov dl,80 shr dl,1; DL = 40 shr dl,2; DL = 10 Shifting right n bits divides the operand by 2 n

SAL and SAR Instructions SAL (shift arithmetic left) is identical to SHL. SAR (shift arithmetic right) performs a right arithmetic shift on the destination operand. An arithmetic shift preserves the number's sign. mov dl,-80 sar dl,1; DL = -40 sar dl,2; DL = -10

Example: if AX=FFFFh =-1d then SAL 3-times MOV CL,3 SAL AL,CL makes AX=FFF8=-8 Overflow may occur, then; Flags : OF (signed), and CF (unsigned) indicate result of last shift. Example: If DH=8A h, CL=2, what is the value of DH, CF after: SHR DH, CL? Solution :initial DH= then DH= b=22h CF=1  Example: use Right Shift to divide unsigned number by 4, put result in AX. MOV AX, ; AX has the number MOV CL,2 SHR AX, CL ; divide by 4

Your turn... mov al,6Bh shr al,1a. shl al,3b. mov al,8Ch sar al,1c. sar al,3d. Indicate the hexadecimal value of AL after each shift: 35h A8h C6h F8h

ROL Instruction ROL (rotate) shifts each bit to the left The highest bit is copied into both the Carry flag and into the lowest bit No bits are lost mov al, b rol al,1; AL = b mov dl,3Fh rol dl,4; DL = F3h

ROR Instruction ROR (rotate right) shifts each bit to the right The lowest bit is copied into both the Carry flag and into the highest bit No bits are lost mov al, b ror al,1; AL = b mov dl,3Fh ror dl,4; DL = F3h

Your turn... mov al,6Bh ror al,1a. rol al,3b. Indicate the hexadecimal value of AL after each rotation: B5h ADh

 Example: show how to inspect bits in a Byte or Word, without changing their contents. Use ROL to count the number of 1-bits in BX without changing BX, put the answer in AX. solution: XOR AX, AX ; AX counts bit MOV CX,16 ; loop control count TOP: ROL BX,1 ; CF contain bit rotated - out JNC NEXT ; is 0-bit (no carry) INC AX ; is 1-bit (carry), increment counter NEXT: LOOP TOP ; loop until completed

RCL Instruction RCL (rotate carry left) shifts each bit to the left Copies the Carry flag to the least significant bit Copies the most significant bit to the Carry flag clc; CF = 0 mov bl,88h; CF,BL = b rcl bl,1; CF,BL = b rcl bl,1; CF,BL = b

RCR Instruction RCR (rotate carry right) shifts each bit to the right Copies the Carry flag to the most significant bit Copies the least significant bit to the Carry flag stc; CF = 1 mov ah,10h; CF,AH = b rcr ah,1; CF,AH = b

Your turn... stc mov al,6Bh rcr al,1a. rcl al,3b. Indicate the hexadecimal value of AL after each rotation: B5h AEh

Application of Shift and Rotate Instructions : Reversing Bit pattern in a BYTE or Word. If AL= , we want to make it = Initialize CX to LOOP_Count MOV CX,8 ; number of reverse operations to do REVERSE: SHL AL,1 ; get bit into CF RCR BL,1 ; rotate it into BL LOOP REVERSE ; loop until done MOV AL,BL ; AL gets reverse pattern

Binary Multiplication mutiply 123 * 36

Binary Multiplication We already know that SHL performs unsigned multiplication efficiently when the multiplier is a power of 2. You can factor any binary number into powers of 2. For example, to multiply AX * 36, factor 36 into and use the distributive property of multiplication to carry out the operation: AX * 36 = AX * (32 + 4) = (AX * 32)+(AX * 4) mov ax,123 mov bx,ax shl ax,5; mult by 2 5 shl bx,2; mult by 2 2 add ax,bx

Your turn... mov ax,2; test value mov dx,ax shl dx,4; AX * 16 push edx; save for later mov dx,ax shl dx,3; AX * 8 shl ax,1; AX * 2 add ax,dx; AX * 10 pop edx; recall AX * 16 add ax,dx; AX * 26 Multiply AX by 26, using shifting and addition instructions. Hint: 26 =

Displaying Binary Bits Algorithm: Shift MSB into the Carry flag; If CF = 1, append a "1" character to a string; otherwise, append a "0" character. Repeat in a loop, 32 times..data buffer BYTE 32 DUP(0),0.code mov cx,32 LEA si, buffer L1:shl ax,1 mov [si],'0' jnc L2 mov [si],'1' L2:inc si loop L1

Isolating a Bit String The MS-DOS file date field packs the year, month, and day into 16 bits: mov ax,dx; make a copy of DX shr ax,5; shift right 5 bits and al, b; clear bits 4-7 mov month,al; save in month variable Isolate the Month field: