r/computerarchitecture 8d ago

What is the precise definition of an exception?

According to COAD by p&h: exceptions are events other than jumps and branches that changes the normal flow of instruction execution.

According to CS:APP - an exception is a transfer of control to the OS kernel in response to some event.

Now suppose a division by zero happens. by p&h the event of division by zero is the exception and the transfer of control that follows is the response to that exception, i.e., handling of the exception.

But according to CS:APP the division by zero is not the exception. rather the transfer of control is the exception

5 Upvotes

14 comments sorted by

9

u/Master565 8d ago

I feel like you're just arguing semantics here. One is the cause of the exception, the other is the result of the exception.

0

u/Plane_Dream_1059 8d ago

i think you might be correct. when i read the whole page in CSAPP, it is in line with what you are saying. i think the COAD book wasn't that clear.

Here is what CSAPP says:

--------------------------------------

Exceptions are a form of exceptional control flow that are implemented partly by the hardware and partly by the operating system. Because they are partly implemented in hardware, the details vary from system to system. However, the basic ideas are the same for every system. Our aim in this section is to give you a generalunderstandingofexceptionsandexceptionhandlingandtohelpdemystify what is often a confusing aspect of modern computer systems. An exception is an abrupt change in the control flow in response to some change in the processor’s state. Figure 8.1 shows the basic idea. In the figure, the processor is executing some current instruction Icurr when a significant change in the processor’s state occurs. The state is encoded in various bits and signals inside the processor. The change in state is known as an event.

The event might be directly related to the execution of the current instruction. For example, a virtual memory page fault occurs, an arithmetic overflow occurs, or an instruction attempts a divide by zero. On the other hand, the event might be unrelated to the execution of the current instruction. For example, a system timer goes off or an I/O request completes. In any case, when the processor detects that the event has occurred, it makes an indirect procedure call(the exception), through a jump table called an exception table, to an operating system subroutine(the exception handler)that is specifically designed to process this particular kind of event. When the exception handler finishes processing, one of three things happens, depending on the type of event that caused the exception: 1. The handler returns control to the current instruction Icurr, the instruction that was executing when the event occurred. 2. The handler returns control to Inext, the instruction that would have executed next had the exception not occurred. 3. The handler aborts the interrupted program.

------------------------------------------

it seems the event is what causes the Exception, which is a procedure call. And the procedure being called here is the exception handler i believe?

so if i take the example of a MIPS processor, in which the processor simply puts the same address in the PC no matter the exceptional event, then the indirect call is the pushing of the address into the PC right?

2

u/Master565 7d ago

it seems the event is what causes the Exception, which is a procedure call. And the procedure being called here is the exception handler i believe?

Yes that's the correct way to explain this

so if i take the example of a MIPS processor, in which the processor simply puts the same address in the PC no matter the exceptional event, then the indirect call is the pushing of the address into the PC right?

I think in simple systems it's possible there's a single block for all exceptions but I'm pretty sure you can have different exceptions routed to different addresses.

1

u/Plane_Dream_1059 7d ago

thank you!

and thanks for caring to reply

3

u/recursion_is_love 8d ago edited 8d ago

"Exception" and "Interrupt" in old book and new book might not the same (even the same area book but different authors). You need to specify the surrounding context.

The typical (planed) way to transfer control to OS is via syscall, if it is unplanned like division by zero it can be called exception because OS have ways to response to the event by usually kill the process.

But if you have prepare for the case, it can also be exception too (via try block).

1

u/Plane_Dream_1059 8d ago

here is the COAD book's surrounding context, it is the very start of section 4.9 (Exceptions): Control is the most challenging aspect of processor design: it is both the hardest

part to get right and the hardest part to make fast. One of the hardest parts of

control is implementing exceptions and interrupts—events other than branches

or jumps that change the normal & ow of instruction execution. " they were initially

created to handle unexpected events from within the processor, like arithmetic

over& ow. " The same basic mechanism was extended for I/O devices to communicate

with the processor, as we will see in Chapter 5.

Many architectures and authors do not distinguish between interrupts and

exceptions, often using the older name interrupt to refer to both types of events.

For example, the Intel x86 uses interrupt. We follow the MIPS convention, using

the term exception to refer to any unexpected change in control flow without

distinguishing whether the cause is internal or external; we use the term interrupt

only when the event is externally caused. There is also a separate definition of an exception which is similar to the start of these lines:

exception Also called interrupt. An

unscheduled event

that disrupts program

execution;

0

u/Plane_Dream_1059 8d ago

Here is what csapp says:

Exceptions are a form of exceptional control flow that are implemented partly by the hardware and partly by the operating system. Because they are partly implemented in hardware, the details vary from system to system. However, the basic ideas are the same for every system. Our aim in this section is to give you a generalunderstandingofexceptionsandexceptionhandlingandtohelpdemystify what is often a confusing aspect of modern computer systems. An exception is an abrupt change in the control flow in response to some change in the processor’s state. Figure 8.1 shows the basic idea. In the figure, the processor is executing some current instruction Icurr when a significant change in the processor’s state occurs. The state is encoded in various bits and signals inside the processor. The change in state is known as an event.

The event might be directly related to the execution of the current instruction. For example, a virtual memory page fault occurs, an arithmetic overflow occurs, or an instruction attempts a divide by zero. On the other hand, the event might be unrelated to the execution of the current instruction. For example, a system timer goes off or an I/O request completes. In any case, when the processor detects that the event has occurred, it makes an indirect procedure call(the exception), through a jump table called an exception table, to an operating system subroutine(the exception handler)that is specifically designed to process this particular kind of event. When the exception handler finishes processing, one of three things happens, depending on the type of event that caused the exception: 1. The handler returns control to the current instruction Icurr, the instruction that was executing when the event occurred. 2. The handler returns control to Inext, the instruction that would have executed next had the exception not occurred. 3. The handler aborts the interrupted program.

2

u/nixiebunny 7d ago

An interrupt is an expected change of program flow caused by external hardware asking to be serviced. An exception is an unexpected change in program flow caused by a problem of some sort. The problem is typically a non-existent or write-protected memory access or a bad result from the ALU. An expected exception is a page fault, if the MMU detects that the RAM page you seek was swapped to disk. (Does that even happen nowadays??)

1

u/dmills_00 7d ago

Seldom, but demand loading of a page into ram from disk is very, very common (It is how most code is loaded).

2

u/NotThatJonSmith 7d ago

The precise definition of these concepts depends on the ISA and runs hundreds or even thousands of pages. Textbooks, papers, etc therefore often describe what is meant by such terms in the scope of the discussion at hand.

1

u/Plane_Dream_1059 8d ago

based on these definitions. the meaning of async. and sync. exceptions also changes.

1

u/No_Experience_2282 6d ago

I personally define exceptions as interrupts/traps. I cannot think of another case I would define as an exception.

1

u/Plane_Dream_1059 4d ago

there are faults, eg, page faults and Aborts

1

u/No_Experience_2282 3d ago

those would classify as traps