You are on page 1of 6

EXPERIMENT 03

FLOW CONTROL INSTRUCTIONS


OBJECTIVE:
 In this lab you would perform following operations
1. Compare
2. Conditional jump
3. Unconditional jump
4. Implementation of if else conditions
EQUIPMENT:
SOFTWARE:
 Turbo assembler (TASM)

DISCUSSION:

For assembly language programs to carry out useful tasks, there must be a way to
make decisions and repeat sections of code. In this lab we would learn how these
things can be accomplished with the jump and loop instructions.
The jump and loop instructions transfer control to another part of the program. This
transfer can be unconditional or can depend on a particular combination of status flag
settings.

AN EXAMPLE OF A JUMP:

.model small
.stack 100h
.data
.code
.startup
mov ah,2 ; display ch function
mov cx,256 ; no of ch to be displayed
mov dl,0 ; dl has ascii code of null ch
print:
int 21h ;display a character
inc dl ;increment ascii code
dec cx ; decrement counter
jnz print ;keep going till cx is not zero
.exit
end
The ASCII table has 255 characters, so we initialized the cx register with 256, as we
will decrement it to zero; dl is initialized with 0 and is incremented by one till it
reaches 255. The conditional jump used here is JNZ: jump if not zero and the label
used is PRINT so JNZ checks the status of the memory byte used with decrement
instruction and if it is not equal to zero the PRINT label is executed again.

OUTPUT OF THE CODE:

FIG: output of given code when assembled on TASM or MASM

All the ASCII characters are printed on screen.

CONDITIONAL JUMPS:
JNZ is an example of conditional jump instruction. The syntax is

Jxxx destination label

If the condition for the jump is true, the next instruction to be executed is the one in
destination label which may precede or follow the jump instruction itself. If the
condition is false, the instruction immediately following the jump is done next.
RANGE OF CONDITIONAL JUMP:
the structure of the machine code of a jump requires that destination label should
precede the jump instruction no more than 126 bytes.

HOW THE CPU IMPLEMENTS A CONDITIONAL JUMP:


To implement a conditional jump, the CPU looks at the FLAGS register. If the
condition is true the CPU adjusts the instruction pointer to point to the destination
table, so that the instruction in destination label is done first. If the jump condition is
false, then the IP is not altered; this means the next instruction in line will be done.

In the preceding program, the CPU executes JNZ PRINT by checking the ZF (ZERO
FLAG), if ZF = 0, control transfers to PRINT; if ZF = 1, the control goes to execute
mov ah,4ch (exit) instruction.

THE CMP INSTRUCTION:


The jump instruction is often provided by CMP (compare) instruction. It has the form

Cmp destination,source

This instruction compares destination and source by computing the destination


contents minus source contents. The result is not stored, but the flags are affected. The
operands of CMP may not both be memory locations, destination may be a constant.
For example suppose a program contains these lines

Cmp ax,bx

Jg below

Where AX=7FFFh and BX=0001. the result of cmp instruction is 7FFFh – 0001h =
7FFEh. Table shows that the jump conditio0n for JG is satisfied as ZF = OF = SF = 0
so control transfers to below.

CONDITIONAL JUMPS:

SIGNED JUMPS:
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 = CF
Jump if not less than or equal to
JL/JNGE Jump if less than SF <> OF
Jump if not greater than or equal
JLE/JNG Jump if less than or equal ZF = 1 or SF<>OF
Jump if greater than
UNSIGNED CONDITIONAL JUMPS:
SYMBOL DESCRIPTION CONDITION FOR
JUMPS
JA/JNBE Jump if above CF = 0 and ZF = 0
Jump if not below or equal
JAE/JNB Jump if above or equal CF = 0
Jump if not below
JB/JNAE Jump if below CF = 1
Jump if not above or equal
JBE/JNA = jump if equal CF = 1 or ZF 1
Jump if not above

SINGLE FLAG JUMPS:


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 zero
JC Jump if carry CF = 1
JNC Jump if no carry CF = 0
JO Jump if overflow OF = 1
JNO Jump if no overflow OF = 0
JS Jump if sign negative SF = 1
JNS Jump if no negative sign SF = 0
JP/JPE Jump if parity even PF = 1
JNP/JPO Jump if parity odd PF = 0

So compare instruction with jumps and label can be used to implement IF_ELSE and
CASE statements.

F T
Conditio

False branch True branch


conditions conditions
AN EXAMPLE TO PRINT EVEN AND ODD NUMBERS
BETWEEN 0-9
title even_odd l1:
.model small mov ah,02
.stack int 21h
.data add dl,2
mess db 0ah,0dh,'enter e 4 even and o 4 odd $' loop l1
.code jmp exit
.startup odd:
mov ax,@data mov dl,49
mov ds,ax mov cx,5
lea dx,mess l2:
mov ah,09 mov ah,02
int 21h int 21h
mov ah,01 add dl,2
int 21h loop l2
cmp al,'o' exit:
je odd .exit
cmp al,'e' End
je even
jmp exit
even:
mov dl,48
mov cx,5

Lab Tasks:
1. Write a code to display even numbers from 0 to 10 if ‘e’ is pressed and display
odd numbers if ‘o’ is pressed?

2. Write a code that display all the alphabets (Capital or small).

3. Write a code to print all ASCII characters using loop instruction.

4. Write a code to check weather input digit is even or odd?

5. Write a code for counting the characters entered before a space is pressed.

Home Tasks:
1. Write a code to print 5 stars each in new line.

2. Write a code to print the table of the number entered by a user.

3. Write a code which prompts the user to enter an option from 1 to 3

 If he presses 1, display a pre-defined string

 If he presses 2, display that string in reverse

 If he presses 3, display the reversed string in capital


 If number is not 1,2 or 3 exit

You might also like