Sum of numbers

Dani 0 Tallied Votes 4K Views Share

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.

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
;--------------------------------------------------
Ichammm 0 Newbie Poster

I need to write a program that reads 5 integer exam scores from the user and take the best 3 scores' average. I got an error I could not find the best 3 scores between 5 of them. Can yo help me to write that?

wildgoose 420 Practically a Posting Shark

You should enter your five integers by looping and storing in an array, then summing the array.

In your code you don't need the compare as the addition will set the zero flag if the result is zero!

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
unimo 0 Newbie Poster

Hi! could you make block diagram for this code and sent for a gintarss@gmail.com

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.