ARM Technical Support Knowledge Articles

CALLING FUNCTIONS FROM INTERRUPTS

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I have created an interrupt service routine as follows:

void isr (void) interrupt 0 using 1
{
.
.
.
}

which calls another function declared as follows:

void doit (void)
{
.
.
.
}

The code inside the doit function fails and causes strange results. What's going on?

ANSWER

Your interrupt service routine operates in register bank 1. The doit function, however, assumes register bank 0. This is the problem.

The reason this is a problem has to do with the way the compiler generates register to register move instructions. On the 8051, there is no register to register move. So, the compiler generates register to memory moves instead. Since the compiler knows the registerbank, the physical address of a register in a register bank can be calculated. For example, when the compiler calculates the address of R2 in register bank 0, the address is 0x02. If the register bank selected is not really 0, then the function overwrites this register and another part of the program will likely crash.

This technique of accessing a register using its absolute address is called absolute register addressing.

In the example above, the doit function is looking in the wrong place for its register values.

To solve this problem, you may do one of the following:

  1. Use the REGISTERBANK directive to specify that doit uses register bank 1. No code is generated in the function to switch the register bank. For example:
    #pragma registerbank(1)
    void doit (void)
    {
    .
    .
    .
    }
    
  2. Use the NOAREGS directive to specify that the compiler no use absolute register addressing. This makes the function register bank independant so that it may be called from multiple functions using different register banks. However, the code generated is larger because register to register moves are not allowed.

SEE ALSO

Article last edited on: 2005-07-09 09:52:47

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential