You are on page 1of 5

UNIVERSITY OF DAR ES SALAAM

College of Information and Communications Technologies

Electronics and Telecommunications Engineering Department


CS 684: Microcomputers and Microprocessors
Assignment # 3

Name: Zeyana Said Juma

Reg No. 2014-06-00380

Programme: M.Sc. Electronics Eng. and Information Technology

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

mov dx, offset message2 ;prompt user


call writestring
call readint
mov num2,ax
call crlf

add ax,num1
mov sum,ax
mov bx,10

mov dx,offset message3 ;display the first number


call writestring
mov ax,num1
call writeint_signed

mov dx,offset message4 ;display second number


call writestring
mov ax,num2
call writeint_signed

mov dx,offset message5 ;display sum


call writestring
mov ax,sum
call writeint_signed

mov ax, 4c00h ;exit


int 21h
main endp ;end main procedure
end main
2. Develop assembly program to compute and display the sum of the first N odd numbers:
1, 3, 5, 7, …,. The program should prompt the user to specify N, where 1 ≤ N ≤ 12 and
then display the numbers in the series with the corresponding sum as follows (numbers
are separated by a two spaces)
The numbers in the series are:
1 3 5 7 9 11 13 15 …
The sum of above series is: ……

.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

mov dx,offset msg_N ;Prompt user to specify N


call writestring
call reading
mov N,ax
call crlf

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

mov num,2 ;copy 2 to memory variable num

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
mov sum,1

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

mov dx,offset msg_sum ;display sum


call writestring
mov ax,1
mov sum, ax
mov bx,10
call crlf

L4:
Call crlf

mov ax,4C00h
int 21h

main endp

end main

You might also like