Interrupt Service Routines: Difference between revisions

Jump to navigation Jump to search
Substituted buggy code by reference to new, complete page
[unchecked revision][unchecked revision]
(→‎Two-Stage Assembler Wrapping: add cld to asm stub)
(Substituted buggy code by reference to new, complete page)
Line 167:
 
=====Asm Goto=====
:''Full article: [[ISRs_PIC_And_Multitasking|ISRs, PIC, And Multitasking]]''
Having read all the above warnings and still eager to have a solution for writing ISRs in C(++) code using gcc? Consider the following:
Since GCC 4.5, the inline assembler supports the "asm goto" statement. It can be used to make ISRs as functions which return the correct address of the ISR entry point.
<source lang="c">
// Call this function when populating the IDT, to get the address of the ISR handling code
void * interrupt_handler( )
{
__asm__ __volatile__ goto( "jmp %l[endOfISR]" : : : "memory" : endOfISR );
__asm__ __volatile__( ".align 16\t\n" : : : "memory" ); // align by 16 for efficiency - could be even higher, depending on CPU
startOfISR:
__asm__ __volatile__( "pushad\t\n" : : : "memory" );
 
printf( "Hello, world of ISRs!" );
__asm__ __volatile__( "popad\t\niret\t\n" : : : "memory" );
endOfISR:
__asm__ __volatile__ goto( "mov %l[startOfISR], %%eax" : : : "memory" : startOfISR );
 
}
</source>
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]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu