Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\mathtt} \newcommand{\hex}{\mathtt} \newcommand{\octal}{\mathtt} \newcommand{\prog}{\mathtt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section17.4Error Condition

Another example of an event that can cause an exception comes from something you probably did not intend to do. Unless you are a perfect programmer, you have probably seen a “segmentation fault.” This can occur when your program attempts to access memory that has not been allocated for your program. I have received this error message (yes, I still make programming mistakes!) when I have made a mistake using the stack, or when I dereference a register that contains a bad address. Examples of situations that can cause a situation that the CPU cannot deal with are:

  • Division by zero.

  • An invalid address.

  • An invalid instruction.

In a perfect world, the application software would include all the checks that would prevent the occurrence of many of these errors. The reality is that no program is perfect, so some of these errors will occur.

When they do occur, it is the responsibility of the operating system to take an appropriate action. The currently executing instruction may have caused the exception to occur. So the CPU often reacts to an exception in the midst of a normal instruction execution cycle. The actions that the CPU must take in response to an exception are essentially the same as those for an interrupt. For example, if the error is caused by an undefined instruction:

  1. The CPU switches to the und mode.

  2. The value in the cpsr is saved in spsr-und.

  3. The address in the pc is saved in lr-und.

  4. The address corresponding to the data abort exception vector, \(\hex{0x00000004}\text{,}\) is loaded into the pc.

For many error conditions, it is not possible for the exception handler to return to the previously executing program. The operating system typically writes an error message in the terminal window. The information saved in the corresponding registers is often displayed as an aid to determining the cause of the error.