0% found this document useful (0 votes)
1 views

Lec3. - Flow Control

The document discusses flow control in programming, focusing on flags used in operations such as Carry, Zero, Sign, and Overflow. It explains the types of jumps (unconditional and conditional) and their syntax, as well as how to check conditions using the CMP instruction. Additionally, it outlines different kinds of conditional jumps based on signed and unsigned interpretations, along with practical code examples for if-else, switch-case, and loops.

Uploaded by

iamnah97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lec3. - Flow Control

The document discusses flow control in programming, focusing on flags used in operations such as Carry, Zero, Sign, and Overflow. It explains the types of jumps (unconditional and conditional) and their syntax, as well as how to check conditions using the CMP instruction. Additionally, it outlines different kinds of conditional jumps based on signed and unsigned interpretations, along with practical code examples for if-else, switch-case, and loops.

Uploaded by

iamnah97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Flow Control

Recap : Flags

Flags

Overflo Carry flag


w Direction Parity flag
Interrupt enable Auxiliary flag
Trap Zero
6 are status flags Sign
3 are control
flag
Flags of Interest

● Carry:The Carry Flag is set to 1 when there is a carry out


from MSB on addition or there is a borrow into the MSB on
subtraction. Also affected by shift and rotate instructions.
● Zero: Set when result of operation is zero
● Sign: Set when msb of result =1
● Overflow : If the result of an operation falls outside the
defined range, Overflow occurs and the truncated result will
be incorrect.
Check for Signed overflow vs Unsigned
Overflow
● For unsigned numbers check Carry flag to check for overflow

● For Signed numbers check for Overflow flag to check overflow


condition
JUMPS
● Will help us execute branching like :if-else statements , switch-
statements ; and loops like :for, while statements.

● 2 categories: (1) Unconditional jumps & (2) Conditional Jumps


Unconditional Jumps
▶ Syntax:

JMP destination_label

▶ Purpose: To Transfer control to another part of the program.

▶ 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

Q5.The following algorithm may be used to carry out division of two


nonnegative numbers by repeated subtraction:
initial~ze quotient to 0
WHILE dividend >-divisor DO
increment quotient
subtract diOi~or from dividend
END_ WHILE
Write a sequence of instructions to divide.AX by BX, and put the
quotient in ex.

Read a character from cx and tell whether it's upper case , lower case
or none

You might also like