Chap 4 e
Chap 4 e
Chap 4 e
Logic Instructions
Syntax for AND, OR, XOR, and TEST instructions:
op-code destination, source
They perform the Boolean bitwise operation and store the result into destination. TEST is just an AND but the result is not stored. TEST affects the flags just like AND does. Both operands must be of the same type
since this does not change the number in ECX and set ZF=1 if and only if ECX=0
Since b XOR b = 0 (b is cleared) This instruction uses only 2 bytes of space. The next instruction uses 3 bytes of space:
MOV ax,0
But 20h = 0010 0000b and bit #5 is always 0 for chars from A (41h) to Z (5Ah). Uppercase (41h-5Ah) A-Z Lowercase (61h-Ah) a-z
4X 0 1 0 0 X 5X 0 1 0 1 X 6X 0 1 1 0 X 7X 0 1 1 1 X
Hence, adding 20h gives the same result as setting this bit #5 to 1. Thus:
OR dl,20h ;converts from upper to lower case AND dl,0DFh;converts from lower to upper case
does not affect any flag and destination cannot be an imm operand Recall that to perform twos complement, we use
NEG destination
affect SF and ZF according to result CF is set to 1 unless the result is 0 OF=1 iff there is a signed overflow
Exercise 1
Use only one instruction among AND, OR, XOR, and TEST to do the following task: (A) Convert the ASCII code of a decimal digit ('0 to '9) contained in AL to its numerical value. (B) Fix to 1 the odd numbered bits in EAX (ie: the bits numbered 1, 3, 5) without changing the even numbered bits. (C) Clear to 0 the most significant bit and the least significant bit of BH without changing the other bits. (D) Inverse the least significant bit of EBX without changing the other bits.
each bit is shifted one position to the left the lsb (least significant bit) is filled with 0 the msb (most significant bit) is moved into CF (so the previous content of CF is lost) dest can be either byte, word or dword
Example:
mov bx, 80h ; BX = 0080h shl bl, 1 ; BX = 0000h, CF=1 (only BL is affected)
Fast Multiplication
Each left shift multiplies by 2 the operand for both signed and unsigned interpretations. Ex:
mov mov shl shl ax, bx, ax, bx, 4 -1 2 3 ;AX ;BX ;AX ;BX = = = = 0004h FFFFh 0010h = 16 FFF8h = -8
Multiplication by shifting is very fast. Try to factor your multiplier into powers of 2: BX * 36 = BX * (32 + 4) = BX*32 + BX*4 So add (BX shifted by 5) to (BX shifted by 2)
11
the msb of dest is filled with 0 the lsb of dest is moved into CF
Each single-bit right shift divides the unsigned value by 2. Ex:
mov bh,13 ;BH = 0000 1101b = 13 shr bh,2 ;BH = 0000 0011b = 3 (div by 4),CF=0
the msb of dest is filled with its previous value (so the sign is preserved) the lsb of dest is moved into CF
mov ah, -15 sar ah, 1 ;AH = 1111 0001b ;AH = 1111 1000b = -8
the result is rounded to the smallest integer (-8 instead of -7) in contrast:
shr ah, 1 ;gives ah = 0111 1000b = 78h
13
ROR rotates the bits to the right (same syntax) CF gets a copy of the lsb
Examples of ROL
mov rol rol rol mov rol rol rol ah,40h ah,1 ah,1 ah,1 ;ah ;ah ;ah ;ah = = = = 0100 1000 0000 0000 0000b 0000b, CF = 0 0001b, CF = 1 0010b, CF = 0
15
Rotate with CF
RCL rotates to the left with participation of CF
16
17
Exercise 2
Give the binary content of AX immediately after the execution of the each instruction below (Consider that AX = 1011 0011 1100 1010b before each of these instructions): (A) SHL AL,2 ; AX = (B) SAR AH,2 ; AX = (C) ROR AX,4 ; AX = (D) ROL AX,3 ; AX = (E) SHL AL,8 ; AX =
18
In AL we have either 30h or 31h (ASCII code of 0 and 1) Hence, AND AL,0Fh converts AL to either 0h or 1h Hence, OR BL,AL possibly changes only the lsb of BL
20
} end Repeat
21
22