Exceptions are events that disrupt the normal execution flow of the program. When an exception occurs the processor handles it by usually executing dedicated piece of code called exception handler. Each type of exception can have its own exception handler.
In some literature sources exceptions and interrupts are analyzed as two different things. Expeptions being generated internally by the microprocessors itself and interrupts being generated by external source (e.g hardware peripherals SPI, ADC, GPIO etc). For the sake of simplifying the categorization we are looking at the interrupts as a specific type of exception.
Types of exceptions
There are many types of exceptions and they are usually dependent on the architecture of the processor. However there are exceptions that are found in almost every processor type. Below are listed some of the most popular exception types.
Interrupt requests (IRQ)
The source of this type of exception is a peripheral device and not the microprocessor itself. The peripheral device can be a module that is internal to the microcontroller (e.g analog to digital converter) or a device that is external to the microcontroller (e.g push button).
These events are detected by the processor and their source is an abnormal event. For example:
- Divide By Zero
- Undefined instruction that the processor cannot decode. Potential reasons are
a) Use of instructions not supported by the microprocessor
b) Corrupted memory contents
- Stack overflow (For more information about the stack see our article The Concept of Stack and Its Usage in Microprocessors)
Supervisor call (SVC)
SVCs are instructions that cause an exception. They are usually used to request an access to system resources (e.g hardware peripheral) from an operating system.
If we have a user code that needs to access a hardware peripheral (e.g ADC) the code will use SVC instruction and then an exception handler in the operating system will be executed and provide access to the desired hardware. Using this mechanism keeps the access to the hardware under the control of the operating system.
The source of this type of exception is the processor power-up sequence. This exception is an integral part of the start-up procedure of the embedded system (The Boot Process of a Microcontroller)
What happens when an exception occurs
- The program counter (PC) value is put into the stack
- The reason for the exception can be stored in dedicated status registers
- Exceptions can be disabled while the current one is being handled
- Execute an exception handler routine for the current exception