6
6
6
•Conditional Branching
•Conditional Jumps
•Assembly language provides all the tools you need for decision-
making logic
6.2 BOOLEAN AND COMPARISON
INSTRUCTIONS
Status Flags (Revision)
• The Zero flag is set when the result of an operation equals zero.
• The Carry flag is set when an instruction generates a result that is too large (or too small) for
the destination operand.
• The Sign flag is set if the destination operand is negative, and it is clear if the destination
operand is positive.
• The Overflow flag is set when an instruction generates an invalid signed result.
• The Parity flag is set when an instruction generates an even number of 1 bits in the low byte
of the destination operand.
• The Auxiliary Carry flag is set when an operation produces a carry out from bit 3 to bit 4.
AND Instruction: performs a boolean (bitwise) AND operation
between each pair of matching bits in two operands and places the result
in the destination operand:
AND destination, source
•The following operand combinations are permitted
• E.g. Suppose we want to know whether bit 0 or bit 3 is set in the AL register.
•The TEST instruction always clears the Overflow and Carry flags. It
modifies the Sign, Zero, and Parity flags in the same way as the AND
instruction.
CMP Instruction :we use the CMP instruction to compare integers
•The CMP (compare) instruction performs an implied subtraction of a
source operand from a destination operand. Neither operand is
modified, but flags are affected:
CMP destination, source
•When two unsigned operands are compared:
•When two signed operands are compared:
Example: destination > source
mov al,5
cmp al,-2 ; Sign flag == Overflow flag
mov al,-1
cmp al,5 ; Sign flag != Overflow flag
6.3 CONDITIONAL JUMPS
Conditional Structures: you can implement high level logic instructions
using a combination of comparisons and jumps. Two steps are involved
in executing a conditional statement:
cmp eax,0
jz L1 ; jump if ZF = 1
.
.
L1:
Example 2:
and dl,10110000b
jnz L2 ; jump if ZF = 0
.
.
L2:
Jcond Instruction: A conditional jump instruction branches to a
destination label when a status flag condition is true. Otherwise, if the
flag condition is false, the instruction immediately following the
conditional jump is executed.
Jcond destination
Types of Conditional Jump Instructions:
•Jumps based on specific flag values
•Jumps based on equality between operands or the value of (E)CX
•Jumps based on comparisons of unsigned operands
•Jumps based on comparisons of signed operands.
Unsigned Comparisons: The jumps in following table are only
meaningful when comparing unsigned values. Signed operands use a
different set of jumps.
Signed Comparisons
6.4 CONDITIONAL LOOP INSTRUCTIONS
LOOPZ and LOOPE Instructions
•The LOOPZ (loop if zero) instruction works just like the LOOP
instruction except the Zero flag must be set in order for control to
transfer to the destination label.
LOOPZ destination
ECX ECX – 1
if ECX > 0 and ZF=1, jump to destination
ECX ECX – 1;
if ECX > 0 and ZF=0, jump to destination
next:
if (al > bl) AND (bl > cl)
X = 1;
A possible implementation:
• Conditional Jumps
• Jumps based on specific flag values
• Jumps based on equality between operands or the value of (E)CX
• Jumps based on comparisons of unsigned operands
• Jumps based on comparisons of signed operands.
• Conditional Structures