Logic Instructions
The ability to manipulate the individual bits of a Data is one of the key advantages of assembly language. In order to do so we use Logic instructions. Let us first remind the basic ligic instructions for now,
AND
0 0 -> 0
1 0 -> 0
0 1 -> 0
1 1 -> 1
OR
0 0 -> 0
1 0 -> 1
0 1 -> 1
1 1 -> 1
XOR
0 0 -> 0
1 0 -> 1
0 1 -> 1
1 1 -> 0
NOT
0 -> 1
1 -> 0
Now lets see where we use these functions in Assembly -
AND -> To clear
OR -> To set
XOR -> To compliment desired bit positions
NOT -> To compliment all bits
AND (Used to Clear )
The form is
AND DESTINATION, SOURCE
Suppose we want to clear the sign bit of AL then the code will be
AND AL,7Fh ;
we know that the sign bit is the MSB so if we AND it with the value which has all 1s except of the MSB as 0 then
AL = 10101100
Msk = 01111111
-----------------------------
rslt = 00101100
OR (used to set)
The form is
OR DESTINATION, SOURCE
Suppose we want to set the sign bit of AL then the code will be
OR AL,80h ;
we know that the sign bit is the MSB so if we OR it with the value which has all 0s except of the MSB as 1 then
AL = 00101100
Msk = 10000000
-----------------------------
rslt = 10101100
XOR (Used to compliment selected bits )
The form is
XOR DESTINATION, SOURCE
Suppose we want to change the sign bit of AL then the code will be
XOR AL,80h ;
we know that the sign bit is the MSB so if we XOR it with the value which has all 0s except of the MSB which is 1 then
AL = 10101100
Msk = 10000000
-----------------------------
rslt = 00101100
Here you may have noticed that destination bit is complimented if that bit position of the Mask is 1 and unchanged if it is 0. (XOR x , 0 - x) (XOR x,1)= x'
NOT (Used to compliment all bits )
The form is
NOT DESTINATION
Suppose we want to invert all the bits of AL then the code will be
NOT AL ;
You can see that NOT takes only one argument.
Suppose AL = 10110010 , after executing the NOT instruction it will be 01001101
Problem (Convert ASCII value to number)
When we enter a character in Assembly the equivalent ASCII value is stored in the memory. Say we pressed 3 then the ASCII value 33h will be saved in AL. we have to convert it to 3...
If we AND a value with 0000 1111 then the relust will be 30 less than the original value
AL = 0011 0011 =33
Msk = 0000 1111
------------------------------
0000 1111 =3
so the code will be
AND AL, 0Fh