You are on page 1of 12

Lab Session 03

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

1. Write a program to print ‘*’ infinite times


2. .model small
3. .stack 100h
4. .code
5. main proc
6. mov cx, 100
7.
8. print:
9. mov dl, '*'
10. mov ah, 02h
11. int 21h
12.
13. mov dl, 0Ah
14. mov Ah, 02h
15. int 21h
16. mov dl, 0Dh
17. mov ah, 02h
18. int 21h
19.
20. loop print
21. mov ah,4ch
22. int 21h
23. main endp
24. end main
.
Write a program to print ASCII characters from A-Z and Z-A

25. .model small


26. .stack 100h
27. .code
28. main proc
29. mov cx, 26
30. mov dl, 'A'
31. print:
32. mov ah, 02h
33. int 21h
34. inc dl
35. loop print
36.
37. mov dl, 10
38. mov ah, 02h
39. int 21h
40. mov dl,13
41. mov ah,02h
42. int 21h
43.
44. mov cx, 26
45. mov dl, 'Z'
46. print1:
47. mov ah, 02h
48. int 21h
49. dec dl
50. loop print1
51.
52. mov Ah,4ch
53. int 21h
54. main endp
55. end main
Write a program to print ASCII characters from a-z and z-a in new line
56. .model small
57. .stack 100h
58. .code
59. Main proc
60. Mov cx, 26
61. mov dl, 'a'
62. print:
63. mov ah, 02h
64. int 21h
65. inc dl
66. loop print
67.
68. mov dl, 10
69. mov ah, 02h
70. int 21h
71. mov dl,13
72. mov ah,02h
73. int 21h
74.
75. mov cx, 26
76. mov dl, 'z'
77. print1:
78. mov ah, 02h
79. int 21h
80. dec dl
81. loop print1
82.
83. mov Ah,4ch
84. int 21h
85. main endp
86. end main
Write a program to print from 0-9 and 9-0
.model small

.stack 100h

.code

Main proc

mov cx, 10

mov dl, '0'

print:

mov ah, 02h

int 21h

inc dl

loop print

mov dl, 10

mov ah, 02h

int 21h

mov dl,13

mov ah,02h

int 21h

mov cx, 10

mov dl, '9'

print1:

mov ah, 02h

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

You might also like