AND destination, source
(same operand types as MOV)OR destination, source
XOR destination, source
assembly
mov eax,SetX
not eax
assembly
mov eax,setX
and eax,setY
assembly
mov eax,setX
or eax,setY
assembly
mov al,'a' ; AL = 01100001b
and al,11011111b ; AL = 01000001b
assembly
mov al,6 ; AL = 00000110b
or al,00110000b ; AL = 00110110b
assembly
mov ax,40h ; BIOS segment
mov ds,ax
mov bx,17h ; keyboard flag byte
or BYTE PTR [bx],01000000b ; CapsLock on
assembly
mov ax,wordVal
and ax,1 ; low bit set?
jz EvenValue ; jump if Zero flag set
assembly
or al,al
jnz IsNotZero ; jump if not zero
assembly
test al,00000011b
jnz ValueFound
assembly
test al,00000011b
jz ValueNotFound
CMP destination, source
assembly
mov al,5
cmp al,5 ; Zero flag set
assembly
mov al,4
cmp al,5 ; Carry flag set
assembly
mov al,6
cmp al,5 ; ZF = 0, CF = 0 (both the Zero and Carry flags are clear)
assembly
mov al,5
cmp al,-2 ; Sign flag == Overflow flag
assembly
mov al,-1
cmp al,5 ; Sign flag != Overflow flag
JB, JC
- jump to a label if the Carry flag is setJE, JZ
- jump to a label if the Zero flag is setJS
- jump to a label if the Sign flag is setJNE, JNZ
- jump to a label if the Zero flag is clearJECXZ
- jump to a label if ECX = 0JZ
: Jump if zero, ZF = 1JNZ
: Jump if not zero, ZF = 0JC
: Jump if carry, CF = 1JNC
: Jump if not carry, CF = 0JO
: Jump if overflow, OF = 1JNO
: Jump if not overflow, OF = 0JS
: Jump if signed, SF = 1JNS
: Jump if not signed, SF = 0JP
: Jump if parity (even), PF = 1JNP
: Jump if not parity (odd), PF = 0JE
: Jump if equal (leftOp = rightOp)JNE
: Jump if not equal (leftOp != rightOp)JCXZ
: Jump if CX=0JECXZ
: Jump if ECX=0JA / JNBE
: Jump if above (if leftOp > rightOp)JAE / JNB
: Jump if above or equal (if leftOp >= rightOp)JB / JNAE
: Jump if below (if leftOp < rightOp)JBE / JNA
: Jump if below or equal (if leftOp <= rightOp)JG / JNLE
: Jump if greater (if leftOp > rightOp)JGE / JNL
: Jump if greater than or equal (if leftOp >= rightOp)JL / JNGE
: Jump if less (if leftOp < rightOp)JLE / JNG
: Jump if less than or equal (if leftOp <= rightOp)assembly
cmp eax,ebx
ja Larger
assembly
cmp eax,ebx
jg Greater
assembly
cmp eax,Val1
jbe L1 ; below or equal
assembly
cmp eax,Val1
jle L1
assembly
mov Large,bx
cmp ax,bx
jna Next
mov Large,ax
Next:
assembly
mov Small,ax
cmp bx,ax
jnl Next
mov Small,bx
Next:
assembly
cmp WORD PTR [esi],0
je L1
assembly
test DWORD PTR [edi],1
jz L2
assembly
and al,00001011b ; clear unwanted bits
cmp al,00001011b ; check remaining bits
je L1 ; all set? jump to L1
assembly
KEY = 239 ; can be any byte value
BUFMAX = 128
.data
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD BUFMAX
.code
mov ecx,bufSize; loop counter
mov esi,0; index 0 in buffer
L1:
xor buffer[esi],KEY; translate a byte
inc esi; point to next byte
loop L1
text
Enter the plain text: Attack at dawn.
Cipher text: «¢¢Äîä-Ä¢-ïÄÿü-Gs
Decrypted: Attack at dawn.
BT bitBase, n
bitBase
may be r/m16
or r/m32
n
may be r16
, r32
, or imm8
assembly
bt AX,9 ; CF = bit 9
jc L1 ; jump if Carry
LOOPE destination
LOOPZ destination
ECX ¬ ECX – 1
ECX > 0
and ZF=1
, jump to destinationLOOPNZ destination
LOOPNE destination
ECX ¬ ECX – 1;
ECX > 0
and ZF=0
, jump to destinationassembly
.data
array SWORD -3,-6,-1,-10,10,30,40,4
sentinel SWORD 0
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
next:
test WORD PTR [esi],8000h; test sign bit
pushfd; push flags on stack
add esi,TYPE array
popfd; pop flags from stack
loopnz next; continue loop
jnz quit; none found
sub esi,TYPE array; ESI points to value
quit:
c++
if( op1 == op2 )
X = 1;
else
X = 2;
assembly
mov eax,op1
cmp eax,op2
jne L1
mov X,1
jmp L2
L1:
mov X,2
L2:
c++
if (al > bl) AND (bl > cl)
X = 1;
assembly
cmp al,bl ; first expression...
ja L1
jmp next
L1:
cmp bl,cl ; second expression...
ja L2
jmp next
L2:
; both are true
mov X,1 ; set X to 1
next:
;...
assembly
cmp al,bl ; first expression...
jbe next ; quit if false
cmp bl,cl ; second expression...
jbe next ; quit if false
mov X,1 ; both are true
next:
;...
c++
if (al > bl) OR (bl > cl)
X = 1;
assembly
cmp al,bl ; is AL > BL?
ja L1 ; yes
cmp bl,cl ; no: is BL > CL?
jbe next ; no: skip next statement
L1:
mov X,1 ; set X to 1
next:
;...
c++
while( eax < ebx)
eax = eax + 1;
assembly
top:
cmp eax,ebx ; check loop condition
jae next ; false? exit loop
inc eax ; body of loop
jmp top ; repeat the loop
next:
;...
assembly
.data
CaseTable BYTE 'A' ; lookup value
DWORD Process_A ; address of procedure
EntrySize = ($ - CaseTable)
BYTE 'B'
DWORD Process_B
BYTE 'C'
DWORD Process_C
BYTE 'D'
DWORD Process_D
NumberOfEntries = ($ - CaseTable) / EntrySize
assembly
mov ebx,OFFSET CaseTable ; point EBX to the table
mov ecx,NumberOfEntries ; loop counter
L1:
cmp al,[ebx] ; match found?
jne L2 ; no: continue
call NEAR PTR [ebx + 1] ; yes: call the procedure
call WriteString ; display message
call Crlf
jmp L3 ; and exit the loop
L2:
add ebx,EntrySize ; point to next entry
loop L1 ; repeat until ECX = 0
L3:
;...
assembly
StateA:
call Getnext ; read next char into AL
cmp al,'+' ; leading + sign?
je StateB ; go to State B
cmp al,'-' ; leading - sign?
je StateB ; go to State B
call IsDigit ; ZF = 1 if AL = digit
jz StateC ; go to State C
call DisplayErrorMsg ; invalid input found
jmp Quit
assembly
IsDigit PROC
cmp al,'0' ; ZF = 0
jb ID1
cmp al,'9' ; ZF = 0
ja ID1
test ax,0 ; ZF = 1
ID1:
ret
IsDigit ENDP
.REPEAT
Directive.WHILE
Directive.IF
, .ELSE
, .ELSEIF
, and .ENDIF
can be used to evaluate runtime expressions and create block-structured IF statements.assembly
.IF eax > ebx
mov edx,1
.ELSE
mov edx,2
.ENDIF
assembly
.IF eax > ebx && eax > ecx
mov edx,1
.ELSE
mov edx,2
.ENDIF
expr1 == expr2
: Returns true when expr1 is equal to expr2.expr1 != expr2
: Returns true when expr1 is not equal to expr2.expr1 > expr2
: Returns true when expr1 is greater than expr2.expr1 >= expr2
: Returns true when expr1 is greater than or equal to expr2.expr1 < expr2
: Returns true when expr1 is less than expr2.expr1 <= expr2
: Returns true when expr1 is less than or equal to expr2.! expr
: Returns true when expr is false.expr1 && expr2
: Performs logical AND between expr1 and expr2.expr1 || expr2
: Performs logical OR between expr1 and expr2.expr1 & expr2
: Performs bitwise AND between expr1 and expr2.CARRY?
: Returns true if the Carry flag is set.OVERFLOW?
: Returns true if the Overflow flag is set.PARITY?
: Returns true if the Parity flag is set.SIGN?
: Returns true if the Sign flag is set.ZERO?
: Returns true if the Zero flag is set.assembly
.data
val1 DWORD 5
result DWORD ?
.code
mov eax,6
.IF eax > val1
mov result,1
.ENDIF
assembly
mov eax,6
cmp eax,val1
jbe @C0001
mov result,1
@C0001:
assembly
.data
val1 SDWORD 5
result SDWORD ?
.code
mov eax,6
.IF eax > val1
mov result,1
.ENDIF
assembly
mov eax,6
cmp eax,val1
jle @C0001
mov result,1
@C0001:
assembly
.data
result DWORD ?
.code
mov ebx,5
mov eax,6
.IF eax > ebx
mov result,1
.ENDIF
assembly
mov ebx,5
mov eax,6
cmp eax,ebx
jbe @C0001
mov result,1
@C0001:
assembly
.data
result SDWORD ?
.code
mov ebx,5
mov eax,6
.IF SDWORD PTR eax > ebx
mov result,1
.ENDIF
assembly
mov eax,0
.REPEAT
inc eax
call WriteDec
call Crlf
.UNTIL eax == 10
assembly
mov eax,0
.WHILE eax < 10
inc eax
call WriteDec
call Crlf
.ENDW