NewNew 发表于 2009-4-10 07:54:47

CodevisionAVR局部变量如何分配内存空间

马老师您好!CVAVR里局部变量如何分配内存空间,它会不会占用全局变量的存储空间

machao 发表于 2009-4-12 19:45:00

请阅读CVAVR的帮助文件,说的非常清楚。

SRAM Memory Organization

A compiled program has the following memory map:

分配图略

The Working Registers area contains 32x8 bit general purpose working registers.
The compiler uses the following registers: R0, R1, R15, R22, R23, R24, R25, R26, R27, R28, R29, R30 and R31.
Also some of the registers from R2 to R15 may be allocated by the compiler for global and local bit variables. The rest of unused registers, in this range, are allocated for global char and int variables and global pointers.
Registers R16 to R21 are allocated for local char and int variables.

The I/O Registers area contains 64 addresses for the CPU peripheral functions as Port Control Registers, Timer/Counters and other I/O functions. You may freely use these registers in your assembly programs.

The Data Stack area is used to dynamically store local variables, passing function parameters and saving registers R0, R1, R15, R22, R23, R24, R25, R26, R27, R30, R31 and SREG during interrupt routine servicing.
The Data Stack Pointer is implemented using the Y register.
At start-up the Data Stack Pointer is initialized with the value 5Fh (or FFh for some chips)+Data Stack Size.
When saving a value in the Data Stack, the Data Stack Pointer decrements.

When the value is retrieved, the Data Stack Pointer is incremented back.
When configuring the compiler, in the Project|Configure|C Compiler|Code Generation menu, you must specify a sufficient Data Stack Size, so it will not overlap the I/O Register area during program execution.

The Global Variables area is used to statically store the global variables during program execution. The size of this area can be computed by summing the size of all the declared global variables.

The Hardware Stack area is used for storing the functions return addresses.
The SP register is used as a stack pointer and is initialized at start-up with value of of the _HEAP_START_ -1 address.
During the program execution the Hardware Stack grows downwards to the Global Variables area.

When configuring the compiler you have the option to place the strings DSTACKEND, respectively HSTACKEND, at the end of the Data Stack, respectively Hardware Stack areas.

When you debug the program with AVR Studio you may see if these strings are overwritten, and consequently modify the Data Stack Size using the Project|Configure|C Compiler|Code Generation menu command.

When your program runs OK you may disable the placement of the strings in order to reduce code size.

The Heap is a memory area located between the Hardware Stack and the SRAM end.

It is used by the memory allocation functions from the Standard Library: malloc, calloc, realloc and free.

The Heap size must be specified in the Project|Configure|C Compiler|Code Generation menu.

It can be calculated using the following formulae:

where:        n is the number of memory blocks that will be allocated in the Heap

block_sizeiis the size of the memory block i

If the memory allocation functions will not be used, then the Heap size must be specified as zero.
页: [1]
查看完整版本: CodevisionAVR局部变量如何分配内存空间