Assembly division error

2.6k views Asked by At

Please explain me, why i get "Divide by zero" error for this code:

"mov ax,300

mov bl,2

idiv bl"

It should be as follows: al = ax div source, ah = ax mod source

1

There are 1 answers

0
Jester On

idiv is signed division which produces quotient in al as you said. That is a 8 bit register and 300/2=150 which does not fit into 8 bits when using signed arithmetic (since the maximum is 127). Somewhat misleadingly, you get divide by zero for overflow too (it's actually called #DE divide error). This is of course written in the instruction set reference.

You can use div bl if you are happy with an unsigned calculation, in that case 150 fits into al and you won't get an error.