COAL Lab 11
COAL Lab 11
Exercise 01:
Write an assembly language program that will display odd numbers from 1 to 10.Use appropriate jump
instructions and loops.
.model small
.stack 100h
.data
msg db 'Odd Numbers: $'
newline db 0Dh, 0Ah, '$'
buffer db '0000$'
.code
main:
mov ax, @data
mov ds, ax
mov cx, 1
display_odd:
cmp cx, 11
jge end_program
mov ax, cx
call print_num
add cx, 2
jmp display_odd
end_program:
mov ah, 4Ch
int 21h
print_num proc
pusha
mov bx, 10
lea di, buffer + 3
mov byte ptr [di+1], '$'
xor cx, cx
convert_loop:
xor dx, dx
div bx
add dl, '0'
mov [di], dl
dec di
inc cx
test ax, ax
jnz convert_loop
popa
ret
print_num endp
end main
Exercise 02:
Write an assembly language program that will display even numbers from 1 to 10.Use appropriate jump
instructions.
.model small
.stack 100h
.data
msg db 'Even Numbers: $'
newline db 0Dh, 0Ah, '$'
buffer db '0000$'
.code
main:
mov ax, @data
mov ds, ax
display_even:
cmp cx, 11
jge end_program
mov ax, cx
call print_num
add cx, 2
jmp display_even
end_program:
mov ah, 4Ch
int 21h
print_num proc
pusha
mov bx, 10
lea di, buffer + 3
mov byte ptr [di], '$'
xor cx, cx
convert_loop:
xor dx, dx
div bx
add dl, '0'
dec di
mov [di], dl
inc cx
test ax, ax
jnz convert_loop
popa
ret
print_num endp
end main
Exercise 03:
Write, assemble and debug a program. So write a program using Conditional Jump instructions that
will add all odd numbers between 1 to 20 (not including 1 & 20).
.model small
.data
sum dw 0
output_msg db "Sum of odd numbers from 1 to 20 (not including 1 & 20): $"
.code
main:
mov ax, @data
mov ds, ax
mov cx, 20
mov ax, 3
add_odd_numbers:
cmp ax, 19
jg end_program
test ax, 1
jnz odd_found
inc ax
jmp add_odd_numbers
odd_found:
add sum, ax
inc ax
jmp add_odd_numbers
end_program:
mov ax, @data
mov ds, ax
lea dx, output_msg
mov ah, 9
int 21h
display_sum:
mov bx, 10
mov cx, 0
mov di, 10
mov si, offset output_msg + 25
convert_loop:
xor dx, dx
div bx
add dl, '0'
dec si
mov [si], dl
inc cx
test ax, ax
jnz convert_loop
display_loop:
mov dl, [si]
mov ah, 2
int 21h
inc si
loop display_loop
ret
end main
Exercise 04:
Write, assemble and debug a program. So, write a program using Conditional Jump instructions that
will add all even numbers between 1 to 20 (not including 1 & 20).
.model small
.data
sum dw 0
output_msg db "Sum of even numbers from 1 to 20 (not including 1 & 20): $"
.code
main:
mov ax, @data
mov ds, ax
mov cx, 20
mov ax, 2
add_even_numbers:
cmp ax, 18
jg end_program
test ax, 1
jz even_found
inc ax
jmp add_even_numbers
even_found:
add sum, ax
inc ax
jmp add_even_numbers
end_program:
mov ax, @data
mov ds, ax
lea dx, output_msg
mov ah, 9
int 21h
display_sum:
mov bx, 10
mov cx, 0
mov di, 10
mov si, offset output_msg + 31
convert_loop:
xor dx, dx
div bx
add dl, '0'
dec si
mov [si], dl
inc cx
test ax, ax
jnz convert_loop
display_loop:
mov dl, [si]
mov ah, 2
int 21h
inc si
loop display_loop
ret
end main
Exercise 05:
Write, assemble and debug a program. So, write a program using Conditional Jump instructions that
will add all numbers between 1 to 20 (not including 1 & 20).
.model small
.data
sum dw 0
output_msg db "Sum of numbers from 1 to 20 (not including 1 & 20): $"
.code
main:
mov ax, @data
mov ds, ax
mov cx, 20
mov ax, 2
add_numbers:
cmp ax, 19
jg end_program
add sum, ax
inc ax
jmp add_numbers
end_program:
mov ax, @data
mov ds, ax
lea dx, output_msg
mov ah, 9
int 21h
display_sum:
mov bx, 10
mov cx, 0
mov di, 10
mov si, offset output_msg + 30
convert_loop:
xor dx, dx
div bx
add dl, '0'
dec si
mov [si], dl
inc cx
test ax, ax
jnz convert_loop
display_loop:
mov dl, [si]
mov ah, 2
int 21h
inc si
loop display_loop
ret
end main
Exercise 06:
Write assembly language program to calculate the factorial of a number. Use appropriate jump
instructions and loops.You can also use mul and dec instructions to solve this exercise
.model small
.data
num dw 5
fact dw 1
result_msg db "Factorial of number ", 13, 10, "$"
newline db 13, 10, '$'
.code
main:
mov ax, @data
mov ds, ax
factorial_loop:
cmp bx, 1
jle end_factorial
mov ax, fact
mul bx
mov fact, ax
dec bx
jmp factorial_loop
end_factorial:
mov ah, 09h
mov dx, offset result_msg
int 21h
display_num:
mov bx, 10
mov cx, 0
convert_loop:
xor dx, dx
div bx
add dl, '0'
push dx
inc cx
test ax, ax
jnz convert_loop
display_loop:
pop dx
mov ah, 02h
int 21h
loop display_loop
ret
14