real mode div turn out to err

Programming, for all ages and all languages.
Post Reply
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

real mode div turn out to err

Post 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!
Enjoy my life!------A fish with a tattooed retina
User avatar
xenos
Member
Member
Posts: 1118
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: real mode div turn out to err

Post 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.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: real mode div turn out to err

Post 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
Enjoy my life!------A fish with a tattooed retina
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: real mode div turn out to err

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: real mode div turn out to err

Post 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
Enjoy my life!------A fish with a tattooed retina
Post Reply