conditional jump, conditional loop instructions, and conditional structures
DESCRIPTION
Conditional Jump, Conditional Loop Instructions, and Conditional Structures. Type of conditional jump instruction Based on specific flag Based on equality between operands, or value of (E)cx. Based on comparisons of unsigned operands. Based on comparison of signed operands. - PowerPoint PPT PresentationTRANSCRIPT
Sahar Mosleh California State University San Marcos Page 1
Conditional Jump,Conditional Loop Instructions,
andConditional Structures
Sahar Mosleh California State University San Marcos Page 2
Type of conditional jump instruction
• Based on specific flag
• Based on equality between operands, or value of (E)cx.
• Based on comparisons of unsigned operands.
• Based on comparison of signed operands.
Mnemonic Description Flags
JZ Jump if zero ZF = 1
JNZ Jump if not zreo ZF = 0
JC Jump if carry CF = 1
JNC Jump if not carry CF = 0
JO Jump if overflow OF = 1
JNO Jump if not overflow OF = 0
JS Jump if signed SF = 1
JNS Jump if not signed SF = 0
JP Jump if parity (even) PF = 1
JNP Jump if not parity (odd) PF = 0
Sahar Mosleh California State University San Marcos Page 3
Equality comparisons
• Based on equality between operands, or value of (E)cx.
CMP destination, Source
Mnemonic Description
JE Jump if destination = source
JNE Jump if not equal
JCXZ Jump if CX=0
JECXZ Jump if ECX = 0
Sahar Mosleh California State University San Marcos Page 4
Unsigned Comparisons
• Jumps based on comparisons of unsigned integers are useful when comparing unsigned values, such as 7FFh and 8000h, where 7FFh is smaller than 8000h latter.
CMP destination, SourceMnemonic Description
JA Jump if above (destination > source)
JNBE Jump if not below or equal (same as JA)
JAE Jump if above or equal (destination >= source)
JNB Jump if not below ( same as JAE)
JB Jump if below
JNAE Jump if not above or equal ( same as JB)
JBE Jump if below or equal ( destination <= source)
JNA Jump if not above ( same as JBE)
Sahar Mosleh California State University San Marcos Page 5
Signed Comparison• It is used when the numbers you are comparing can be interpreted as
signed values. CMP destination, Source
• Example: mov al,7Fh ; (7Fh or +127)Cmp al,80h ; (80h or -128 )
Ja Isabove ; no: 7Fh not> 80 hJg isGreater ; yes: + 127 > -128
Mnemonic Description
JG Jump if Greater (destination > source)
JNLE Jump if not less than or equal (same as JG)
JGE Jump if Greater than or equal (destination >= source)
JNL Jump if not Less than ( same as JGE)
JL Jump if less ( destination < source )
JNGE Jump if not Greater or equal ( same as JL)
JLE Jump if Less than or equal ( destination <= source)
JNG Jump if not greater ( same as JLE)
Sahar Mosleh California State University San Marcos Page 6
Application:
• Larger of two integers.
• The following code compares the unsigned integers in Ax and Bx and moves the larger of the two to DX:
mov dx,ax ; assume Ax is larger
cmp ax,bx ; if Ax is .= BX then
jae L1 ; jump if AX>=BX to L1
mov dx,bx ; else move BX to DX
L1:
Sahar Mosleh California State University San Marcos Page 7
Application:
• The following instructions compare the unsigned values in the three variables V1, V2, V3 and move the smallest of the three to Eax:
.data
V1 Dword ?
V2 Dword ?
V3 Dword ?
.code
mov eax, V1 ; assumes V1 is smallest
cmp eax,V2 ; if eax <= V2 then
jbe L1 ; jump to L1
mov eax, V2 ; else move V2 to ax
L1: cmp eax,V3 ; if eax <= v3 then
jbe L2 ; jump to L2
mov eax, V3 ; else move to eax
L2:
Sahar Mosleh California State University San Marcos Page 8
• Scanning array for first non zero value.
Title Scanning an Array.dataintArray dword 0,0,0,0,1,20,35,12,66,4,0noneMsg byte “A non-Zero value was not found”,0.codeMain proc
Mov ebx,offset intArrayMov ecx, lenghtof intArray
L1:Cmp [ebx],0Jnz FoundAdd ebx,4Loop L1Jmp Notfound
Found:Mov eax,[ebx]Call writeintJmp Quit
Notfound:Mov edx,offset nonMsgCall writestring
Quit:Call crlfExit
Main endpEnd main
Sahar Mosleh California State University San Marcos Page 9
Conditional Loop instructions.
Loopz and Loope instructions.
• The LOOPZ instruction permits a loop to continue while the Zero flag is set and the unsigned value of ECX is greater than zero. The destination label must be between -128 byte and +127 from the location of the following instruction.
LOOPZ destination
• LOOPE instruction is equal to LOOPZ because they share the same circuitry, this is the execution logic of loopz and loope:
ECX = ECX-1
• If ECX>0 and ZF=1 jump to destination
Sahar Mosleh California State University San Marcos Page 10
LOOPNZ and LOOPNE instructions
• The LOOPNZ ( loop if not zero) instruction is the counter part of LOOPZ. The loop continues while the unsigned value of ECX is greater than Zero and the Zero flag is clear.
• The syntax is:
LOOPNZ destination
• The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ. They share the same circuitry.
• This is the execution logic of LOOPNZ and LOOPNE
ECX = ECX - 1
• If ECX > 0 and ZF = 0, jump to destination• Otherwise no jump occurs and control passes to the next instruction
Sahar Mosleh California State University San Marcos Page 11
TITLE Scanning for a Positive Value (Loopnz.asm); Scan an array for the first positive value. If positive value found ESI is left pointing at it. If The ;loop fails to find a positive number, it stops when ecx equal to zero. In this case JNZ jumps to quit ;and ESI points to sentinel value (0) stored immediately after the array.
INCLUDE Irvine32.inc.data
array SWORD -3,-6,-1,-10,10,30,40,4sentinel SWORD 0
.codemain PROCmov esi,OFFSET arraymov ecx,LENGTHOF array
next:mov ebx,[esi] ; move element of array to ebxtest ebx,10000000b ; test highest bit of ebxpushfd ; push flags on stackadd esi, 2popfd ; pop flags from stackloopnz next ; continue loopjnz quit ; none foundsub esi, 2 ; SI points to value
quit:call crlfexit
main ENDPEND main
Sahar Mosleh California State University San Marcos Page 12
Conditional Structure
• Conditional structures are conditional expressions that trigger a choice between different logical branches.
• Each branch causes a different sequence of instructions to execute
Sahar Mosleh California State University San Marcos Page 13
Block-Structured IF Statement
• In most high level languages an IF statement implies that a boolean expression is followed by two lists of statements. One performed when the expression is true and another performed when the expression is false.
If (expression)
Statement list 1
Else
Statement list 2
• The else portion of the statement is optional. The following flowchart shows the two branching path in the conditional if structure, labeled true and false.
Sahar Mosleh California State University San Marcos Page 14
Statement list 1 Statement list 2
end
Boolean expression
start
FALSETRUE
Sahar Mosleh California State University San Marcos Page 15
Example• Compile the following C++ if statement to assembly code
If (op1 == op2){
X = 1;Y = 2;
}
Answer:mov eax, op1cmp eax, op2 ; compare EAX to op2je L1 ; jump if equal to L1jmp L2 ; otherwise, jump to L2
L1:mov X, 1mov Y, 2
L2:
Sahar Mosleh California State University San Marcos Page 16
Compound expressions
Logical AND operator
• You can implement a boolean expression that uses the logical AND operator in at least two ways.
• Consider the following compound expression written in Pseudo code.
If (al > bl) AND (bl > cl) {
X = 1}
• For any given compound expression there are at least several ways to implemented it in assembly. We are implementing two ways of the above compound expression.
Sahar Mosleh California State University San Marcos Page 17
cmp al, bl ;first expression ja L1jmp Next
L1: cmp bl, cl ; second expressionja L2jmp Next
L2: ; both are truemov X, 1 ; set X to 1
Next:
cmp al, bl ; first expressionjbe Next ; quit if falsecmp bl, cl ; second expressionjbe next ; quit if falsemov X, 1 ; both are true
Next:
We will assume that the values are unsigned. The implementation using JA (jump if above)
We can simplify the code if we reverse the JA condition and use JBE instead
If (al > bl) AND (bl > cl) {
X = 1 }
Sahar Mosleh California State University San Marcos Page 18
Logical OR operator
• When multiple expression occur in a compound expression using the logical OR operator the expression is automatically true as soon as one expression is true
Example:
If (al > bl) OR (bl > cl)X = 1
• In the following implementation, the code branches to L1 if the first expression is true; otherwise it falls through the second CMP instruction. The second expression reverse the > operator and uses JBE instead.
cmp al, bl
ja L1
cmp bl, cl
jbe next
L1: mov X, 1
next:
Sahar Mosleh California State University San Marcos Page 19
While Loops
• The WHILE structure test a condition first before performing a block of statements.
• As long as the loop condition remains true, the statement are repeated.
while (val1 < val2)
{
Val1++;
Val2--;
}
• When coding this structure in assembly language, it is convenient to reverse the loop condition and jump to endwhile when the condition becomes true
Sahar Mosleh California State University San Marcos Page 20
• Assuming that val1 and val2 are variables
mov eax, val1 ; copy variable to eax
while:
cmp eax, val2 ; if not (val1 < val2)
jnl endwhile ; exit the loop
inc eax ; val1++
dec val2 ; val2--
jmp while ; repeat the loop
endwhile:
mov val1, eax ; save new value for val1
Sahar Mosleh California State University San Marcos Page 21
Example: IF statement Nested in a Loop
• High-level structure languages are particularly good at representing nested control structure.
• In the following C++ example, an IF statement is nested inside a WHILE loop.
While (op1 < op2){
Op1 ++;If (op2 == op3)
X = 2;Else
X = 3;}
• To simplify the translation, in the following flowchart, the registers have been substituted for variables (EAX = op1, EBX = op2, and ECX = op3)
Sahar Mosleh California State University San Marcos Page 22
eax = op1ebx = op2ecx = op3
op1 =eax
end
begin
false
inc eax
X = 2 X = 3
L7:
L6:
L5:L4:
L2:true
falsetrue L3:ebx = = eax?
L1: eax < ebx?
Sahar Mosleh California State University San Marcos Page 23
Assembly codemov eax, op1mov ebx, op2mov ecx, op3
L1: cmp eax, ebx ; EAX < EBX?jl L2 ; true
jmp L7 ; falseL2
inc eaxL3:
cmp ebx, ecx ; EBX = = ECX?je L4 ; truejmp L5 ; false
L4:mov X, 2 ; X = 2jmp L6
L5:mov X, 3 ; X = 3
L6:Jmp L1 ; repeat the loop
L7:mov op1, eax ; update op1
;While (op1 < op2) ;{
;Op1 ++
;If (op2 == op3)
;X = 2
;Else
;X = 3 ; }