140 likes | 347 Views
Assembly Language – Lab 4. Logic, Shift, and Rotate Instructions, Binary Representation. Logic Instructions. I nstructions to operate on bits are called logical instructions . The logical Instructions: AND , OR , XOR , NOT Syntax for AND , OR , XOR , and TEST instructions:
E N D
Assembly Language – Lab 4 Logic, Shift, and Rotate Instructions, Binary Representation
Logic Instructions • Instructions to operate on bits are called logical instructions. • The logical Instructions: AND, OR, XOR, NOT • Syntax for AND, OR, XOR, and TEST instructions: op-code destination, source • NOT syntax - NOT destination
Clearing Bits: AND Instruction • A bit and operation compares two bits and sets the result to 0 if either of the bits is 0. • To clear bit 5 of a byte we and the byte with 1101 1111 MOV AL, 62H ; AL = 0110 0010 AND AL, 0DFH ; and it with 1101 1111 ; AL is 42H 0100 0010 1 and 0 returns 0 0 and 1 returns 0 0 and 0 returns 0 1 and 1 returns 1
Setting Bits: OR Instruction • A bit OR operation compares two bits and sets the result to 1 if either bit is set to 1. 1 and 0 returns 1 0 and 1 returns 1 1 and 1 returns 1 0 and 0 returns 0
Example 1 • Assign value 2 in register AL, the print the value of that register.
XOR instruction • The XOR operation compares two bits and sets the result to 1 if the bits are different. • A common use of XOR is to clear a register, i.e. set all bits to 0, for example, we can clear register AX as follows XOR AX, AX 1 and 0 returns 1 0 and 1 returns 1 1 and 1 returns 0 0 and 0 returns 0
Example 2 • Converts uppercase letter to lowercase using OR instruction.
Shifting Bits to the Left • To shift 1 bit to the left we use: SHL destination,1 or SHL destination, CL ; CL = number of shifts • Each destination bit is shifted one position to the left • The lsb (least significant bit) is filled with 0 (in the rightmost). • Leftmost bits is moved into CF (Carry flag). • CF contains the last bit shifted out. • Each left shift multiplies by 2 the operand for both signed and unsigned interpretations. MOV AL, 82H ; AL = 1000 0010B SHL AL, 2 ; AL = 0000 1000B, CF=0
Shifting bits to the right • To shift to the right use either: SHR destination, CL ;value of CL = number of shifts SHR destination, 1 • The msb (most significant bit) is filled with 0 (leftmost). • rightmost bits is moved into CF (Carry flag). • Each single-bit right shift divides the unsigned value by 2. MOV BL,13 ;BL = 0000 1101B = 13 SHR BL,2 ;BL = 0000 0011B = 3 (DIV BY 4),CF=0
Rotate Instructions (ROL instruction) • The ROL (rotate left) instruction shifts the bits in the destination to the left. • The msb is shifted into the rightmost bit and also into the CF.
Example 3 • Count the number of 1 bits in BX (BX=10111101B), without changing BX. Put the answer in AX.
Rotate Instructions (ROR instruction) • The ROR (rotate right) instruction shifts the bits in the destination to the right. • The rightmost is shifted into the msb bit and also into the CF.
Example 4 • Input a binary number then display it in a newline.