Page 1 of 1

Calling Interrupts in C

Posted: Sun Jun 10, 2007 12:16 pm
by t0xic
Hey everyone,

I have a quick question... How do you call interrupts through C code, i.e.

Code: Select all

asm("int 80");
Thanks,

--t0xic

Posted: Sun Jun 10, 2007 12:39 pm
by earlz
didn't you just solve your own question?
if that wasn't it, you can call an interrupt in GCC like

Code: Select all

__asm(".intel_syntax noprefix\n"
"int 0x80\n"
".att_syntax");
umm..yea

Posted: Sun Jun 10, 2007 12:43 pm
by t0xic
Sorry I left out my problem. When I try to call int's like that, it either doesn't work or the compiler whines about it

--t0xic

EDIT:

had a "for" loop in front of asm call -.-

Posted: Sun Jun 10, 2007 1:04 pm
by earlz
have you setup the IDT? and what is the specific line you use for the asm? if you use your method, gcc will probably complain, but mine, last time I checked, worked..

Posted: Sun Jun 10, 2007 1:14 pm
by t0xic
Yes I have a gdt set up, and yours works. Is there any way I could set up a

Code: Select all

int_call( int i )
{ 
    asm(".intel_syntax noprefix\n"
	"int [i] \n"
	".att_syntax");
}

code? I have tried this and gcc complains about improper syntax.

Thanks,

--t0xic

Posted: Sun Jun 10, 2007 1:15 pm
by neon
Compilier "whines" about it?

Different compiliers use different synthax for assembly.
ie:

Code: Select all

asm ("int 0x3");

__asm ("int 0x03");

__asm {
   int 0x03
};

__asm__ ("int 0x03", : : );
etc...

*edit: Ah, GCC uses

Code: Select all

__asm__ ("int 0x03", : : );

Posted: Sun Jun 10, 2007 1:26 pm
by earlz
t0xic wrote:Yes I have a gdt set up, and yours works. Is there any way I could set up a

Code: Select all

int_call( int i )
{ 
    asm(".intel_syntax noprefix\n"
	"int [i] \n"
	".att_syntax");
}

code? I have tried this and gcc complains about improper syntax.

Thanks,

--t0xic

I said IDT not GDT..(though you have to have GDT also)
GCC doesn't recognize local variable symbols..though it does recognize global symbols(also, you'd have to have a leading underscore to make it _i)

One (untested) way would be to

Code: Select all

__asm(".intel_syntax noprefix\n"
"mov al,[esp+8]\n"
"int al\n"
".att_syntax");
It's been so long I messed with the stack, I've forgot which way the stack goes for popping..lol

I think that is right, though I'm not sure..

Posted: Sun Jun 10, 2007 1:36 pm
by Aali
bad bad bad hckr83

gcc (in contrast to many other compilers) actually has a useful, elegant way to merge inline asm with C code

i suggest you check the relevant manual entry, its a long read and may not be the easiest thing to understand, but you'll need it if you want to produce safe, working, semi-portable (as in, doesn't break if some inner working of the compiler changes) inline asm

Posted: Sun Jun 10, 2007 1:37 pm
by t0xic
Sorry, I have an idt set up too. I have interrupt handlers, and all of that fun stuff, I was trying to implement software interrupts.

This code doesn't work, but I'll keep messign around:

Code: Select all


int_call( int i )
{
    asm(".intel_syntax noprefix\n"
   "int [i] \n"
   ".att_syntax");
}

Thanks again,

--t0xic

Posted: Sun Jun 10, 2007 1:50 pm
by jnc100
This works okay:

Code: Select all

	__asm volatile ( "int $0x80" : : "a" (0x0), "c" (0x1), "d" (0x0) );
Not sure about what happens if you put it in intel syntax, but in gcc's default (at&t) you need to prefix all immediate operands by a '$'. The above calls int 0x80 with eax set to 0, ecx to 1 and edx to 0.

Regards,
John.

Posted: Sun Jun 10, 2007 2:01 pm
by earlz
or you could do that...I just hate AT&T syntax :P

Posted: Sun Jun 10, 2007 2:14 pm
by t0xic
@John: That works great, but like hckr, I don't have an appreciation for at&t syntax

Thanks everyone

--t0xic

Posted: Sun Jun 10, 2007 4:57 pm
by pcmattman
t0xic wrote:@John: That works great, but like hckr, I don't have an appreciation for at&t syntax
You will, young padawan. I remember when I said I hated AT&T syntax, now I think it's the easiest to read.

And if you're using GCC, you may as well get used to AT&T, because that's what inline assembly is meant to use.