Assembly Language For Intel-Based Computers, 4 Edition Boolean and Comparison Instructions
Assembly Language For Intel-Based Computers, 4 Edition Boolean and Comparison Instructions
Assembly Language For Intel-Based Computers, 4 Edition Boolean and Comparison Instructions
1
AND Instruction XOR Instruction
• Performs a Boolean AND operation between each • Performs a Boolean exclusive-OR operation between
pair of matching bits in two operands each pair of matching bits in two operands
• Syntax: • Syntax:
XOR destination, source XOR
AND destination, source
AND
(same operand types as MOV)
00111011
XOR 00001111
00111011
AND 0 0 0 0 1 1 1 1 unchanged 00110100 inverted
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 5 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 7
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 6 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 8
2
Applications (1 of 5) Applications (4 of 5)
• Task: Convert the character in AL to upper case. • Task: Jump to a label if an integer is even.
• Solution: Use the AND instruction to clear bit 5. • Solution: AND the lowest bit with a 1. If the result is Zero,
the number was even.
mov al,'a' ; AL = 01100001b
and al,11011111b ; AL = 01000001b mov ax,wordVal
and ax,1 ; low bit set?
jz EvenValue ; jump if Zero flag set
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 11
Applications (2 of 5) Applications (5 of 5)
• Task: Convert a binary decimal byte into its equivalent • Task: Jump to a label if the value in AL is not zero.
ASCII decimal digit.
• Solution: OR the byte with itself, then use the JNZ (jump
• Solution: Use the OR instruction to set bits 4 and 5. if not zero) instruction.
ORing any number with itself does not change its value.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 12
3
TEST Instruction CMP Instruction (2 of 3)
test al,00000011b
jz ValueNotFound
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 13 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 15
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 14 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16
4
Setting and Clearing Individual CPU Flags
Summary of FLAGs (cont.)
CMP Results ZF CF
• Carry flag
destination < source 0 1 • Set carry flag: use STC (set carry)
destination > source 0 0 • Clear carry flag: use CLC (clear carry)
• Overflow flag
destination=source 1 0
• Set overflow flag
If two operands are signed, then we have following table • Add two positive byte values that produce a negative
sum
CMP Results Flags • Clear overflow flag: OR with 0
• EX: OR eax, 0
Destination < source SF != OF
Destination = source ZF = 1
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 20
5
Jcond Instruction Jumps Based on Equality
• A conditional jump instruction branches to a label
when specific register or flag conditions are met
• Examples:
• JB, JC jump to a label if the Carry flag is set
• JE, JZ jump to a label if the Zero flag is set
• JS jumps to a label if the Sign flag is set
• JNE, JNZ jump to a label if the Zero flag is clear
• JECXZ jumps to a label if ECX equals 0
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 24
6
Jumps Based on Signed Comparisons Applications (2 of 5)
cmp eax,Val1
jbe L1 ; below or equal
cmp eax,Val1
jle L1
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27
Applications (1 of 5) Applications (3 of 5)
• Task: Jump to a label if unsigned EAX is greater than EBX • Compare unsigned AX to BX, and copy the larger of the two
into a variable named Large
• Solution: Use CMP, followed by JA
mov Large,bx
cmp eax,ebx cmp ax,bx
ja Larger jna Next
mov Large,ax
Next:
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 28
7
Applications (4 of 5) Your turn . . .
• Jump to label L1 if the memory word pointed to by ESI equals
Zero • Write code that jumps to label L1 if either bit 4, 5, or 6
cmp WORD PTR [esi],0
is set in the BL register.
je L1 • Write code that jumps to label L1 if bits 4, 5, and 6
are all set in the BL register.
• Write code that jumps to label L2 if AL has even
• Jump to label L2 if the doubleword in memory pointed to by
parity.
EDI is even
• Write code that jumps to label L3 if EAX is negative.
test DWORD PTR [edi],1
jz L2
• Write code that jumps to label L4 if the expression
(EBX – ECX) is greater than zero.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 29 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 31
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 30 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 32
8
String Encryption Program 6.4 Conditional Loop Instructions
• Tasks:
• Input a message (string) from the user
• Encrypt the message • LOOPZ and LOOPE
• Display the encrypted message • LOOPNZ and LOOPNE
• Decrypt the message
• Display the decrypted message
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 33 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 35
• Copies bit n from an operand into the Carry flag • LOOPZ: loop if zero
• Syntax: BT bitBase, n • LOOPE: loop if equal
• bitBase may be r/m16 or r/m32 • Syntax:
• n may be r16, r32, or imm8 LOOPE destination
LOOPZ destination
• Example: jump to label L1 if bit 9 is set in the AX
register: • Logic:
• ECX ¬ ECX – 1
bt AX,9 ; CF = bit 9 • if ECX > 0 and ZF=1, jump to destination
jc L1 ; jump if Carry
• Useful when scanning an array for the first element
that does not match a given value.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 34 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 36
9
LOOPNZ and LOOPNE Your turn . . .
Locate the first nonzero value in the array. If none is found, let
• LOOPNZ (LOOPNE) is a conditional loop instruction ESI point to the sentinel value:
• Syntax:
.data
LOOPNZ destination array SWORD 50 DUP(?)
LOOPNE destination sentinel SWORD 0FFFFh
.code
• Logic: mov esi,OFFSET array
• ECX ¬ ECX – 1; mov ecx,LENGTHOF array
• if ECX > 0 and ZF=0, jump to destination L1: cmp WORD PTR [esi],0 ; check for zero
quit:
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 37 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 39
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 38 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 40
10
6.5 Conditional Structures Your turn . . .
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 41 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 43
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 42 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 44
11
Compound Expression with AND (1 of 3) Compound Expression with AND (3 of 3)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 45 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 47
if (al > bl) AND (bl > cl) Implement the following pseudocode in assembly
X = 1; language. All values are unsigned:
12
Compound Expression with OR (1 of 2) WHILE Loops
• When implementing the logical OR operator, consider that HLLs use A WHILE tests a condition first before performing a block of
short-circuit evaluation statements. Consider the following example:
• In the following example, if the first expression is true, the second
expression is skipped:
while( eax < ebx)
eax = eax + 1;
if (al > bl) OR (bl > cl)
X = 1; This is a possible implementation:
top: cmp eax,ebx ; check loop condition
jae next ; false? exit loop
inc eax ; body of loop
jmp top ; repeat the loop
next:
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 49 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 51
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 50 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 52
13
Table-Driven Selection (1 of 3) Table-Driven Selection (3 of 3)
L3:
required for
procedure pointers
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 53 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 55
Table-Driven Selection (2 of 3)
.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
14