Assembly Language Write an assembler program to find the sum of the first n odd
ID: 3771306 • Letter: A
Question
Assembly Language
Write an assembler program to find the sum of the first n odd integers starting from 1 (n is a positive integer) by using the pseudo-code below.
;n is greater than or equal to 0 – use db to define it
;result is in sum – use db to define sum
Get the value of n
counter = 1
j = 2
term = 1
sum = 0
back: if counter <= n then
sum = sum + term
term = term + 2
counter = counter+1
goto back
else stop
halt
n: db 5
sum: db 0
Note: In this example since n=5 then we will find the sum of first 5 odd integers starting from 1 which means:
1+3+5+7+9=25 (Result will be 19 in hexadecimal notation)
Explanation / Answer
Here is your program as per your requirements.
model small
.stack 200H
.data
msg1 DB 10,13,'Enter limit: $'
msg2 DB 10,13,'Enter the numbers: $'
msg3 DB 10,13,'Sum = $'
newline DB 10,13, ' $'
sum DW 0
.code
printstring MACRO msg ;macro definition
PUSH AX
PUSH DX
MOV AH,09H
MOV DX,offset msg
INT 21H
POP DX
POP AX
ENDM
.startup
printstring msg1
CALL readnumtoAX ;enter the limit
MOV CX,AX ;cxlimit
printstring newline
printstring msg2
label1: printstring newline
CALL readnumtoAX ;enter the no.s
ADD sum,AX ;sum=sum+ax
DEC CX ;cxcx-1
JNZ label1
printstring newline
printstring msg3 ;print sum
MOV AX,sum
CALL displayAX
.exit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.