Interrupt Service Routines: Difference between revisions
Jump to navigation
Jump to search
[unchecked revision] | [unchecked revision] |
Content deleted Content added
m reworded one sentence, looks more sane now |
→Black Magic: Asm goto under new section, and less opinionated about gotos and asm gotos |
||
Line 134: | Line 134: | ||
Neither gcc nor g++ offer any means (on x86 or x86-64) to have an interrupt service routine only in C or C++ without performing '''black magic'''. |
Neither gcc nor g++ offer any means (on x86 or x86-64) to have an interrupt service routine only in C or C++ without performing '''black magic'''. |
||
===Black Magic=== |
Added===Black Magic=== |
||
Look at the faulty code [[Interrupt_Service_Routines#The_Problem|above]], where the proper C function exit code was skipped, screwing up the stack. Now, consider this code snippet, where the exit code is added manually: |
Look at the faulty code [[Interrupt_Service_Routines#The_Problem|above]], where the proper C function exit code was skipped, screwing up the stack. Now, consider this code snippet, where the exit code is added manually: |
||
Line 164: | Line 164: | ||
This assumes that leave is the correct end-of-function handling - you are doing the function return code "by hand", and leave the compiler-generated handling as "dead code". Needless to say, such assumptions on compiler internals are dangerous. This code can break on a different compiler, or even a different version of the same compiler. It is therefore strongly discouraged, and listed only for completeness. |
This assumes that leave is the correct end-of-function handling - you are doing the function return code "by hand", and leave the compiler-generated handling as "dead code". Needless to say, such assumptions on compiler internals are dangerous. This code can break on a different compiler, or even a different version of the same compiler. It is therefore strongly discouraged, and listed only for completeness. |
||
===Asm Goto=== |
|||
Having read all the above warnings and still eager to have a solution for writing ISRs in C(++) code using gcc? Consider the following: |
Having read all the above warnings and still eager to have a solution for writing ISRs in C(++) code using gcc? Consider the following: |
||
<source lang="c"> |
<source lang="c"> |
||
Line 181: | Line 182: | ||
} |
} |
||
</source> |
</source> |
||
This code uses "asm goto" - a relatively new GCC-specific feature (see http://wiki.osdev.org/Inline_Assembly#asm_goto). |
This code uses "asm goto" - a relatively new GCC-specific feature (see http://wiki.osdev.org/Inline_Assembly#asm_goto). If you think gotos are evil, then asm gotos are worse - but in this case they might come in handy. |
||
[[Category:Interrupts]] |
[[Category:Interrupts]] |