MCU global variable local variable


If a global variable is defined in C51 , the compiler will specify a special address for the variable in RAM, and the value assigned to the variable in the C program will be stored in this special address. The program operates the variable, first from the special address Take out the stored value from the , and then perform the calculation. Global variables are defined at special addresses in memory with fixed storage locations.

A local variable defined in C51 can have the same name as the global variable, but in this case, the local variable has a higher priority, and the global variable with the same name is temporarily shielded in the function module.


If a local variable is defined in C51, the compiler will allocate the address of the variable to the register group R0~R7. Since it is a local variable, the compiler will use the immediate value assignment statement to assign value to the register Rn representing the variable, and the final calculation result will also be stored in the register group, and the location is arbitrarily specified by the compiler. Because local variables are directly operated with registers, the access speed and computer speed are fast; due to the limited number of registers, if there are too many local variables, the code will become redundant due to frequent register allocation.


The local variable space is the stack space, which is the stack space.

When a local variable is declared, it is in the stack space, not when the function is called, and it is pushed onto the stack.

Define a local variable a, and the compiler will assign the address of a to the register group R0~R7. Since it is a local variable, the compiler will use an immediate value assignment statement to assign a value to the register Rn representing a, and the result of the final calculation will also be stored in the register group, and the location is arbitrarily specified by the compiler.

Define a global variable a, the compiler will specify a special address for the variable a in RAM, and the value assigned to a in the C program will be stored in this special address. When the program manipulates the variable a, it first takes out the stored value from the dedicated address, and then performs the calculation.

The local variables of the program exist in the ( stack ), the global variables exist in the ( static area  ), and the dynamic application data exists in the (  heap ).


Suppose a global variable int i = 0x1234 is defined in keil; the initialization of this i must be after power-on and before the main function. After compiling, debug and execute from address 0. Why don't you see the initialization of i? Some say it is in the init.a51 file. But no clear explanation has been found.

The codes are all programmed in ROM, and the global variables are all in RAM, so they must be initialized when powered on. I know that the compiler must add initialization code before main, but I don't know where to add it? It seems to be invisible in the assembled assembly after debug in keil.

Generally speaking, if global variables have initial values, they will be stored in a certain area of ​​ROM. After power-on, a process of copying from ROM to RAM will be performed... This is the initialization of global variables.

Do you still remember that when you used keil to build a project, there was a prompt: Copy Standard 8051 Startup Code to project folder and add file to project ?" 

This prompt means, whether to add Startup code to the project, Startup code is cpu reset or power-on start A piece of startup code that runs immediately after.
When programming in c, the cpu first finds the Startup code code, and then jumps to the main function entry, so it does not start from the rom 0 address

and the role of the Startup code code is:

1: Clear the on-chip RAM PDATA stack and pointer

2: If there is a global variable, initialize it, if there is no global variable, enter the main function directly







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324487322&siteId=291194637