You are on page 1of 15

Program flow control

Bratosin Ioan Alexandru


Control instructions
Control instructions are important to learn in order to manage the flow of the
program according to certain decisions.
unconditional jumps:
The basic instruction that transfers control to another point in the program is JMP.
The basic syntax of JMP instruction:
JMP label
To declare a label in your program, just type its name and add ":" to the end, label
can be any character combination but it cannot start with a number, for example
here are 3 legal label definitions:
label1:
label2:
a:
Label can be declared on a separate line or before any other
instruction, for example:
x1:
MOV AX, 1

x2: MOV AX, 2


Example
Unlike JMP instruction that does an unconditional jump, there are
instructions that do a conditional jumps (jump only when some
conditions are in act).
JE , JZ Jump if Equal (=). Jump if Zero.
JNE , JNZ Jump if Not Equal (<>). Jump if Not Zero.
JG , JNLE Jump if Greater (>). Jump if Not Less or Equal (not <=).
JL , JNGE Jump if Less (<). Jump if Not Greater or Equal (not >=).
JGE , JNL Jump if Greater or Equal (>=).Jump if Not Less (not <).
JLE , JNG Jump if Less or Equal (<=).Jump if Not Greater (not >).
JNO Jump if not overflow
Example
We can replicate high level conditional instruction with the help of
jumps:
CMP- compares 2 numbers.
If.. Then..else
If(AX=3)
Then BX=5 else BX= DX
Generally, when it is required to compare numeric values CMP
instruction is used (it does the same as SUB (subtract) instruction, but
does not keep the result, just affects the flags).
The logic is very simple, for example:
it's required to compare 5 and 2, 5 - 2 = 3 the result is not zero (Zero
Flag is set to 0).
Another example: it's required to compare 7 and 7, 7 - 7 = 0 the
result is zero! (Zero Flag is set to 1 and JZ or JE will do the jump).
here's an example of CMP instruction and conditional jump:
Example 2:

For(x=0;cx<5;cx++)
ax=ax+cx

How can we convert this with CMP and jump tags?


LOOP decrease cx, jump to label if cx >0.
LOOPE decrease cx, jump to label if cx >0 and equal (zf = 1).
LOOPNE decrease cx, jump to label if cx >0 and not equal (zf = 0).
LOOPNZ decrease cx, jump to label if cx >0 and zf = 0.
LOOPZ decrease cx, jump to label if cx >0 and zf = 1.
JCXZ jump to label if cx is zero.

ZF the zero flag is used to check the result of an arithmetic operation,


including bitwise logical instructions. It is set to 1, or true, if an
arithmetic result is zero, and reset otherwise.
Alternative we can use the LOOP for the same instruction at slide 11
MOV AX,0
MOV CX,5
for:
ADD AX, CX
LOOP for ;decrease CX , if CX >0 jump back to for label
Example of nested loop

mov ah, 0EH ; the subfuction of the Video


Services ROM Interrupt
mov bh, 0 ; set the video page
mov al, XX ; where XX is a constant, register, or
memory position of the ASCII char
int 10h ; actually writes the character on the
screen and moves the cursor one position
Exercise
1.Read 2 numbers from the keyboard and compare them if they are
equal or not. Then print the result.
2.Make a program that reads 2 numbers from the keyboard,
increments the first number by 1 and the 2nd number by 2, 5 times and
prints the value of the sum of the 2 numbers on the screen after each
incrementation.
3.Using loop instruction please create a program that has the following
output

You might also like