Raising interrupts from C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
gsingh2011
Member
Member
Posts: 83
Joined: Tue Feb 03, 2009 11:37 am

Raising interrupts from C

Post by gsingh2011 »

I thought that if I wanted to raise an interrupt from C I could just do this:

Code: Select all

asm("int 0h");
But I just get:

Code: Select all

/tmp/ccIvOtJL.s: Assembler messages:
/tmp/ccIvOtJL.s:39: Error: junk `h' after expression
/tmp/ccIvOtJL.s:39: Error: operand size mismatch for `int'
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Raising interrupts from C

Post by sortie »

Note how this is inline assembly given to GCC (I suppose that is your compiler). The compiler uses the GNU assembler as its assembler backend rather than whatever third party assembly you think you are using (probably nasm). You'll need to write your assembler first in GNU assembler format (probably AT&T syntax if you are on i386 or x86_64) and then possibly escape the string containing assembly (perhaps add double %%'s to use a specific register). Next you'll need to add input and output operand rules to the inline assembly to communicate with the compiler which registers are input and output. You'll also need to add a list of registers to the clobber list, but also keep in mind that staging the CPU status flags (eflags on i386) and memory also needs to be specified. If the assembly statement at this point looks like it has no side effects, the compiler will happily optimize it away. You'll need to add a volatile keyword to the inline assembly if this is undesired. For instance, the interrupt instruction in your simple statement looks like it has no consequence (except that volatile is implied if there are no register operands). Keep in mind that the compiler will never look at the text inside your assembly statement but just mindlessly forward it to the assembler after having substituted virtual registers with actual registers.

In other words, this is want you want:

Code: Select all

asm volatile ( "int 0x0" ); /* Run interrupt 0x0. */
gsingh2011
Member
Member
Posts: 83
Joined: Tue Feb 03, 2009 11:37 am

Re: Raising interrupts from C

Post by gsingh2011 »

Thanks for the response. That instruction fixed one of the error messages, but the other one is still there:

Code: Select all

/tmp/cc9Ls7p0.s: Assembler messages:
/tmp/cc9Ls7p0.s:42: Error: operand size mismatch for `int'
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Raising interrupts from C

Post by jnc100 »

Immediate values are prefixed by a $ in AT&T e.g.

Code: Select all

asm volatile("int $0x0");
Regards,
John.
gsingh2011
Member
Member
Posts: 83
Joined: Tue Feb 03, 2009 11:37 am

Re: Raising interrupts from C

Post by gsingh2011 »

That works. Thanks for the help.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Raising interrupts from C

Post by sortie »

gsingh2011: Oh sorry, I forgot to add the $ character to my code by mistake. Thanks jnc100 for correcting me. :-)
Post Reply