Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple explanation of exceptions?

Tags:

assembly

I have an exam tomorrow and one of the subjects is exceptions in assembly. I have a list of them but I haven't been able to find a simple explanation of what an exception is and when each one happens.

The exceptions are:

  1. Area violation
  2. Division by 0 (this one is obvious)
  3. Invalid instruction
  4. Invalid address
  5. Failings

Any info will be of great help, I only need a general idea of what each one is.

like image 687
lisovaccaro Avatar asked Dec 06 '25 14:12

lisovaccaro


2 Answers

Normally the code flows as the instructions goes. So when you read the program, you can see where each instruction follows one after the other. The CPU follows this list. Now an exception, as the name says, interrupts this flow of execution. You already have given some examples. An execpetion occurs whenever something happens that the normal flow of instructions can not continue and it has to be dealt with. So exceptions are basically a way of notifiying the OS or the programmer that something out of the way happened. This can either be a bug in the code or it can be intentional.

For example, a division by zero is usually a bug. Accessing an invalid pointer can also be a bug, but it can also be a trigger for the OS to swap a memory page in from disk. The program gets halted, the OS makes sure that the page gets loaded and then resets the code to continue as if nothing bad happened.

Exceptions are rather similar to interrupts, because they break the regular flow of the program and have to be serviced and often you don't know when they will exactly happen.

like image 150
Devolus Avatar answered Dec 08 '25 19:12

Devolus


Strictly speaking, the exceptions you're referring to are not in assembly language, but are in the operational behavior of the CPU.

Conceptually speaking, a CPU has a set of state registers, consumes a stream of instructions, and manipulates state according to those instructions. Now, this stream contains instructions that manipulate state (e.g. add 1 to register EAX) and instructions that alter the future instruction stream (e.g. JMP topOfLoop).

An exception is like a jump that is taken implicitly. For example, if the current instruction is DIV and the divisor is zero, then the CPU can abort the instruction and instead jump to a different instruction stream, which is known as the exception handler. This is useful because the exception handler can do things to recover from the bad condition. Also, each instruction can potentially trigger many different kinds of exceptions (memory error, protection error, operand error, mode error, alignment error, etc.), and it would be cumbersome to explicitly write instructions to check all of these conditions.

like image 22
Nayuki Avatar answered Dec 08 '25 18:12

Nayuki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!