Start New Discussion within our Software Development Community

I wrote this code to get a standard deviation from inputs that are stored in an array.

I outputted all of my inputs and they are correct, and it's subtracting the mean correctly, hoever when i hit the check flag(if counter is equal to zero) i get a floating point error

any ideas?

it is looping correctly..... up until the counter hits zero

compute_variance:
	pusha
	mov	ebx, [esp+36]	;starting location of array
	mov	esi, [esp+44]	;move the count value into esi
	mov	ebp, [esp+52]	;move mean value into edx
	mov	ecx, 0		;set ecx to accumulator to 0
variance_loop:
	mov	edx, 0		;set edx to 0
	mov	eax, [ebx]	;move value of array into eax
	sub	eax, ebp	;minus value by mean
	mul	eax		;multiply by itself to square the value
	add	ecx, eax	;add eax value into accumulator
	add	ebx, 4		;increment memory location in array
	dec	esi		;decrement counter


	cmp	esi, 0		;if 0 then exit loop
	jz	variance_done
	jmp	variance_loop
variance_done:
	xor	esi, edx	;zero out registers esi and edx
	mov	esi, [esp+44]	;move count value back onto esi
	;mov	edx, 0		;initializing edx to zero
	mov	eax, ecx	;move accumulator into eax
	
	idiv	esi		;divide accumulator by the count value
	
	mov	[esp+56],eax	;move the accumulator back onto the stack
	popa
	ret

The only place I see a floating point error (division by zero) is at your idiv... if eax or esi == 0 then you will get this... a cmp wouldn
t throw that error... if you want to put the accumulator back on the stack shouldn't

mov	[esp+56],eax

be after popa? I could be wrong I am super tired...

if you are testing for 0 you can change cmp esi, 0 to test esi, esi (its smaller) :)

This article has been dead for over six months. Start a new discussion instead.