Logical Instructions of 8086
There are two types of logical instructions in 8086
- Shift instructions
- Rotate instructions.
These two instructions move the bits inside the register side by side. The syntax of these instructions is
ROTATE INSTRUCTIONS:
ROL BL , 1
This instruction is to shift all the bits from LSB to all the way left of it. Thus named rotate left. Here BL is the register in which rotation should be done. Here 1 specifies the number of rotations.
If number of rotations in 1
Then ROL BL, 1
If number of rotations are more than 1
Then
MOV CL , 05H
ROL BL, CL
If the number of rotations is more than one, then we have to use the CL register.
From the above figure, rotating left goes all up to MSB, and MSB value is assigned to LSB as well as Carry Flag, where the CF has lost its value.
ROR BL, 1
This instruction is to shift all the bits from MSB to all the way right of it. Thus named rotate right.
From the above figure, rotating right goes all up to LSB, and LSB value is assigned to MSB as well as Carry Flag, where the CF has lost its value.
RCL BL, 1
This instruction is to shift all the bits from LSB to all the way left of it including the carry flag in it. Thus named rotate left with carry.
From the above figure, rotating left goes all up to CF, and then the value of CF is assigned to LSB. Here we don’t lose the data of the CF flag register.
RCR BL, 1
This instruction is to shift all the bits from MSB to all the way left of it including the carry flag in it. Thus named rotate right with carry.
From the above figure, rotating right goes all up to CF, and then the value of CF is assigned to MSB. Here we don’t lose the data of the CF flag register.
Application of rotate instructions:
Find whether a byte is even or odd?
To find this , we have to determine the value of LSB ( all other bits are multiple of 2 ) , if LSB is 1 then its odd number or else it’s even number.
Rotate left and let LSB come in CF, access the CF.
Then rotate back right to get actual data. To get the original value of CF use RCR and RLC.
Find any bit in a register !
Then rotate the bits, until the required bit gets in CF. access the CF.
CF is a part of the flag register which is SFR, and all the bits of CF are given or accessed by the programmer.
Swap higher nibble and lower nibble.
Rotate 4 times. Exclude CF in the loop.
SHIFT INSTRUCTIONS:
These instructions are similar to rotate instructions , but there is no shift from last bit to starting bit.
SAL BL , 1
Shift left. Here all the bits are shifted to right , and LSB doesn’t get any bit, automatically it gets a zero. So if there are 4 shift operations , then the lower nibble gets all zeroes, as the number of shifts increases, zeroes get increased from the lower side. Syntax is similar to rotation.
This instruction can be written using any data type SAL or SHL. Both represent the same thing.
SHR BL, 1
Here shifting is done towards right. In this case MSB gets no shifted value from other bit, so this automatically gets zero.
SAR BL, 1
In the above instruction, if it was a signed number , then because of shifting it may become a +ve number from -ve number. SAR stands for shift arithmetic register. In this MSB doesn’t get a zero, MSB get itself it’s value, MSB also shifts the bit and also retains it.
Read related topics:
Arithmetic instructions of 8086