FW: How are exceptions actually implemented in assembly?

Gustavo Córdova Avila gcordova at sismex.com
Wed Jan 23 12:02:28 EST 2002


<damn>
  I accidently sent this directly to Hung Jung.
  I'm forwarding it to the list.
</damn>

> (1) goto statements: this is how subroutines/functions are
> implemented, with pushing and popping of stacks.
> 
> (2) interrupts: actual microprocessor interruptions. You jump to
> another address when something "bad" or "urgent" happens.

and you could add:

(3) cpu exception: jump to a known location upon hitting an
unknown instruction, or instruction-prefix ("A-line" programming
on atari ST comes to mind).

> So, how is C (hence Python) exception handling actually implemented? I
> searched the web without hitting answers. My guess is that it is
> choice (1), with a call to a sub-block of code having one extra item
> in the return stack. If so, is exception handling actually kind of
> resource-intensive? (extra codes, extra memory that propagate down the
> code block structure?) Or are exceptions implemented in some smart way
> that doesn't drain too much resources? Just curious.

Hmmm... I'd hazard the following guess:

1. Allocate an exception information block for the program.

2. Define an "exception vector", where to jump upon an exception.

3. Upon entering a try { ... } catch(err) { ... } blocks:
   3.1 Allocate a "local" exception information block (which is, "err").
   3.2 Substitute the "exception vector" with the address of the
       catch(err) { ... } block, storing the old address.

4. Upon encountering an exception, store all relevant information
   into the exception block and jump to the address pointed to by
   the exception vector.

5. Check if the exception captured is "compatible" with the exceptions
   you're trying to catch:
   5.1 If not, restore older exception vector and jump there.
   5.2 If so, then continue with catch() {...} block.

6. The "final" exception handler (the original one) just kills the
   program and displays a message.

It's kinda simplistic, but I think we can begin from here. I'd do it
like this in an assembly language program. Maybe I'd use a CPU exception
vector to jump to a known location with a single opcode, it's simplest
like that, but that's subject to availability.

> (This is related to the Aspect thread... exception handling being
> often mentioned as an "aspect", but I have not seen any code example
> in Aspect Oriented Programming that actually implements exception
> handling as an aspect.)
> 
> thanks,
> 
> Hung Jung

I hope this helps.

-gus




More information about the Python-list mailing list