Professional Documents
Culture Documents
Assignment #3
Assignment #3
1. Write assembly program which will prompt the user to enter two numbers from the
keyboard, and find their sum. Display the numbers entered and their sum. Sample of
output is as follows:
Enter the first number: ......
Enter the second number: ......
The sum of ...... and ...... is .......
Ans:
.model small
.stack 100h
.data
message1 db "Enter the first number: ", 0
message2 db "Enter the second number: ", 0
message3 db "The sum of ", 0
message4 db " and ", 0
message5 db " is ", 0
num1 dw ?
num2 dw ?
sum dw ?
.code
extrn crlf:proc, writestring:proc, readint:proc, writeint_signed:proc, clrscr:proc
main proc
mov ax,@data ;initialize data segment
mov ds,ax
call clrscr
mov dx,offset message1 ;prompt user
call writestring
call readint
mov num1,ax
call crlf
add ax,num1
mov sum,ax
mov bx,10
.model small
.stack 100h
.data
msg_N db "Specify N ",0
msg_error db "Out of range, N should be between 1...12. ",0
msg_series db "The numbers in the series are: ",0
msg_sum db "The sum of the above series is: ",0
msg_space db “ ”,0
num dw ?
sum dw ?
N dw ?
.code
extrn writeint:proc, writestring:proc, readint:proc, crlf:proc
main proc
mov ax,@data
mov ds,ax
cmp N,12 ;check if the number entered is within the required range
ja L2
cmp N,1
jb L2
sub N,1
mov cx,N ;set counter to N-1
cmp N,1
je L3
L1:
add ax,2 ;add 2 to get the next odd number
call writeint ;display the number
mov dx,offset msg_space ;display space
call writestring
add sum, ax
loop L1 ;loop
call crlf
jmp L4
L2:
mov dx,offset msg_error ;display error message
call writestring
call crlf
L3: ;display 1
mov dx, offset msg_series
call writestring
call crlf
mov ax,1 ;display 1
mov bx,10
call writeint
mov dx,offset msg_space ;display space
call writestring
call crlf
L4:
Call crlf
mov ax,4C00h
int 21h
main endp
end main