Download presentation
Presentation is loading. Please wait.
1
Microprocessor and Assembly Language
Lecture-9-Bit Manipulation Instructions Muhammad Hafeez Department of Computer Science GC University Lahore
2
Today’s Agenda Logic Instructions (AND, OR, NOT, XOR)
Shift Instructions (SHL, SHR, SAL, SAR) Rotate Instructions (ROL, ROR, RCL, RCR)
3
Login Instructions Usage To Set Bits To Clear Bits To Examine Bits
4
AND, OR, XOR Peform Bit-Wise Operations on Operands and store result in destination Sytax AND Destination, Source OR Destination, Source XOR Destination, Source Destination Must be Register or Memory Location Source could be immediate, register or memory location Memory to Memory is not allowed
5
AND, OR, XOR Affect On Flag Rgister
SF, ZF, SF are affected AF is undefined CF, OF=0 (since they operate on bits not on numbers)
6
Bit Manipulation Using Mask
We can selectively modify bits in Destination using bitter pattern called ‘mask’ We use following properties of AND, OR and XOR Y AND 1 = Y, Y AND 0 = 0 Y OR 1 = 1, Y OR 0 =Y Y XOR 1 = ~Y, Y XOR 0 = 1
7
Bit Encoding and Use of Bit Masks
ASCII is a bit encoding of English Character Set, you can develop encoding at your own AND can be used to clear bits, prepare a bit pattern (mask) that has a ‘0’ at particular bit position, set ‘1’ at remaining position to preserve them OR can be used to set bits, prepare a mask that has ‘1’ at particular bit, position, place ‘0’ at other position XOR can be used to complement specific bit, a ‘1’ mask complement, ‘0’ preserve
8
Examples Prepare following Masks:
Clear the Sign of AL=0E5H using AND Instruction Set MSB and LSB and clear other bits in BL=0DFH using OR Change Sign of DL=0FEH using XOR
9
Use in Assembly Language
AND Instruction is used Convert an ASCII Digit to a Number (Its Reverse) Convert an Upper Case Letter to Lower Case (Its Reverse)
10
Use in Assembly Language
AND Instruction is used Convert an ASCII Digit to a Number Bit mask – b – Hex = 0FH AND this mask with Destination (Reg/ Mem) AND AL,0FH (Its Reverse) Convert an Upper Case Letter to Lower Case Bit Mask = d – Clear 5th Bit AND AL, 0DFH (Its Reverse)
11
Use in Assembly Language
XOR Instruction is used Clear a Register/ Memory Location Alternate is MOV AX,0 Or XOR AX,AX In what condition XOR cannot be used? (m/m) OR Instruction OR AL, AL = CMP AL,0 Test a register is zero
12
Use in Assembly Language
NOT Instruction is used To compliments the bits
13
Test Instruction Just Like CMP to SUB instruction, TEST instruction is the alternate to AND instruction Used to Test specific bits, place in ‘1’ at those positions, does not change destination TEST Destination, mask The result has 1 in tested position, IF, destination has 1’s in those positions, otherwise, result is zero, ZF=1 (if AL is even, even numbers has 0 at 0 position) TEST AL,1 JZ Below ;yes
14
Test Instruction Just Like CMP to SUB instruction, TEST instruction is the alternate to AND instruction Used to Test specific bits, place in ‘1’ at those positions, does not change destination TEST Destination, mask The result has 1 in tested position, IF, destination has 1’s in those positions, otherwise, result is zero, ZF=1 (if AL is even, even numbers has 0 at 0 position) TEST AL,1 JZ Below ;yes
15
Shift Instructions Shifts the bits in DESTINATION to Right OR Left, One Place or More Place There are two types of Shift Instruction SHL (Shift Left) SHR (Shift Right) And SAL (Shift Arithmetic Left) SAR (Shift Arithmetic Right)
16
Shift Instructions There are two formats to write shift instruction
Opcode Destination, count OR Opcode Destination, CL SHL/SHR/SAL/SAR REG/MEM , count SHL/SHR/SAL/SAR REG/MEM , CL Count could be immediate value between 0 and 15
17
Shl Instruction Shift Number of bits to left in the destination
A ‘0’ is placed at LSB, MSB is shifted to CF If more than 1 bits to be shifted, place value in CL register
18
Shl Instruction Affect on Flag
SF, PF, ZF reflect the result AF is undefined CF= last bit shifted out OF For multishift OF is undefined For Single Shift OF = 1 if MSB changes after shift
19
Sal Instruction Due to arithmetic nature of SHL, often SAL is used
Both Instructions produces the same result MOV CL,3 SAL AL,CL MULTIPLY AL BY 8
20
Shr Instruction Perform right shift on destination Format is:
SHR Destination, count SHR Destination, 1 SHR Destination, CL A zero is shift at MSB, LSB is shifted to CF
21
Sar Instruction Equivalent to SHR instruction
22
Rotate Instructions Shifts the bits within a register or memory location without discard Two types of Rotate Instructions ROL (Rotate Left) ROR (Rotate Right) And RCL (Rotate through Carry Left) RCR (Rotate through Carry Right)
23
Rotate Instruction Shifts the bits within a register or memory location without discard General Syntax Format ROL/ROR/RCL/RCR REG/MEM , count ROL/ROR/RCL/RCR REG/MEM , CL
24
Rol Instruction Rotate Left bits are exited from MSB and entered in LSB, as well as, every bit that leave MSB copies itself in CF
25
Rol Instruction
26
Ror Instruction Rotate Right bits are exited from LSB and entered in MSB, as well as, every bit that leave LSB copies itself in CF
27
Rcl Instruction Rotate through Carry Left bits are exited from MSB and entered in CF and from CF entered in LSB, acts as CF is a part of operand
28
Rcr Instruction Rotate through Carry right bits are exited from LSB and entered in CF and from CF entered in MSB, acts as CF is a part of operand
29
Application of Shifts and Rotate Instructions
Binary Input
30
Application of Shifts and Rotate Instructions
Binary Input
31
Application of Shifts and Rotate Instructions
Binary Output
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.