Professional Documents
Culture Documents
Object
To understand the concept of jump instruction in assembly language.
Theory
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.
SUB AX , BX
L2:
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.
JE , JZ ZF = 1 Jump if equal
JC CF = 1 Jump if carry
JO OF = 1 Jump if overflow
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.
Sample Code
Write a loop to display: AAAAAA
.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
Alternative solution is:
.model small
.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
.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
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
mov ah,4ch
int 21h
mainendp
end main
Lab Tasks
Write a program to check if two numbers are equal or not.
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bl,al
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bh,al
cmp bl,bh
je result
mov ah,09h
int 21h
mov ah,4ch
int 21h
result:
mov ah,09h
int 21h
mov ah,4ch
int 21h
main endp
end main
Write a program to check which number is greater.
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bl,al
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bh,al
cmp bl,bh
js result
int 21h
mov ah,09h
int 21h
mov ah,4ch
int 21h
result:
mov ah,09h
int 21h
mov ah,4ch
int 21h
main endp
end main
Write a program to display ‘A’ to ‘S’.
.model small
.stack 100h
.code
Main proc
mov bl , 19
mov dl , 'A'
start:mov ah , 02h
int 21h
inc dl
dec bl
jnz start
mov ah,4ch
int 21h
main endp
end main
Write a loop to display ‘a’ to ‘z’
.model small
.stack 100h
.code
Main proc
Mov bl , 26
mov dl , 'A'
start:mov ah , 02h
int 21h
inc dl
dec bl
jnz start
mov ah,4ch
int 21h
main endp
end main
Write a program to take hidden password from user and show the password
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax
mov ah,09h
int 21h
mov ah,08h
int 21h
mov bl,al
mov ah,02h
int 21h
mov ah,08h
int 21h
mov bh,al
int 21h
mov ah,08h
int 21h
mov cl,al
mov ah,02h
int 21h
mov ah,08h
int 21h
mov ch,al
mov ah,02h
int 21h
mov ah,09h
int 21h
mov dl, bl
mov ah,02h
int 21h
mov dl, bh
mov ah,02h
int 21h
mov dl, cl
mov ah,02h
int 21h
mov dl, ch
mov ah,02h
int 21h
mov ah,4ch
int 21h
main endp
end main