Page 1 of 1

real mode div turn out to err

Posted: Thu Aug 26, 2010 11:50 pm
by lemonyii
hi,
i've been programming for long, but how stupid today!
my code to print decade in real mode:

Code: Select all

printd:
	pusha
	mov		bp,sp
	mov		cx,10	;at most 10 decade numbers
	.div:
	clc
	mov		ebx,10
	div		ebx
	push	edx
	loop	.div
	mov		cx,10
	.try0:
	pop		eax
	or		eax,eax
	jz		.try0
	.prt:
	add		al,'0'
	mov		bl,2
	mov		ah,0x0e
	int		0x10
	pop		eax
	loop	.prt
	mov		sp,bp
	popa
	ret
it died at " div ebx" , bochsdbg showed that it went wrong here(for that the next instruction executed is IRET).
but why? it do not use fpu, divider is not zero, why?
and if i use " div bl ", no better. and if mov bl,123 previously, it do not die anymore.
help stupid me, thanks!

Re: real mode div turn out to err

Posted: Fri Aug 27, 2010 12:15 am
by xenos
I think you're messing up the stack. Look at how often the push and pop instructions are performed: In the first loop, push is executed 10 times - once per loop iteration. But then you pop off all zeroes, and in the next loop you're pop is executed another 10 times.

It should be possible to fix this if you add "dec cx" right after .try0, in order to count the entries you already popped off the stack.

Re: real mode div turn out to err

Posted: Fri Aug 27, 2010 2:40 am
by lemonyii
I think you're messing up the stack.
i check the logic of the code, it won't. because i limited cx to 10 at both push and pop, and saved sp in bp for ensurance.
as to
if you add "dec cx" right after .try0
i think the LOOP instruction will decrease cx, isn't it?
i will try still.
and the problem is, it turn to interrupt at the FIRST execution of DIV, so it won't matter if i messed up stack or not, or the cx of loop.
thank you,
lemonyii

Re: real mode div turn out to err

Posted: Fri Aug 27, 2010 3:46 am
by Combuster
div uses three inputs: eax, edx and whatever you specify at the instruction (ebx), I only see you setting one of the three.

Re: real mode div turn out to err

Posted: Fri Aug 27, 2010 8:01 pm
by lemonyii
div uses three inputs: eax, edx and whatever you specify at the instruction (ebx), I only see you setting one of the three.
aha! eax is the argument, i forgot edx! it works!
thank you!
lemonyii