Working on this code to sum up real numbers from an array, it's 32bit architecture so i'm using ecx and edx to hold the upper and lower values of the floating point number

as of right now the code will output the last real number from my array, and not the sum

any ideas or input is appreciated

;*************************************ADD ARRAY**********************************************
segment .bss
sum		resq	1
segment .data
summessage	db	"The Sum is: ", 0
segment .text
extern readdouble,print_string, read_int, writedouble, print_nl, print_int
	global addarray
addarray:
	pusha
	mov	edi, 0		;initialize counter to 0
	mov	esi, 0		;initialize accum to 0
	;mov	ecx, 0		;zero out ecx and edx
	;mov	edx, 0
	
	mov	ebx, [ebp]	;moves starting location of array1 into ebx
	mov	edi, [ebp+12]	;moves array size
add_loop:
	mov	ecx, [ebx]	;mov higher order
	mov	edx, [ebx+4]	;mov lower order

	push ecx
	push edx

	fldz
        fld     qword [esp]                    ;The second input is now in a floating point register, specifically st0.
        pop     dword ecx 
        pop     dword edx                      ;The first input is now on top of the system stack (the stack addressed by bytes)

	fadd    qword [esp]                    ;The first input is added to the second input and the sum
		                               ;replaces the second input in st0

	add	ebx,8
	inc	esi

	cmp	edi, esi
	jz	add_done
	jmp	add_loop
add_done:
	call	print_nl
	mov     eax, summessage                ;Setup to display a message
        call    print_string                   ;Dr. Carter's library

        push    dword 0                      ;Make space on sytem stack for the sum value
        push    dword 0                      ;Ditto
        fst     qword [esp]                    ;Copy contents of st0 to space currently on top of the system stack
        pop     edx                            ;Copy 4 MSBs to ecx
        pop     ecx                            ;Copy 4 LSBs to ecx
	
        call    writedouble                    ;Show the 8-byte value
        call    print_nl                       ;Newline
	
	popa
	ret

My mother, what a strange code.
This could be done:
1. Moving zero to st0.
2. Pointing ebx to the first real.
3. Looping adding with: fadd [ebx] indicating the size of your real number (may be qword) and adding to ebx the size of your real (may be 8) and testing if the loop must end, may be 'dec edi jnz theloop'.
4. Getting the value of st0 as real 'fst'.
It is good to init the fpu with: finit and may be set the control word.
I can not test the code without the required routines, but I believe that there is too much push and pop.
Cheers.

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.