Professional Documents
Culture Documents
Object
To understand the concept of loop in assembly language.
Theory
Loop
A loop is a sequence of instructions that is repeated. The number of times to repeat may be known
in advance, or it may depend on conditions i.e. it’s a count controlled loop.
KEYWORD: LOOP
A FOR loop is implemented using the LOOP instruction. The counter for the loop is the CX
register, which is initialized to loop_count, which is the number of times the loop is executed.
Execution of the LOOP instruction causes CX to be decremented automatically. If CX becomes 0,
the next instruction after loop is done.
Sample Code
Write a program that prints a character 100 times.
.model small
.stack 100h
.code
main proc
mov cx, 100 ;number of times loop will execute
mov dl, '*' ;ASCII code of character 0
print: ;loop starts from here
mov ah, 02h ;display a character
int 21h
loop print ;executes the FOR loop
mov ah,4ch ;terminates the current process
int 21h
main endp
end main
Lab Tasks
.stack 100h
.code
Main proc
mov cx, 10
print:
int 21h
inc dl
loop print
mov dl, 10
int 21h
mov dl,13
mov ah,02h
int 21h
mov cx, 10
print1:
int 21h
dec dl
loop print1
mov Ah,4ch
int 21h
main endp
end main
Write a program to print ASCII characters.
.model small
.stack 100h
include emu8086.inc
.code
Main proc
mov cx, 26
mov dl, 'A'
BB:
mov ah,02h
int 21h
printn
inc dl
dec cx
JNZ BB
mov ah,4ch
int 21h
main endp
________________________________
.model small
.stack 100h
include emu8086.inc
.code
Main proc
mov cx, 26
mov dl, 'z'
BT:
mov ah,02h
int 21h
printn
dec dl
dec cx
JNZ BT
Mov ah,4ch
int 21h
main endp