Transferred from: http://blog.sina.com.cn/s/blog_a575eb9401014tam.html
How is the RAM space allocated when the program is running in a single-chip microcomputer?
I am now on a program to reduce the use of on-chip RAM optimization, there are some effects, but also produced some questions, here to ask you prawns:
First, the phenomenon:
1, I am in by reducing the global variables, function within the use of variables, reduce the function of parameter transfer and other means to optimize, found that some time I reduce the use of a variable, Keil compiled results show that data is reduced by one byte, sometimes this has been reduced the use of several variables, the data value has not changed, Then reduce the use of variables, will be one of the reduction, and then not reduced.
2, I block the program after some code compiled, showing that the data is increased instead.
Second, the question:
1, SCM program run time, internal RAM is if the allocation of management, the data value is composed of what parts?
2, how to explain the two phenomena described earlier?
3, the data value is not included in the stack space, then the program runs the stack space about how much, and what factors, can not be estimated?
4. What is the limit of the amount of RAM used in monolithic microcomputer? (refers to Keil compiled data value should not be more than how much)
A: How the RAM space is allocated when the program is running in a single-chip microcomputer
1, the allocation of RAM is related to the compilation mode you choose, you can look at the compiler's manual, and then open the resulting distribution table carefully control the source program, you should be able to find the law.
2, still related to the compilation mode, usually changes in the number of global variables can be immediately reflected in the length of the data segment, but if the local variable is specified with the stack, it is not necessarily reflected in the length of the data segment.
3. The stack space is related to the allocation of your RAM space, which is determined at connection time, as specified in the link description file.
4, the use of RAM is of course related to the size of your microcontroller RAM.
Sorry, I am not familiar with the Keil environment, I can not help you to explain the specific to the Keil, the above is the basic principle, every C language environment is like this.
Thank you, ordinary people! If you have the chance to go to Guang fo area, I invite you to drink!
Keil compilation Mode I choose the small:variables in data mode, we generally should be this mode. Variables are defined in the Data/idata area.
Keil compilation results and compiler itself, even if it is a different compiler, in memory allocation is not a common method of compliance?
Or can you describe the memory allocation method of one of the other compilers?
Usually the change in the number of global variables can be reflected immediately on the length of the data segment, but if the local variable is specified with a stack, it does not necessarily reflect the length of the data segment.
Re: Do you mean that the use of space used by local variables is not necessarily included in the compilation results? I compiled the result is more than 200 bytes of data, by what composition?
Ram use limit is related to SCM, that is like my SCM chip RAM is 256, then I use the space (that is, Keil compiled results) the limit is how much, or related? Which is the usual practice, how much space do I leave on the stack?
Another question:
What is the size of the bin file and hex file generated by the same program?
I hear the bin will be half the hex?
What is the difference between the two files in use?
How variables are allocated in memory
Usually the ram area of a microcontroller can be divided into 3 classes, short address area, long address area and external address area.
The short address area generally refers to the 00-ff between the 8-bit address, the long address area generally refers to the 0100-FFFF must use more than 8-bit address area, the external address area refers to the CPU external bus access to the area, different regions have different instruction addressing methods, for example:
MOV A, 40H; Access short Address area
MOV A, @DPTR; access to the long address area
There is no external address area in the General 51.
A stack area is also divided according to the purpose.
The different storage allocation modes determine that the global variables are placed in that area, and the instructions for accessing the short address area may be shorter and faster than the instructions for accessing the long address area, but the long address area can accommodate more variables, especially large arrays.
Local variables have two allocation strategies, one is placed on the stack, because the local variable is only valid in his function, out of the function of the stack can be cleared out of the local variables occupy space, the space can be reused. This strategy reduces the use of local variables and reflects a decrease in memory consumption because memory consumption is dynamic.
Another strategy is to parse the function call relationship and place the local variable in a specific memory area, as in the following example:
Func1 ()
{
CHAR C1, C2;
....
}
Func2 ()
{
CHAR x1, x2, x3;
...
}
Main ()
{
Func1 ();
Func2 ();
....
}
The compiler discovers that func1 and Func2 do not have a call relationship, they assign C1 and X1 to the same address, C2 and X2 are assigned to the same address, and X3 is assigned to another address, so processing can be more efficient than the Stack method. When you lose C1, there is no reduction in the total amount of memory.
Therefore, the length of the stack depends on your program's call relationship, the use of local variables and other factors, according to the actual situation.
As for the difference between the bin and the hex file, my understanding bin is to store the executable code in binary form, while the hex file holds the executable code in ASCII form;
0x12, 0x34, 0x56 bin file
1 2 3 4 5 6 hex File
What do you mean their size is?
I'm sorry, I can only talk so much, and then I'll be able to write a book.
How is the RAM space allocated when the program is running in a single-chip microcomputer?