Divide by Zero : What Happens?

What happens if you do divide by zero on the sam3x8e microcontroller?

What happened when you tried it ?

image

5 Likes

Divide by 0 generates a “usage fault”
Probably ther is no default handler for that fault, and the “default handler “ ends up being executed, which probably doesn’t do anything useful.

image

I was taught decades ago, that the programmer should trap the possibility of division by zero and prevent it from happening in the first place. For example if you prompt the user for the coeffts of a quadratic and they give a=0, then it should say "nope sorry, that's not a quadratic" before trying to evaluate The Formula with a denominator of zero.

In fact the very very first flowchart I ever drew, caught that.

This happened when I tried to divide by zero (very carefully).

The interesting thing about post#3 is that the earth is round, and it does get divided.

@fgotham

Divide-by-Zero Interrupt: If an instruction mistakenly attempts to divide an integer by zero, a fault condition exists and the 8086 CPU is automatically interrupted by an internally generated interrupt signal of the ITC (Interrupt Type Code) code of 00h.

The CPU vectors at the IVT [00h] entry to collect the beginning address of the ISRINT0. It is hoped that the ISRINT0 contains corrective codes to overcome this fault condition. In case the ISRINT0 does not contain the corrective codes, the CPU would oscillate between the MLP (Main Line Program) and the ISRINT0. It is due to the fact that having finished the ISRINT0 codes, the CPU always points back to the same instruction of the MLP that triggered the divide-by-zero interrupt.

In the MLP, the CPU again attempts to perform x/0 operation; it is interrupted and then goes to the ISRINT0. Assume that there are corrective codes already inserted in the ISRINT0 [Example-5.3 below]. Now the previous faulty operation has turned into a valid operation and the CPU is no more interrupted. It continues executing the remaining instructions of the MLP. The codes of Example-5.3 may be experimented on a 8086 Trainer to verify the characteristic features of the divide-by-zero interrupt.

Example – 5.3 [verifying the functional characteristics of the divide-by-zero interrupt]

1. The programming codes are shown below.

                ORG		1000h	                    ORG		2000h	
		ML1	:	mov		ah, 23h				ISR1:	mov		al, 23h
		ML2:	mov		al, 00h				ISR2:	iret
		ML3:	div		al
		ML4:	goto	8086 CPU prompt
		ML4:	nop

2. Working Principles
At ML3, the CPU attempts to perform the operation 23/0, which is an abnormal condition. The CPU is automatically interrupted and jumps to ISRINT0 at location 02000h. The ISRINT0 contains corrective codes.

Is the 8086 a microcontroller (the subject of this forum section)?
Is the 8086 a device supported by Arduino (the subject of this forum)?
Is the 8086 a sam3x8e microcontroller (the subject of the original question)?

If not, why bother posting this?

You judge the relevancy.

(MCU contains MPU)

OK, I'll flag it as off-topic.
(Part of the answer is on page 78 of the datasheet)

Have you read the above quoted post #4? If the 32-bit SAM3X8E (of Arduino DUE) would support the handling mechanism of divide-by-zero situation/fault, the OP could design an experiment to understand that mechanism.

Several of the replies in this topic are off topic, but that is not an invitation for you to flag them all

You have pointed out that post #9 is not relevant to the topic so let's leave it at that

Yes, I read reply 4, and usage fault is exactly how the processor datasheet describes (really very well) a divide-by-zero (though they don't spell it that way) condition.
It doesn't describe an 8086 "fault condition"

Do you think SAM3X8E is fundamentally different from 8086 or ATmega328P or 8085? They all are programmable devices with varying hardware capabilities. You can program them to do any job to the extent your imagination goes. The theory of divide-by-zero is applicable to all the devices.

Yes, they're all fundamentally similar, but why not point to the datasheet, where the answer lies, rather than muddying the waters with an example from some archaic architecture?

At least I had that example in my collection, which is related with the thread to some extent.

I'll bear that in mind - I've got my dad's Morris Oxford owner manual somewhere, I'll refer to that next time I've got a problem with my Toyota's CVT (they've both got four wheels and a petrol engine, right?)

Great tip, thanks.

The Models have changed over the time; but, the Wankel/Otto cycles have remained the same.