Jump to content

Mod % Opperator In An Interrupt Routine


Guest Joe
 Share

Recommended Posts

Hi,

 

Using the same code in different threads breaks the linker:

 

<blank project>

 

#include <system.h>

unsigned char a = 5;
unsigned char b = 10;

void interrupt()
{
   if(a % 4)
   {
   }
}

void main()
{
   if(b % 4)
   {
   }
}

 

I think i understand why... as i see it, the compiler is generating a sub-routine that provides the modulus function ( <b> % <b>)

 

As the modulus opperator is a complex one to perform (takes about 10 instructions?), then using a sub-routine makes sence.

-- but the compiler should be able to handle using the same routine within 2 execution paths by saving the stack contents when the interrupt is called?

 

So why does this fail, and how can i fix it?

 

 

 

Joe

Link to comment
Share on other sites

declaring the interrupt routine as inline fixes the problem.. no idea why? I really dont see how an interrupt routine can be inline anyway as you cannot tell where in the code the interrupt will occurr...

 

 

So allowing an interrupt to be inline is a bug? :)

 

ah well, it solved my problem, < for now >

Link to comment
Share on other sites

Joe,

 

Modulus (%) is not coded inline by compiler, it calls a function to do this work.

 

The problem with calling a function is multiple threads has been explained fully before

here.

 

Now the only way for linker to handle this would be to make a copy of each function for each thread of execution. Linker can't currently do this.

 

The answer (for now) is to write a separate function to do the math, and call this from only one thread. I know its not so nice, but it will get around the problem.

 

Regards

Dave

Link to comment
Share on other sites

I've worked arround the issue by setting global bit flags in my interrupt (thread), and processing them in the main thread. This way the mod % is only called in the main thread.

 

 

Cheers,

 

 

Joe

Link to comment
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...