Class 14
Class 14
38
Conditional Loop Instructions
39
LOOPZ and LOOPE
• Syntax:
LOOPE destination
LOOPZ destination
• Logic:
• ECX ← ECX – 1
• if ECX > 0 and ZF=1, jump to destination
• Useful when scanning an array for the first element
that does not match a given value.
40
LOOPNZ and LOOPNE
• LOOPNZ (LOOPNE) is a conditional loop instruction
• Syntax:
LOOPNZ destination
LOOPNE destination
• Logic:
• ECX ← ECX – 1;
• if ECX > 0 and ZF=0, jump to destination
• Useful when scanning an array for the first element
that matches a given value.
41
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:
42
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 ; check for zero
quit:
43
. . . (solution)
.data
array SWORD 50 DUP(?)
sentinel SWORD 0FFFFh
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
L1: cmp WORD PTR [esi],0 ; check for zero
pushfd ; push flags on stack
add esi,TYPE array
popfd ; pop flags from stack
loope L1 ; continue loop
jz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:
44
What's Next
45
Conditional Structures
• Block-Structured IF Statements
• Compound Expressions with AND
• Compound Expressions with OR
• WHILE Loops
• Table-Driven Selection
46
Block-Structured IF Statements
Assembly language programmers can easily translate logical
statements written in C++/Java into assembly language. For
example:
47
Implement the following pseudocode in assembly
language. All values are unsigned:
48
Implement the following pseudocode in assembly
language. All values are 32-bit signed integers:
49
Compound Expression with AND (1 of 3)
50
Compound Expression with AND (2 of 3)
51
Compound Expression with AND (3 of 3)
52
Implement the following pseudocode in assembly
language. All values are unsigned:
54
Compound Expression with OR (2 of 2)
55
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:
56
Implement the following loop, using unsigned 32-bit integers:
57
Table-Driven Selection (1 of 4)
58
Table-Driven Selection (2 of 4)
.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
59
Table-Driven Selection (3 of 4)
60
Table-Driven Selection (4 of 4)
61
What's Next
62
Application: Finite-State Machines
63
Application: Finite-State Machines
64
Finite-State Machine
65
Finite-State Machine Examples
• FSM that recognizes strings beginning with 'x', followed by
letters 'a'..'y', ending with 'z':
'a'..'y'
start 'x'
A B
'z
'
C
digit
digit
start +,-
A B
66
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 +,-
A B
67
Implementing an FSM
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
68
IsDigit Procedure
IsDigit PROC
cmp al,'0' ; ZF = 0
jb ID1
cmp al,'9' ; ZF = 0
ja ID1
test al,0 ; ZF = 1
ID1: ret
IsDigit ENDP
69
StateA
Flowchart of State A
GetNext
true
AL = '+' ? StateB
false
digit. false
IsDigit
true
ZF = 1 ? StateC
false
DisplayErrorMsg
quit
70
• 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.
71
What's Next
72
Creating IF Statements
• Runtime Expressions
• Relational and Logical Operators
• MASM-Generated Code
• .REPEAT Directive
• .WHILE Directive
73
Runtime Expressions
• .IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate
runtime expressions and create block-structured IF
statements.
• Examples:
.IF eax > ebx .IF eax > ebx && eax > ecx
mov edx,1 mov edx,1
.ELSE .ELSE
mov edx,2 mov edx,2
.ENDIF .ENDIF
74
Relational and Logical Operators
75
Signed and Unsigned Comparisons
.data
val1 DWORD 5
result DWORD ? Generated code:
.code
mov eax,6
mov eax,6 cmp eax,val1
.IF eax > val1 jbe @C0001
mov result,1 mov result,1
.ENDIF
@C0001:
76
Signed and Unsigned Comparisons
.data
val1 SDWORD 5
result SDWORD ? Generated code:
.code
mov eax,6
mov eax,6 cmp eax,val1
.IF eax > val1 jle @C0001
mov result,1 mov result,1
.ENDIF
@C0001:
77
Signed and Unsigned Comparisons
.data
result DWORD ? Generated code:
.code
mov ebx,5
mov ebx,5 mov eax,6
mov eax,6 cmp eax,ebx
.IF eax > ebx jbe @C0001
mov result,1
mov result,1
@C0001:
.ENDIF
78
Signed and Unsigned Comparisons
.data
result SDWORD ? Generated code:
.code
mov ebx,5
mov ebx,5 mov eax,6
mov eax,6 cmp eax,ebx
.IF SDWORD PTR eax > ebx jle @C0001
mov result,1
mov result,1
@C0001:
.ENDIF
79
.REPEAT Directive
Executes the loop body before testing the loop condition
associated with the .UNTIL directive.
Example:
mov eax,0
.REPEAT
inc eax
call WriteDec
call Crlf
.UNTIL eax == 10
80
.WHILE Directive
Tests the loop condition before executing the loop body The
.ENDW directive marks the end of the loop.
Example:
mov eax,0
.WHILE eax < 10
inc eax
call WriteDec
call Crlf
.ENDW
81
Summary
• Bitwise instructions (AND, OR, XOR, NOT, TEST)
• manipulate individual bits in operands
• CMP – compares operands using implied subtraction
• sets condition flags
• Conditional Jumps & Loops
• equality: JE, JNE
• flag values: JC, JZ, JNC, JP, ...
• signed: JG, JL, JNG, ...
• unsigned: JA, JB, JNA, ...
• LOOPZ, LOOPNZ, LOOPE, LOOPNE
• Flowcharts – logic diagramming tool
• Finite-state machine – tracks state changes at runtime
82