Keil Logo

Register Banks

In all members of the 8051 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

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


      ; FUNCTION falarm (BEGIN)
0000 C0D0       PUSH  PSW
0002 75D018     MOV   PSW,#018H
                      ; SOURCE LINE # 6
                      ; SOURCE LINE # 7
0005 0500   R   INC   alarm_count+01H
0007 E500   R   MOV   A,alarm_count+01H
0009 7002       JNZ   ?C0002
000B 0500   R   INC   alarm_count
000D  ?C0002:
                      ; SOURCE LINE # 8
000D D3         SETB  C
000E 9200   E   MOV   alarm,C
0010 9200   E   MOV   ?alfunc?BIT,C
0012 120000 E   LCALL alfunc
                      ; SOURCE LINE # 9
0015 D0D0       POP   PSW
0017 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.


  • 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.