SUBROUTINES
SUBROUTINES
org 0x0100
jmp start
change:
mov ax,8
ret
start:
call change
mov ax,0x0100
int 0x21
-------------
org 0x0100
jmp start
swap:
mov dx,[bx]
mov ax,[bx+2]
mov [bx+2],dx
mov [bx],ax
ret
start:
mov ax,5
mov dx,10
mov cx,data
call swap
mov ax,0x4c00
int 0x21
data:dw 5,8
-----------------
org 0x0100
jmp start
swap:
push dx
push ax
mov dx,[bx]
mov ax,[bx+2]
mov [bx+2],dx
mov [bx],ax
pop ax
pop dx
ret
start:
mov ax,5
mov dx,10
mov cx,data
call swap
mov ax,0x4c00
int 0x21
data:dw 5,8
-------------
org 100h
.data
result dw 0
.code
main proc
mov ax, 10
mov bx, 20
push ax
push bx
call addNumber
mov [result], ax
pop bx
pop ax
jmp terminate
main endp
addNumber proc
add ax, bx
ret
addNumber endp
terminate:
mov ax, 4c00h
int 21h
end main
---------------------
Example 1: Summing Two Numbers and Storing the Result
Question:
Write an assembly program that sums the values 15 and 25, stores the result in a
memory location labeled sum, and then exits the program. Use a subroutine to
perform the addition.
org 100h
.data
sum dw 0
.code
main proc
mov ax, 15
mov bx, 25
push ax
push bx
call addNumbers
mov [sum], ax
pop bx
pop ax
jmp terminate
main endp
addNumbers proc
add ax, bx
ret
addNumbers endp
terminate:
mov ax, 4c00h
int 21h
end main
---------------------
Example 2: Multiplying Two Numbers and Storing the Result
Question:
Write an assembly program that multiplies the values 7 and 6, stores the result in
a memory location labeled product, and then exits the program. Use a subroutine to
perform the multiplication.
org 100h
.data
product dw 0
.code
main proc
mov ax, 7
mov bx, 6
push ax
push bx
call multiplyNumbers
mov [product], ax
pop bx
pop ax
jmp terminate
main endp
multiplyNumbers proc
imul bx
ret
multiplyNumbers endp
terminate:
mov ax, 4c00h
int 21h
end main
----------------------------
Question:
Write an assembly program that divides 30 by 5, stores the result in a memory
location labeled quotient, and then exits the program. Use a subroutine to perform
the division.
org 100h
.data
quotient dw 0
.code
main proc
mov ax, 30
mov bx, 5
push ax
push bx
call divideNumbers
mov [quotient], ax
pop bx
pop ax
jmp terminate
main endp
divideNumbers proc
div bx
ret
divideNumbers endp
terminate:
mov ax, 4c00h
int 21h
end main