Lab 03
Lab 03
Lab 03
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.
CONDITIONAL JUMPS:
JNZ is an example of conditional jump instruction. The syntax is
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.
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.
Cmp destination,source
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
So compare instruction with jumps and label can be used to implement IF_ELSE and
CASE statements.
F T
Conditio
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?
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.