Calling a C ISR Handler from a wrapper

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
srg

Calling a C ISR Handler from a wrapper

Post by srg »

Hi

I'm just trying to get my timer interrupt to call a C function to, at the moment, just increment the second charachter in video memory (don't worry about the numbers).

isr_wrapper_32:
      pushad
      call exception_31
      mov al, 0x20
      out 0x20, al
      popad
      IRET

This asm works fine and I can increment from here If I wanted to.

void exception_31(void)
{
   unsigned char *vid_ptr = 0;

   vid_ptr = 0xb8002;

   *vid_ptr++;
}

But here, nothing seems to happen. What is happening to the stack when I'm in an ISR as using this local variable doesn't seem to work. Or am I not supposed to use local vars in ISRs??

Or have I missed something when calling the C function?

Thanks
srg
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Calling a C ISR Handler from a wrapper

Post by Candy »

srg wrote: *vid_ptr++;
You might want to put that in brackets.

Code: Select all

(*vid_ptr)++;
Shouldn't be necessary though.

The compiler should complain about you assigning an int to a pointer. Try:

Code: Select all

unsigned char *vid_ptr = (char *)0xB8002;

(*vid_ptr)++;
srg

Re:Calling a C ISR Handler from a wrapper

Post by srg »

:o :o :o It worked

Blimey, I didn't know that made such a difference!

Anyway now that's sorted. I noticed that with initalising the pointer you did:

unsigned char *vid_ptr = (char *)0xB8002;

You made a type cast. Now I know this is trictly off topic but, when assigning values to pointers (where they point to), why does GCC give a warning when you don't use a cast?

Before GCC I used Borland C++ 3 for DOS, it never gave any warnings when it came to assigning addresses to pointers without casts?

It seems odd to me that you should give a char cast to a char pointer when the addresses it holds (in GCC's case) a 32-bits.

Thanks
srg
User avatar
Candy
Member
Member
Posts: 3883
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Calling a C ISR Handler from a wrapper

Post by Candy »

srg wrote: Anyway now that's sorted. I noticed that with initalising the pointer you did:

unsigned char *vid_ptr = (char *)0xB8002;

You made a type cast. Now I know this is trictly off topic but, when assigning values to pointers (where they point to), why does GCC give a warning when you don't use a cast?
Because it isn't valid. You mean either:

Code: Select all

bla = (char *)0x1234;
or

Code: Select all

*bla = 0x1234;
when you say

Code: Select all

bla = 0x1234;
the compiler can't guess which you want.
Before GCC I used Borland C++ 3 for DOS, it never gave any warnings when it came to assigning addresses to pointers without casts?

It seems odd to me that you should give a char cast to a char pointer when the addresses it holds (in GCC's case) a 32-bits.
It's safer this way. It at least warns when you make an ambiguous statement or something that could cause an error. Try to learn to code in such a way that when you compile with

Code: Select all

-Wall -W -Werror -pedantic -pedantic-error
that the compiler doesn't give any warning. Note, this doesn't mean your code is good or readable, it means the code isn't ambiguous.
proxy

Re:Calling a C ISR Handler from a wrapper

Post by proxy »

just to let you know, there is a very good reason why *vid_ptr++; didn't work and (*vid_ptr)++; did.

the reason is that the ++ operator always has precidence over the derefence operator, however (this is more notable in C++, than C) because of the way it is defined (returns value it had before incrementing) it tends to act like it has lower precidence in many cases.

for example: *ptr++ = x; acts like "assign x to what ptr points to and increment ptr" but this is not what is actually happening as far as the language definition goes. what is really happening is "copy ptr to a temp value, increment ptr, assign x to what temp points to"

fortunately though, modern compilers are smart enough to know the intent here, so it actually will emit code which decrements after the dereference when it can to save a temp variable.

proxy
Post Reply