Keil Logo

Register Banks

In all members of the 251 family, the first 32 bytes of DATA memory (0x00-0x1F) is grouped into 4 banks of 8 registers each. Programs access these registers as R0-R7. The register bank is selected by two bits of the program status word, PSW.

Register banks are useful when processing interrupts or when using a real-time operating system because the MCU can switch to a different register bank for a task or interrupt rather than saving all 8 registers on the stack. The MCU can then restore switch back to the original register bank before returning.

The using function attribute specifies the register bank a function uses. For example:

void rb_function (void) using 3
  {
  .
  .
  .
  }

The argument for the using attribute is an integer constant from 0-3. Expressions with operators are not allowed. The using attribute is not allowed in function prototypes. The using attribute affects the object code of the function as follows:

  • The currently selected register bank is saved on the stack at function entry.
  • The specified register bank is set.
  • The former register bank is restored before the function is exited.

The following example shows how to specify the using function attribute and what the generated assembly code for the function entry and exit looks like.

stmt level  source

1          extern bit alarm;
2          int alarm_count;
3          extern void alfunc (bit b0);
4
5          void falarm (void) using 3  {
6   1        alarm_count++;
7   1        alfunc (alarm = 1);
8   1      }


ASSEMBLY LISTING OF GENERATED OBJECT CODE

;       FUNCTION falarm (BEGIN)
                               ; SOURCE LINE # 5
000000 C0D0           PUSH     PSW
000002 75D118         MOV      PSW1,#018H
                               ; SOURCE LINE # 6
000005 7E3500      R  MOV      WR6,alarm_count
000008 0B34           INC      WR6,#01H
00000A 7A3500      R  MOV      alarm_count,WR6
                               ; SOURCE LINE # 7
00000D D3             SETB     C
00000E D200        E  SETB     alarm
000010 D200        E  SETB     ?alfunc?BIT
000012 120000      E  LCALL    alfunc
                               ; SOURCE LINE # 8
000015 D0D0           POP      PSW
000017 22             RET
;       FUNCTION falarm (END)

In the previous example, the code starting at offset 0000h saves the initial PSW on the stack and sets the new register bank. The code starting at offset 0015h restores the original register bank by popping the original PSW from the stack.

Note

  • The using attribute may not be used in functions that return a value in registers. You must exercise extreme care to ensure that register bank switches are performed only in carefully controlled areas. Failure to do so may yield incorrect function results. Even when you use the same register bank, functions declared with the using attribute cannot return a bit value.

The using attribute is most useful in interrupt functions. Usually a different register bank is specified for each interrupt priority level. Therefore, you could assign one register bank for all non-interrupt code, a second register bank for the high-level interrupt, and a third register bank for the low-level interrupt.

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.