AND
- Performs AND on each bit of source and destination operands
- Result is stored on destination operand
AND reg,reg
AND reg,mem
AND reg,imm
AND mem,reg
AND mem,imm
- Example:
mov al,0000001b ; al = 0000001b
and al,0000000b ; al = 0000000b
- Can be used to mask bits (leave them unchanged)
and AL,11110110b ; clear bits 0 and 3, leave others unchanged
- Flags being modified:
- Always clears overflow and carry flags
- Modifies Sign, Zero, and Parity flags
OR
- Similar to AND but performs OR instead
OR reg,reg
OR reg,mem
OR reg,imm
OR mem,reg
OR mem,imm
- Can be used to modify a single bit leaving others unchanged
mov al,00000000b
or al,00000100b ; set bit 2, leave others unchanged
- Here are the flags being modified (assuming AL is being modified)
XOR
- Can be used as bit flippers (for symmetric encryption) - reversible property
- Can be used whether a byte has even parity (parity flag set, even numbers of 1’s) or odd parity (parity flag clear, odd numbers of 1’s)
mov al,10110101b ; 5 bits = odd parity
xor al,0 ; Parity flag clear (odd)
mov al,11001100b ; 4 bits = even parity
xor al,0 ; Parity flag set (even)
- Can be also used to check parity of a 16-bit integer - 16-bit parity
mov ax,64C1h ; 0110 0100 1100 0001
xor ah,al ; Parity flag set (even)
- Doing XOR on exactly same integer results to 0 (XOR’ing to yourself). That means that it will set parity flag since the results is all 0’s (even number of 1’s).
mov al,22h ; al = 22h
xor al,22h ; al = 0
NOT
- Inverts bits
NOT reg
NOT mem
- Doesn’t affect any flags
- Example:
mov al,11110000b
not al ; AL = 00001111b
Test
- Performs an implied AND (doesn’t modify the destination operand)
- Example:
test al,00001001b ; test bits 0 and 3
- Can be used to check if a bit is set (can also work on multiple bits) set - Modifies zero flag
; Example for testing status of a device using but 5
mov al,status
; If bit 5 in status is set, this will clear zero flag.
; Otherwise it will set zero flag.
test al,00100000b
jnz DeviceOffline
; Can be used also on multiple bits 0, 1 and 4
test al,00010011b
jnz InputDataByte
- Can be used to test if an integer is even or odd
mov eax,18
test eax,1 ; ZF = 1
mov eax,17
test eax,1 ; ZF = 0
CMP
- Performs implied substraction between source and destination operands (both are not modified)
CMP destination,source
- Modifies zero, carry and sign flags
- Example:
; Let’s look at three code fragments showing how flags are affected by the CMP
; instruction. When AX equals 5 and is compared to 10, the Carry flag is set
; because subtracting 10 from 5 requires a borrow
mov ax,5
cmp ax,10 ; ZF = 0 and CF = 1
; Comparing 1000 to 1000 sets the Zero flag because subtracting the source from
; the
destination produces zero:
mov ax,1000
mov cx,1000
cmp cx,ax ; ZF = 1 and CF = 0
; Comparing 105 to 0 clears both the Zero and Carry flags because subtracting 0
; from
105 generates a positive, nonzero value
mov si,105
cmp si,0 ; ZF = 0 and CF = 0
Manipulating individual CPU flags
We can manipulate (set or clear) cpu flags using the boolean and comparison instructions. Here are some examples.
- Zero flag
test al,0 ; set Zero flag
and al,0 ; set Zero flag
or al,1 ; clear Zero flag
- Sign flag - operates against the highest bit of the destination operand
or al,80h ; set Sign flag
and al,7Fh ; clear Sign flag
- Carry flag
stc ; set Carry flag
clc ; clear Carry flag
- Overflow flag
mov al,7Fh ; AL = +127
inc al ; AL = 80h (-128), OF=1
or eax,0 ; clear Overflow flag
How do we mimic conditional statements like in higher level languages?
Here is an example program that finds the large value between 2 integers.
; filename: LargerOfTwoIntegers.asm
mov edx,eax ; assume EAX is larger
cmp eax,ebx ; if EAX is >= EBX
jae L1 ; jump to L1
mov edx,ebx ; else move EBX to EDX
L1: ; EDX contains the larger integer
...
No comments:
Post a Comment