This is a program I wrote for my x86 assembly class which computes the sum of n numbers. It uses Irvine32.inc which came with the textbook.
Sum of numbers
title Number Addition (add.asm)
; Dani Horowitz
; CSC111 x86 Assembly Programming
; This program adds integers a user inputs
INCLUDE Irvine32.inc
;--------------------------------------------------
.stack ; begin stack segment
;--------------------------------------------------
.data ; begin data segment
sum dword 0
counter dword 0
prompt byte "Enter a value: (0 to stop) ", 0
result byte "The sum of ", 0
count byte " numbers is ", 0
;--------------------------------------------------
.code ; begin code segment
;--------------------------------------------------
;--------------------------------------------------
prompt_user PROC
;
; Take a break! Pauses screen
;--------------------------------------------------
mov edx, OFFSET prompt ; prompt user to enter values
call WriteString
call Crlf
GetValue:
call ReadInt ; read integer from keyboard
jo Overflow ; if ReadInt set overflow flag, jump to Overflow
add sum, eax ; add entered value to accumulator sum
jo Overflow ; if addition set overflow flag, jump to Overflow
cmp eax, 0 ; did user enter 0?
je Done ; if yes, goto Done
inc counter ; increment counter
jmp GetValue ; loop
Overflow:
mov eax, 0
mov sum, 0
Done:
ret
;--------------------------------------------------
prompt_user ENDP
;--------------------------------------------------
;--------------------------------------------------
print_sum PROC
;
; Take a break! Pauses screen
;--------------------------------------------------
mov edx, OFFSET result ; prints "The sum of "
call WriteString
mov eax, counter ; prints count of #s entered
call WriteDec
mov edx, OFFSET count ; prints " numbers is "
call WriteString
mov eax, sum ; prints sum
call WriteDec
call Crlf
ret
;--------------------------------------------------
print_sum ENDP
;--------------------------------------------------
;--------------------------------------------------
main proc
;--------------------------------------------------
call Clrscr ; clear screen
call prompt_user ; get integers
call print_sum ; print their sum
exit
main endp
end main
;--------------------------------------------------
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.