C Funcitons from Assembly

Programming, for all ages and all languages.
Post Reply
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

C Funcitons from Assembly

Post 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
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: C Funcitons from Assembly

Post 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
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: C Funcitons from Assembly

Post 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.
If a trainstation is where trains stop, what is a workstation ?
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: C Funcitons from Assembly

Post 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
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: C Funcitons from Assembly

Post 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.
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: C Funcitons from Assembly

Post 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?)
Post Reply