OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Apr 24, 2024 4:50 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Divide by 0 exception when dividing 4 by 2 ?
PostPosted: Thu Mar 30, 2017 2:52 am 
Offline
Member
Member

Joined: Tue Feb 28, 2017 10:22 am
Posts: 25
Hello,

I am currently running my 32-bit protected mode code in Bochs.
I have loaded GDT, IDT and PIC, and interrupts are enabled.

To write my itoa implementation, I have to use the DIV instruction, which calls int 0 everytime.

Here is my test code :

Code:
mov eax, 4
mov ecx, 2
div ecx ;will call int0


This code calls my divide by 0 exception handler, and I don't know why.

Thanks by advance,
Ankeraout.


Top
 Profile  
 
 Post subject: Re: Divide by 0 exception when dividing 4 by 2 ?
PostPosted: Thu Mar 30, 2017 2:58 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

Ankeraout wrote:
Here is my test code :

Code:
mov eax, 4
mov ecx, 2
div ecx ;will call int0


This code calls my divide by 0 exception handler, and I don't know why.


It's because the DIV instruction divides a 64-bit unsigned integer (in EDX:EAX) by a 32-bit divisor (in ECX in your case). You don't set EDX to zero, so you could be dividing a huge number (e.g. 0xFFFFFFFF00000004) by 2.

Also note that you get a "divide error" exception when the result doesn't fit in 32-bits (which includes "infinity"). If EDX was 2 or larger the result won't fit in 32-bits and...


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Divide by 0 exception when dividing 4 by 2 ?
PostPosted: Thu Mar 30, 2017 3:04 am 
Offline
Member
Member

Joined: Tue Feb 28, 2017 10:22 am
Posts: 25
Oh okay, I will test clearing EDX :D
Thank you !

EDIT : It works. Thanks !


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 152 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group