Keil Logo

C51: Stack Pointer Initialization in Startup Code


Information in this article applies to:

  • C51 Version 5.50 and later

SYMPTOMS

The stack pointer is initialized at the end of the C startup code in STARTUP.A51. Why is this necessary?

CAUSE

The last few lines of the startup code appear as follows:

IF PBPSTACK <> 0
EXTRN DATA (?C_PBP)
                MOV     ?C_PBP,#LOW PBPSTACKTOP
ENDIF

                MOV     SP,#?STACK-1
                LJMP    ?C_START

After reset, the stack pointer is initialized to 7h. The stack will start growing up from address 8h.

The Keil C compiler uses internal DATA memory for your variables and also allows you to use register banks 1, 2, and 3. If the stack pointer were not adjusted, calls to functions and interrupts would overwrite your variables.

Therefore, the last thing the startup code does is to set the stack pointer to the end of all your internal DATA variables.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  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.