Lecture 7 (A)
Lecture 7 (A)
1 10101010 2 10101010
=10100000 =11111010
= 01011010
AND, OR, and XOR
instructions
The AND, OR, and XOR instructions perform the named logic
operations. The formats are:
OR destination, source
AF is undefined
CF, OF= 0
One use of AND, OR, and XOR is to selectively modify the bits in
the destination.
b AND 1 = b
b OR 0 = b
b XOR 0 = b
b AND 0 =0
b OR 1 = 1
b XOR 1 = ~b (complement of b)
The AND instruction can be used to CLEAR specific destination bits while
preserving the others.
Example
Solution:
Thus, OR AL,81h
*** To avoid typing errors, it's best to express the mask in hex rather than
binary, especially if the mask would be 16 bits long.
Converting an ASCII Digit to a
Number
when program reads a character or digit from the keyboard, AL gets the ASCII code of
the character.
For example, if the "5" key is pressed, AL gets 35h instead of 5. To get 5 in AL, we did
SUB AL,30h
We can also do this by using an AND instructions to clear the high four bits of AL.
AND AL,0Fh
As the ASCII codes of "0" to "9" are 30h to 39h, this method will convert any ASCII digit
to a decimal value.
Using AND emphasizes on modifying bit pattern of AL and makes program more
readable.
Sub DL,20h
SUB AX,AX
CMP CX,0
Not Instruction
NOT AX
TEST Instruction
The TEST Instruction performs an AND operation of the destination
with the source but does not change the destination contents.
The purpose of the test instruction is to set the status flags. The
format is:
CF, OF =0
AF = Undefined
The mask should contain 1's in the bit positions to be tested and
0’s elsewhere
• As 1 AND b = b, 0 AND b = 0
Will have 1's in the tested bit positions if and only if the
destination has 1’s in these positions; and 0’s elsewhere.
if the destination has 0’s in all the tested positions, the result
will be 0 and thus ZF=1
Find Even Number
Example: Jump to label BELOW, If AL contains an even
number.
Solution: Even numbers have a 0 in bit 0. Thus, the mask is
00000001b=1
TEST AL, 1
JZ BELOW
The shift and rotate instructions shift the bits in the destination operand
by one or more positions either to the left or right.
For a rotate instruction, bits shifted out from one end of the operand are
put back into the other end.
The instruction have two possible formats. For a single shift or rotate,
the form is
Opcode destination,1
• Opcode destination, CL
The SHL (shift left) instruction shifts the bits in the destination to the left. The format for a
single shift is
SHL destination, 1
A 0 is shifted into the rightmost bit position and the msb is shifted into CF. If the shift
count N is different from 1, the instruction takes the form
SHL destination, CL (Here CL contains N and the above instruction made N single shifts)
Example: Suppose DH contains 8Ah and CL contains 3. What are the values
of DH and of CF after the instruction SHL DH,CL is executed?
The binary value of DH is 10001010. After 3 left shifts, CF will contain 0. The
new contents of DH may be obtained by
Adding three zero bits to the right end, thus 01010000b = 50h.
Multiplication by Left Shift
Let us consider a decimal number 235.
• If each digit is shifted left and 0 is attached on the right end, we get
2350 which is same as multiplying by 10.
For a single left shift, CF and OF accurately indicate unsigned and signed
over- flow, respectively.
However, the overflow flags are not reliable indicators for a multiple left shift
as multiple shift is really a series of single shifts, and OF and CF only reflect
the result of the last shift.
Right Shift (SHR) Instructions
The instruction SHR (shift right) performs right shifts on the destination
operand
SHR destination, 1
A 0 is shifted Into the msb position, and the rightmost bit is shifted
SHR destination, CL
** here CL contains N In this case N single right shifts are made. The effect on
the flags is the same as for SHL
References