Lab#4: Jump Instructions SSUET/QR/114
LAB#4
Objectives:
To introduce control flow instructions
Jump instructions are used to change the flow of a Assembly language program. Jump
instructions are two types:
Unconditional Jump
The JMP instruction, whose syntax is:
JMP target
Transfers unconditionally control to the target location.
Conditional Jump-Instructions
Conditional jumps are of the general form:
JconditionStatementLabel
where (i) condition is one, two, or three letters (ii) the Statement Label must in the current code
segment and within 128 to +127 bytes from the conditional jump instruction.
How the CPU implements a conditional jump
Except for the JCXZ (Jump if the value in the CX register is zero) instruction, every conditional
jump instruction must follow a status-flag modifying instruction, either immediately. It is the
settings of the flags by this status-flag modifying instruction to which the conditional jump reacts
to.
When a conditional jump is executed, the CPU checks the flags register. If the conditions for the
jump (expressed as one or more status flag settings) are true, the CPU adjusts the IP register to
point to the destination label, so that the instruction at this label will be executed next. If the
jump condition is false, then the IP register is not altered; this means that the next sequential
instruction will be executed.
Note: The conditional jump instructions DO NOT MODIFY the flags; they only react to the
current flag values.
Example: . . .
SUB AX , BX
JZ L2 ; jump to L2 if the result is zero
L2:
CE-207: Microprocessor and Microcontroller 22
Lab#4: Jump Instructions SSUET/QR/114
THE CMP (Compare) INSTRUCTION
The CMP instruction is used to compare either two signed numbers or two unsigned numbers.
A signed number can be Greater, Less, or Equal to another signed number.
An unsigned number can be Above, Below, or Equal to another unsigned number.
The CMP instruction, whose syntax is:
CMP Operand1 , Operand2
compares two operands, and then sets or clears the following flags: AF, CF, OF, PF, and ZF.
The instruction performs the subtraction:
Operand1 - Operand2
without modifying any of its operands.
Note:
The two operands must be of the same size.
Both operands may not be memory locations at the same time.
No operand may be a segment register.
Operand1 may not be an immediate value.
Conditional jumps can be classified into three: (1) Signed jumps, (2) Unsigned jumps, and (3)
Single flag jumps.
SIGNED JUMPS UNSIGNED JUMPS
condition Equivalent mnemonic jump condition mnemonic jump condition
condition
not JG , JNLE ZF = 0 and SF = OF JA , JNBE CF = 0 and ZF = 0
not JGE , JNL SF = OF JAE , JNB CF = 0
not JL , JNGE SF OF JB , JNAE CF = 1
not JLE , JNG ZF = 1 or SF OF JBE , JNA CF = 1 or ZF = 1
Single flag jumps
CE-207: Microprocessor and Microcontroller 23
Lab#4: Jump Instructions SSUET/QR/114
mnemonic jump condition description
JE , JZ ZF = 1 Jump if equal
JNE , JNZ ZF = 0 Jump if not equal
JC CF = 1 Jump if carry
JNC CF = 0 Jump if no carry
JO OF = 1 Jump if overflow
JNO OF = 0 Jump if no overflow
JS SF = 1 Jump if sign negative
JNS SF = 0 Jump if sign is not negative
JP, JPE PF = 1 Jump if parity even, i.e., if there is an even number of 1 bits in
the result.
JNP, JPO PF = 0 Jump if parity odd, i.e., if there is an odd number of 1 bits in
the result.
Example: Write a loop to display: AAAAAA
Solution: .model small
.stack 100h
.code
mainproc
Mov cx , 6
l1: mov ah , 02h
mov dl , ‘a’
int 21h
dec cx
jnz l1
mov ah,4ch
int 21h
mainendp
end main
an alternative solution is:
.model small
CE-207: Microprocessor and Microcontroller 24
Lab#4: Jump Instructions SSUET/QR/114
.stack 100h
.code
mainproc
Mov cx , 6
l1: mov ah , 02h
mov dl , ‘a’
int 21h
dec cx
jcxz l2
jmp l1
l2:
mov ah,4ch
int 21h
mainendp
end main
Example: Write a loop to display: ABCDEFG
Solution: .model small
.stack 100h
.code
mainproc
Movbl , 7
mov dl , ‘a’
start: mov ah , 02h
int 21h
inc dl
decbl
jnz start
mov ah,4ch
int 21h
mainendp
end main
an alternative solution is:
.model small
.stack 100h
.code
mainproc
Mov dl , ‘a’
@1: mov ah , 02h
int 21h
inc dl
cmp dl , ‘g’
jbe @1
CE-207: Microprocessor and Microcontroller 25
Lab#4: Jump Instructions SSUET/QR/114
mov ah,4ch
int 21h
mainendp
end main
Lab Tasks:
Task#1: Implement all codes given in above example.
SOLUTION
OUTPUT:
SOLUTION
CE-207: Microprocessor and Microcontroller 26
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
Task#2: Display ‘*’ 10 times each in new line (Use NEW_LINE Macro)
SOLUTION:
OUTPUT:
CE-207: Microprocessor and Microcontroller 27
Lab#4: Jump Instructions SSUET/QR/114
Task #3: Display ASCII characters from A-Z and Z-A
SOLUTION:
CE-207: Microprocessor and Microcontroller 28
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
SOLUTION:
CE-207: Microprocessor and Microcontroller 29
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
Task#4: Display ASCII characters from a-z and z-a.
SOLUTION:
CE-207: Microprocessor and Microcontroller 30
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
SOLUTION:
CE-207: Microprocessor and Microcontroller 31
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
Task#5: Display ASCII characters from A-Z and z-a each on new line
SOLUTION:
CE-207: Microprocessor and Microcontroller 32
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
CE-207: Microprocessor and Microcontroller 33
Lab#4: Jump Instructions SSUET/QR/114
SOLUTION:
CE-207: Microprocessor and Microcontroller 34
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
CE-207: Microprocessor and Microcontroller 35
Lab#4: Jump Instructions SSUET/QR/114
Task#6: Display printable key strokes of a keyboard until user prints Escape “Esc” key
(Hint: ASCII code of Esc is 27)
SOLUTION:
CE-207: Microprocessor and Microcontroller 36
Lab#4: Jump Instructions SSUET/QR/114
OUTPUT:
CE-207: Microprocessor and Microcontroller 37