How processor handles case of division by zero

15,104

Solution 1

To answer in general terms, rather than going into gory details for Linux on x86_64, which are likely to obscure the concepts.

CPUs tend to throw an exception interrupt, on things like division by zero, or dereferencing a NULL pointer. These interrupts are trapped, like when hardware interrupts, halting execution of current program and return control to the OS, which then handles the event. Whilst the actions are very environment dependant, commonly the program may be terminated, all resources freed (memory, open files) and optionally core dumps/stack traces generated for debugging purposes on a developers system.

A runtime might be able to configure things so an exception handler is called, perhaps a scripting language wants to catch integer division by 0, or integer overflow and then throw a programming language exception or generate diagnostics to help the programmer understand where & why, it happened. Raising a signal, which may be caught by the application and handled, or lead to termination, is another traditional possibility.

On some RISC CPUs, software traps in OS would run to fix up misaligned data accesses, so reading memory would work, but with a performance penalty. In past, traps would sometimes be used to emulate, defined instructions but which were not implemented in hardware by a particular CPU model. I've also seen hardware memory errors logged, as OS initiates an ECC memory recovery operation, though that is handled differently on x86.

System calls, actually use the same mechanism to jump, from a user space application, into the OS kernel which then handles the event, hence the common term trap.

Solution 2

Let me try to answer this a little differently. Every processor I have worked with defines an interrupt vector structure. On the Intel chips this structure is called the interrupt dispatch table (IDT). The interrupt vector is an array of pointers to functions. Each entry in the array corresponds to a specific event (interrupt or exception (fault or trap)).

The operating system sets up the functions (interrupt handler, exception handler) for each event. When a divide by zero occurs, it triggers an exception. The CPU responds by invoking the exception handler in the interrupt vector corresponding to a divide by zero. On the Pentium, this is the very first entry in the table.

Share:
15,104
Alfred
Author by

Alfred

I am a programmer and interested in algorithms and software and system design.

Updated on June 03, 2022

Comments

  • Alfred
    Alfred almost 2 years

    Curious what the processor/CPU does in general or let say, on intel cpu & Linux, when it executes a division by zero instruction. Also how the error is relayed to the application, so that it can log the error or notify the developer?

    Thank you!

  • Rob11311
    Rob11311 almost 10 years
    Right, was trying to differentiate them from interrupts generated by peripherals, quick explanations can be so tricky
  • Alfred
    Alfred almost 10 years
    @Rob11311 "These interrupts are trapped, like when hardware interrupts, halting execution of current program and return control to the OS", so all these are handled by cpu, i.e. is implemented by hardware or firmware (since at this point, OS has no control), right?
  • Rob11311
    Rob11311 almost 10 years
    Yes, it's the hardware. The OS scheduler can regain control by scheduling a timer interrupt even if no other interrupts occur (peripherals or system calls), so really it just lends the CPU to the user program.