Lec3. - Flow Control
Lec3. - Flow Control
Recap : Flags
Flags
JMP destination_label
▶ Example:
ORG 100h
.CODE
MOV AX, 2
MOV BX, 2
JMP LABEL_SUB
ADD AX, BX ;this instruction will never execute
LABEL_SUB:
SUB AX, BX
RET
Conditional Jumps
● Jump only when a certain condition is fulfilled.
● The conditions are usually indicated by FLAGS(s).
● SYNTAX:
Jxxx destination_label
where xxx represents the condition
● The destination_label must precede the jump instruction by no more
than 126 bytes or proceed it by 127 bytes
HOW TO CHECK THE CONDITION
● We will use a compare Instruction: CMP
● CMP (compare) instruction performs an implied subtraction of a source
operand from destination operand. Neither operand is modified.
● CMP destination, source
● FLAGS (will also affect SF and OF)
CMP Result ZF CF
Destination < 0 1
Source
Destination > 0 0
Source
Destination = 1 0
Source
Kinds of conditional Jumps
THREE Kinds :
1. Signed Jumps
2. Unsigned Jumps
3. Single Flag Jumps
Signed Jumps
▶ Signed Jumps: are used when a signed
interpretation is being given to result.
Symbol Description Condition for Jumps
JG/JNLE Jump if greater than ZF = 0 and SF = OF
Jump if not less than or equal to
JGE/JNL Jump if greater than or equal to SF = OF
Jump if not less than
JL/JNGE Jump if less than SF <> OF
Jump if not greater than or equal to
JLE/JNG Jump if less than or equal to ZF = 1 and SF <> OF
Jump if not greater than
Unsigned Jumps
Unsigned Jumps: are used for an unsigned
interpretation of result
Symbol Description Condition for Jumps
JA/JNBE Jump if above than ZF = 0 and CF = 0
Jump if not below than or equal to
JAE/JNB Jump if above than or equal to CF = 0
Jump if not below than
JB/JNAE Jump if below than CF = 1
Jump if not above than or equal to
JBE/JNA Jump if below than or equal to ZF = 1 and CF = 1
Jump if not above than
Single Flag Jumps
Single-Flag Jumps: which operate on settings of individual
flags.
Symbol Description Condition for Jumps
JE/JZ Jump If Equal ZF = 1
Jump If Equal to Zero
JNE/JNZ Jump If Not Equal ZF = 0
Jump If Not Equal to Zero
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 Sign Negative SF = 1
JNS Jump If Sign Non Negative SF = 0
JP/JPE Jump if parity even PF = 1
JNP/JPO Jump if parity not even/jump if PF = 0
parity odd
CODE EXAMPLES
IF-ELSE
Q: Suppose AX and BX have some values stored in them. Compare the
values and store the larger one in CX
Switch -case statements
Q: If AX contains a negative number, put -1 In BX; if AX
contains 0, put O In BX; if AX contains a positive number, put 1 In BX.
FOR Loop
Q: Increment AX ,8 times
While Loop
Q : Read a memory location into AX . Provided that the read value is
greater than zero. Decrement this value until it is zero. Record the
number of times you have to run the loop.
MUST READ CHAPTER 5 and 6
Assignment
1+4+7...148
Read a character from cx and tell whether it's upper case , lower case
or none