Calling Interrupts in 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
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Calling Interrupts in C

Post 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
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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 -.-
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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..
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post 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", : : );
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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..
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post 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
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post 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.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

or you could do that...I just hate AT&T syntax :P
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

@John: That works great, but like hckr, I don't have an appreciation for at&t syntax

Thanks everyone

--t0xic
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
Post Reply