Page 1 of 1

Raising interrupts from C

Posted: Mon Oct 28, 2013 9:03 am
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'

Re: Raising interrupts from C

Posted: Mon Oct 28, 2013 11:55 am
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. */

Re: Raising interrupts from C

Posted: Mon Oct 28, 2013 12:14 pm
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'

Re: Raising interrupts from C

Posted: Mon Oct 28, 2013 2:16 pm
by jnc100
Immediate values are prefixed by a $ in AT&T e.g.

Code: Select all

asm volatile("int $0x0");
Regards,
John.

Re: Raising interrupts from C

Posted: Mon Oct 28, 2013 2:24 pm
by gsingh2011
That works. Thanks for the help.

Re: Raising interrupts from C

Posted: Mon Oct 28, 2013 5:25 pm
by sortie
gsingh2011: Oh sorry, I forgot to add the $ character to my code by mistake. Thanks jnc100 for correcting me. :-)