MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE9 BIT MANIPULATION INSTRUCTIONS
MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE-9 -BIT MANIPULATION INSTRUCTIONS MUHAMMAD HAFEEZ DEPARTMENT OF COMPUTER SCIENCE GC UNIVERSITY LAHORE
TODAY’S AGENDA l l l Logic Instructions (AND, OR, NOT, XOR) Shift Instructions (SHL, SHR, SAL, SAR) Rotate Instructions (ROL, ROR, RCL, RCR)
LOGIN INSTRUCTIONS l Usage l l l To Set Bits To Clear Bits To Examine Bits
AND, OR, XOR l l Peform Bit-Wise Operations on Operands and store result in destination Sytax l l l 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
AND, OR, XOR AFFECT ON FLAG RGISTER l l l SF, ZF, SF are affected AF is undefined CF, OF=0 (since they operate on bits not on numbers)
BIT MANIPULATION USING MASK l l l 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
BIT ENCODING AND USE OF BIT MASKS l l 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
EXAMPLES l l Prepare following Masks: Clear the Sign of AL=0 E 5 H using AND Instruction Set MSB and LSB and clear other bits in BL=0 DFH using OR Change Sign of DL=0 FEH using XOR
USE IN ASSEMBLY LANGUAGE l AND Instruction is used l Convert an ASCII Digit to a Number (Its Reverse) l Convert an Upper Case Letter to Lower Case (Its Reverse)
USE IN ASSEMBLY LANGUAGE l AND Instruction is used l l l l Convert an ASCII Digit to a Number Bit mask – 0000 1111 b – Hex = 0 FH AND this mask with Destination (Reg/ Mem) AND AL, 0 FH (Its Reverse) Convert an Upper Case Letter to Lower Case Bit Mask = 1101 1111 d – Clear 5 th Bit AND AL, 0 DFH (Its Reverse)
USE IN ASSEMBLY LANGUAGE l XOR Instruction is used l Clear a Register/ Memory Location l l l 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
USE IN ASSEMBLY LANGUAGE l NOT Instruction is used l To compliments the bits
TEST INSTRUCTION l l l 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 l l 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
TEST INSTRUCTION l l l 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 l l 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
SHIFT INSTRUCTIONS l Shifts the bits in DESTINATION to Right OR Left, One Place or More Place l There are two types of Shift Instruction l l l SHL (Shift Left) SHR (Shift Right) And l l SAL (Shift Arithmetic Left) SAR (Shift Arithmetic Right)
SHIFT INSTRUCTIONS l There are two formats to write shift instruction l Opcode Destination, count OR Opcode Destination, CL l l l SHL/SHR/SAL/SAR l Count could be immediate value between 0 and 15 l REG/MEM , count REG/MEM , CL
SHL INSTRUCTION l l l 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
SHL INSTRUCTION AFFECT ON FLAG l SF, PF, ZF reflect the result AF is undefined l CF= last bit shifted out l OF l l l For multishift OF is undefined For Single Shift OF = 1 if MSB changes after shift
SAL INSTRUCTION l l Due to arithmetic nature of SHL, often SAL is used Both Instructions produces the same result l MOV CL, 3 SAL AL, CL l MULTIPLY AL BY 8 l
SHR INSTRUCTION l Perform right shift on destination l Format is: l SHR l l l Destination, count SHR Destination, 1 SHR Destination, CL A zero is shift at MSB, LSB is shifted to CF
SAR INSTRUCTION l Equivalent to SHR instruction
ROTATE INSTRUCTIONS l Shifts the bits within a register or memory location without discard l Two types of Rotate Instructions l l l ROL (Rotate Left) ROR (Rotate Right) And l l RCL (Rotate through Carry Left) RCR (Rotate through Carry Right)
ROTATE INSTRUCTION l Shifts the bits within a register or memory location without discard l General Syntax Format l l ROL/ROR/RCL/RCR REG/MEM , count REG/MEM , CL
ROL INSTRUCTION l Rotate Left bits are exited from MSB and entered in LSB, as well as, every bit that leave MSB copies itself in CF
ROL INSTRUCTION
ROR INSTRUCTION l Rotate Right bits are exited from LSB and entered in MSB, as well as, every bit that leave LSB copies itself in CF
RCL INSTRUCTION l 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
RCR INSTRUCTION l 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
APPLICATION OF SHIFTS AND ROTATE INSTRUCTIONS l Binary Input
APPLICATION OF SHIFTS AND ROTATE INSTRUCTIONS l Binary Input
APPLICATION OF SHIFTS AND ROTATE INSTRUCTIONS l Binary Output
- Slides: 31