Page 1 of 1

C Funcitons from Assembly

Posted: Sat Aug 13, 2011 3:57 am
by mark3094
Hi,

I'm calling a C function from an assembly interrupt handler.
The interrupt handler looks like this:

Code: Select all

_isr4:
    cli
    push byte 0    ; Error code
    push byte 4    ; Interrupt number
    jmp isrhandler
Which jumps to a default handler:

Code: Select all

isrhandler:
    mov eax, _isr
    call eax
	add esp, 8
    iret
Which calls a C function:

Code: Select all

void isr(ubyte isr, ubyte err) {
	printf ("Interrupt %c", isr + 48);
}
So far this works just fine. However, if I try to do something like printf("%d", isr), the system crashes.
Is this somehow related to the data in isr is being popped from the stack, or is there more likely something wrong with my printf function?

I have not yet remapped the PIC, and I have not enabled hardware interrupts. It will print '6' a lot before crashing, so it looks like it's generating interrupt 6 (invalid opcode)...

Thankyou

Re: C Funcitons from Assembly

Posted: Sat Aug 13, 2011 4:31 am
by mark3094
On further research, it looks like it is related to my printf function, as it happens under other circumstances as well.
I will follow up on this thread if I find anything I think will be useful to anyone else

Re: C Funcitons from Assembly

Posted: Sat Aug 13, 2011 4:48 am
by gerryg400
One thing you must do it to make sure the direction flag is set the way your compiler expects when entering your kernel. I think that newer GCC versions rely on the direction flag being cleared on function entry. Your compiler may be the same.

Re: C Funcitons from Assembly

Posted: Sat Aug 13, 2011 8:46 pm
by mark3094
I have found the problem. It is not related to calling C functions from assembly as the title of this thread suggests.

The problem is with compiler options. I'm using Visual Studio 2010, and I had the 'Program Database' Debug Information Format option (/Zi) turned on.

Once I changed it to 'Program Database for Edit and Continue' (/ZI) it started working correctly.

There are other options that this implies, and I think one of those was causing the problems I was experiencing.

http://msdn.microsoft.com/en-us/library/958x11bc.aspx

Re: C Funcitons from Assembly

Posted: Sat Aug 13, 2011 9:03 pm
by Owen
Generally this kind of thing implies a bug in your code that the change of options has covered up, and will in the long run come back to haunt you.

I'm betting register corruption. There are far more registers which need saving across an interrupt than you are doing.

Re: C Funcitons from Assembly

Posted: Mon Aug 15, 2011 4:19 am
by mark3094
Good point.
This is only my first crude attempt though. I just want to get something which basically works, then expand on it from there.

I will of course, take your advide and push a lot more registers to the stack for my real interrupts (will pusha / popa cover it?)