Chapt 06
Chapt 06
(c) Pearson Education, 2006-2007. All rights reserved. You may modify and copy this slide show for your personal use,
or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Chapter Overview
The Zero flag is set when the result of an operation equals zero.
AND Instruction
Performs a Boolean AND operation between each
pair of matching bits in two operands
Syntax:
AND destination, source
AND
00111011
AND 0 0 0 0 1 1 1 1
cleared
00001011
unchanged
OR Instruction
Performs a Boolean OR operation between each pair
of matching bits in two operands
Syntax:
OR destination, source
OR
00111011
OR 0 0 0 0 1 1 1 1
unchanged
00111111
set
XOR Instruction
Performs a Boolean exclusive-OR operation between
each pair of matching bits in two operands
Syntax:
XOR
00111011
XOR 0 0 0 0 1 1 1 1
unchanged
00110100
inverted
NOT Instruction
Performs a Boolean NOT operation on a single
destination operand
Syntax:
NOT
NOT destination
NOT
00111011
11000100
inverted
Applications
(1 of 5)
; AL = 01100001b
; AL = 01000001b
Applications
(2 of 5)
; AL = 00000110b
; AL = 00110110b
10
Applications
(3 of 5)
; BIOS segment
; keyboard flag byte
; CapsLock on
11
Applications
(4 of 5)
12
Applications
(5 of 5)
ORing any number with itself does not change its value.
13
TEST Instruction
14
CMP Instruction
(1 of 3)
15
CMP Instruction
(2 of 3)
; ZF = 0, CF = 0
16
CMP Instruction
(3 of 3)
17
What's Next
18
Conditional Jumps
Jumps Based On . . .
Specific flags
Equality
Unsigned comparisons
Signed Comparisons
Applications
Encrypting a String
Bit Test (BT) Instruction
19
Jcond Instruction
A conditional jump instruction branches to a label
when specific register or flag conditions are met
Examples:
20
Jcond Ranges
Prior to the 386:
jump must be within 128 to +127 bytes from current
location counter
IA-32 processors:
32-bit offset permits jump anywhere in memory
21
22
23
24
25
Applications
(1 of 5)
26
Applications
(2 of 5)
; below or equal
27
Applications
(3 of 5)
Large,bx
ax,bx
Next
Large,ax
Small,ax
bx,ax
Next
Small,bx
28
Applications
(4 of 5)
29
Applications
(5 of 5)
30
Your turn . . .
Write code that jumps to label L1 if either bit 4, 5, or 6
is set in the BL register.
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
parity.
Write code that jumps to label L3 if EAX is negative.
Write code that jumps to label L4 if the expression
(EBX ECX) is greater than zero.
31
Encrypting a String
The following loop uses the XOR instruction to transform every
character in a string into a new value.
KEY = 239
; can be any byte value
BUFMAX = 128
.data
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD BUFMAX
.code
mov ecx,bufSize
mov esi,0
L1:
xor buffer[esi],KEY
inc esi
loop L1
; loop counter
; index 0 in buffer
; translate a byte
; point to next byte
32
33
; CF = bit 9
; jump if Carry
34
What's Next
35
36
37
38
LOOPNZ Example
The following code finds the first positive value in an array:
.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:
39
Your turn . . .
Locate the first nonzero value in the array. If none is found, let
ESI point to the sentinel value:
.data
array SWORD 50 DUP(?)
sentinel SWORD 0FFFFh
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
L1: cmp WORD PTR [esi],0
quit:
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.
40
. . . (solution)
.data
array SWORD 50 DUP(?)
sentinel SWORD 0FFFFh
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
L1: cmp WORD PTR [esi],0
pushfd
add esi,TYPE array
popfd
loope L1
jz quit
sub esi,TYPE array
quit:
41
What's Next
42
Conditional Structures
Block-Structured IF Statements
Compound Expressions with AND
Compound Expressions with OR
WHILE Loops
Table-Driven Selection
43
Block-Structured IF Statements
Assembly language programmers can easily translate logical
statements written in C++/Java into assembly language. For
example:
if( op1 == op2 )
X = 1;
else
X = 2;
mov
cmp
jne
mov
jmp
L1: mov
L2:
eax,op1
eax,op2
L1
X,1
L2
X,2
44
Your turn . . .
Implement the following pseudocode in assembly
language. All values are unsigned:
cmp
ja
mov
mov
next:
ebx,ecx
next
eax,5
edx,6
45
Your turn . . .
Implement the following pseudocode in assembly
language. All values are 32-bit signed integers:
if( var1
var3 =
else
{
var3 =
var4 =
}
<= var2 )
10;
6;
7;
mov
cmp
jle
mov
mov
jmp
L1: mov
L2:
eax,var1
eax,var2
L1
var3,6
var4,7
L2
var3,10
46
(1 of 3)
47
(2 of 3)
; first expression...
cmp bl,cl
ja L2
jmp next
; second expression...
L1:
L2:
mov X,1
next:
48
(3 of 3)
al,bl
next
bl,cl
next
X,1
;
;
;
;
;
first expression...
quit if false
second expression...
quit if false
both are true
49
Your turn . . .
Implement the following pseudocode in assembly
language. All values are unsigned:
cmp
ja
cmp
jbe
mov
mov
next:
ebx,ecx
next
ecx,edx
next
eax,5
edx,6
50
(1 of 2)
51
(1 of 2)
al,bl
L1
bl,cl
next
X,1
;
;
;
;
;
is AL > BL?
yes
no: is BL > CL?
no: skip next statement
set X to 1
52
WHILE Loops
A WHILE loop is really an IF statement followed by the body
of the loop, followed by an unconditional jump to the top of
the loop. Consider the following example:
while( eax < ebx)
eax = eax + 1;
eax,ebx
next
eax
top
;
;
;
;
53
Your turn . . .
Implement the following loop, using unsigned 32-bit integers:
while( ebx <= val1)
{
ebx = ebx + 5;
val1 = val1 - 1
}
top: cmp
ja
add
dec
jmp
next:
ebx,val1
next
ebx,5
val1
top
54
Table-Driven Selection
(1 of 3)
55
Table-Driven Selection
(2 of 3)
56
Table-Driven Selection
(3 of 3)
match found?
no: continue
yes: call the procedure
and exit the loop
point to next entry
repeat until ECX = 0
L3:
required for
procedure pointers
57
What's Next
58
Start state
Terminal state(s)
Nonterminal state(s)
59
Finite-State Machine
Accepts any sequence of symbols that puts it into an
accepting (final) state
Can be used to recognize, or validate a sequence of
characters that is governed by language rules (called a regular
expression)
Advantages:
60
FSM Examples
'x'
B
'z
'
C
digit
start
+,-
digit
61
Your turn . . .
Explain why the following FSM does not work as well
for signed integers as the one shown on the previous
slide:
digit
digit
start
+,-
62
Implementing an FSM
The following is code from State A in the Integer FSM:
StateA:
call Getnext
cmp al,'+'
je StateB
cmp al,'-'
je StateB
call IsDigit
jz StateC
call DisplayErrorMsg
jmp Quit
;
;
;
;
;
;
;
;
63
IsDigit Procedure
Receives a character in AL. Sets the Zero flag if the character
is a decimal digit.
IsDigit PROC
cmp
al,'0'
jb
ID1
cmp
al,'9'
ja
ID1
test ax,0
ID1: ret
IsDigit ENDP
; ZF = 0
; ZF = 0
; ZF = 1
64
Flowchart of State A
StateA
GetNext
AL = '+' ?
true
StateB
false
AL = '-' ?
true
StateB
false
IsDigit
ZF = 1 ?
true
StateC
false
DisplayErrorMsg
quit
65
Your turn . . .
Draw a FSM diagram for hexadecimal integer
constant that conforms to MASM syntax.
Draw a flowchart for one of the states in your FSM.
Implement your FSM in assembly language. Let the
user input a hexadecimal constant from the keyboard.
66
What's Next
67
Runtime Expressions
Relational and Logical Operators
MASM-Generated Code
.REPEAT Directive
.WHILE Directive
68
Runtime Expressions
.IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate
runtime expressions and create block-structured IF
statements.
Examples:
.IF eax > ebx
mov edx,1
.ELSE
mov edx,2
.ENDIF
69
70
MASM-Generated Code
.data
val1
DWORD 5
result DWORD ?
.code
mov eax,6
.IF eax > val1
mov result,1
.ENDIF
Generated code:
mov eax,6
cmp eax,val1
jbe @C0001
mov result,1
@C0001:
71
MASM-Generated Code
.data
val1
SDWORD 5
result SDWORD ?
.code
mov eax,6
.IF eax > val1
mov result,1
.ENDIF
Generated code:
mov eax,6
cmp eax,val1
jle @C0001
mov result,1
@C0001:
72
MASM-Generated Code
.data
result DWORD ?
.code
mov ebx,5
mov eax,6
.IF eax > ebx
mov result,1
.ENDIF
Generated code:
mov ebx,5
mov eax,6
cmp eax,ebx
jbe @C0001
mov result,1
@C0001:
73
MASM-Generated Code
.data
result SDWORD ?
.code
mov ebx,5
mov eax,6
.IF SDWORD PTR eax > ebx
mov result,1
.ENDIF
Generated code:
mov ebx,5
mov eax,6
cmp eax,ebx
jle @C0001
mov result,1
@C0001:
74
.REPEAT Directive
Executes the loop body before testing the loop condition
associated with the .UNTIL directive.
Example:
; Display integers 1 10:
mov eax,0
.REPEAT
inc eax
call WriteDec
call Crlf
.UNTIL eax == 10
75
.WHILE Directive
Tests the loop condition before executing the loop body
The .ENDW directive marks the end of the loop.
Example:
; Display integers 1 10:
mov eax,0
.WHILE eax < 10
inc eax
call WriteDec
call Crlf
.ENDW
76
Summary
77