Conditional Loop Instructions Conditional Loop Instructions
• LOOPZ and LOOPE
• LOOPNZ and LOOPNE
LOOPZ and LOOPE 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.
LOOPNZ and LOOPNE 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.
LOOPNZ Example LOOPNZ Example
.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
The following code finds the first positive value in an array:
Your turn . . . Your turn . . .
.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
(fill in your code here)
Locate the first nonzero value in the array. If none is found, let
ESI point to the sentinel value:
. . . (solution) . . . (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 next ; continue loop
Conditional Structures Conditional Structures
• Block-Structured IF Statements
• Compound Expressions with AND
• Compound Expressions with OR
• WHILE Loops
• Table-Driven Selection
Block
Block - - Structured IF Statements Structured IF Statements
Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example:
mov eax,op1 cmp eax,op2 jne L1
mov X,1 jmp L2 L1: mov X,2 L2:
if( op1 == op2 ) X = 1;
else
X = 2;
Your turn . . . Your turn . . .
Implement the following pseudocode in assembly language. All values are unsigned:
if( ebx <= ecx ) {
eax = 5;
edx = 6;
}
Your turn . . . Your turn . . .
Implement the following pseudocode in assembly language. All values are 32-bit signed integers:
if( var1 <= var2 ) var3 = 10;
else {
var3 = 6;
var4 = 7;
}
Compound Expression with AND [1/3]
Compound Expression with AND [1/3]
• When implementing the logical AND operator, consider that HLLs use short-circuit evaluation
• In the following example, if the first expression is false, the second expression is skipped:
if (al > bl) AND (bl > cl) X = 1;
Compound Expression with AND [2/3]
Compound Expression with AND [2/3]
cmp al,bl ; first expression...
ja L1 jmp next L1:
cmp bl,cl ; second expression...
ja L2
if (al > bl) AND (bl > cl) X = 1;
This is one possible implementation . . .
Compound Expression with AND [3/3]
Compound Expression with AND [3/3]
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
if (al > bl) AND (bl > cl) X = 1;
But the following implementation uses 29% less code by
reversing the first relational operator.
Your turn . . . Your turn . . .
Implement the following pseudocode in assembly language. All values are unsigned:
if(ebx <= ecx) AND (ecx > edx )
{
eax = 5;
edx = 6;
}
Compound Expression with OR [1/2]
Compound Expression with OR [1/2]
• When implementing the logical OR operator, consider that HLLs use short-circuit evaluation
• In the following example, if the first expression is true, the second expression is skipped:
if (al > bl) OR (bl > cl) X = 1;
Compound Expression with OR [2/2]
Compound Expression with OR [2/2]
cmp al,bl ; is AL > BL?
ja L1 ; yes
cmp bl,cl ; no: is BL > CL?
jbe next ; no: skip next statement if (al > bl) OR (bl > cl)
X = 1;
We can use "fall-through" logic to keep the code as short as
possible:
WHILE Loops WHILE Loops
while( eax < ebx) eax = eax + 1;
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:
top: cmp eax,ebx ; check loop condition jae next ; false? exit loop
inc eax ; body of loop
jmp top ; repeat the loop
This is a possible implementation:
Your turn . . . Your turn . . .
while( ebx <= val1) {
ebx = ebx + 5;
val1 = val1 - 1 }
Implement the following loop, using unsigned 32-bit integers:
Table
Table - - Driven Selection [1/3] Driven Selection [1/3]
• Create a table containing lookup values and the offsets of labels or procedures
• Use a loop to search the table
Table
Table - - Driven Selection [2/3] Driven Selection [2/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'
Step 1: Create a table containing lookup values and procedure
offsets.
Table
Table - - Driven Selection [3/3] Driven Selection [3/3]
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
jmp L3 ; and exit the loop
L2: add ebx,EntrySize ; point to next entry
loop L1 ; repeat until ECX = 0
L3:
Step 2: Use a loop to search the table. When a match is found,
we call the procedure offset stored in the current table
entry.
Application: Finite
Application: Finite - - State Machines State Machines
• A finite-state machine (FSM) is a graph structure that changes state based on some input. Also called a state-transition diagram.
• We use a graph to represent an FSM, with squares or circles called
nodes,and lines with arrows between the circles called edges (or arcs).
• A FSM is a specific instance of a more general structure called a
directed graph(or digraph).
• Three basic states, represented by nodes:
• Start state
Finite
Finite - - State Machine 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:
• Provides visual tracking of program's flow of control
• Easy to modify
• Easily implemented in assembly language
FSM Examples FSM Examples
• FSM that recognizes strings beginning with 'x', followed by letters 'a'..'y', ending with 'z':
start 'x'
'a'..'y'
'z '
A B
C
• FSM that recognizes signed integers:
digit
Your turn . . . Your turn . . .
• Explain why the following FSM does not work as well for signed integers as the one shown on the previous slide:
start
digit
A +,- B
digit
Implementing an FSM 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
The following is code from State A in the Integer FSM:
Flowchart of State A
Flowchart of State A
StateAGetNext
AL = '+' ? true
AL = '-' ? true
ZF = 1 ? true IsDigit
false
false
false
StateB
StateB
StateC
State A accepts a plus or
minus sign, or a decimal
digit.
Runtime Expressions Runtime Expressions
.IF eax > ebx mov edx,1 .ELSE
mov edx,2 .ENDIF
• .IF, .ELSE, .ELSEIF, and .ENDIF can be used to create block-structured IF statements.
• Examples:
.IF eax > ebx && eax > ecx mov edx,1
.ELSE
mov edx,2 .ENDIF
Relational and Logical Operators
Relational and Logical Operators
MASM MASM - - Generated Code Generated Code
mov eax,6 cmp eax,val1 jbe @C0001 mov result,1
@C0001:
.data
val1 DWORD 5 result DWORD ? .code
mov eax,6
.IF eax > val1 mov result,1 .ENDIF
Generated code:
MASM MASM - - Generated Code Generated Code
mov eax,6 cmp eax,val1 jle @C0001 mov result,1
@C0001:
.data
val1 SDWORD 5 result SDWORD ? .code
mov eax,6
.IF eax > val1 mov result,1 .ENDIF
Generated code:
MASM automatically generates a signed jump (JLE).
.REPEAT Directive .REPEAT Directive
; Display integers 1 – 10:
mov eax,0 .REPEAT
inc eax
call WriteDec call Crlf
Executes the loop body before testing the loop condition associated with the .UNTIL directive.
Example:
.WHILE Directive .WHILE Directive
; Display integers 1 – 10:
mov eax,0
.WHILE eax < 10 inc eax
call WriteDec call Crlf
.ENDW