EmbeddedRelated.com
Forums
Memfault Beyond the Launch

ram usage clarification

Started by Dan Muzzey August 8, 2005
Hello everyone

 

I've got a basic question about RAM allocation on the MSP430's. 
I'm
using CrossWorks and would like to know how the RAM gets allocated.  I
think I may have a couple of things slightly off.  It looks to me like
static variables get put at the bottom of the ram, followed by Global
variables.  Does the stack follow this? 

 

 

 

First, where do global variables go?

Second, where does the stack go?

Third, would I be correct in saying that local variables get put on the
stack?  I understand that there are several registers as well that may
get used and would be used first but lets pretend that there are to many
variables for the registers.

Fourth, where would static variables within a module end up?

Fifth, where would static variables within a function end up?

 

I know these are pretty basic questions.  Dan M






Beginning Microcontrollers with the MSP430

Hi Dan,

> I've got a basic question about RAM
allocation on the MSP430's.  I'm
> using CrossWorks and would like to know how the RAM gets allocated.  I
> think I may have a couple of things slightly off.  It looks to me like
> static variables get put at the bottom of the ram, followed by Global
> variables.  Does the stack follow this? 

The stack is set to Top of RAM, and grows downwards.
All RAM allocation is from the start upwards.
Any static or Global variables go in RAM without direct stack usage, as the
stack
pointer varies between functions.
Basically a static variable is just like a global variable from MCU point of
view.
The only difference is a matter of scope.
Static variables can only be "seen" in RAM by the functions (or
modules) that have them,
so this is mainly a compile issue.

The order of which things are allocated in RAM can be altered graphically by
looking
at the memory map with editor.
(Project Explorer, click on Project name, then right click -> View Memory
map)

> First, where do global variables go?

As above

> Second, where does the stack go?

As above

> Third, would I be correct in saying that local
variables get put on the
> stack?  I understand that there are several registers as well that may
> get used and would be used first but lets pretend that there are to many
> variables for the registers.

R12/13/14/15 are "scratch" registers - which means the compiler is
free to use those
without saving them (except in an ISR of course)
Even in default Debug configuration, the compiler does well in using registers.
For example, use several local variables to main(), and then right click on a
module in Project Explorer,
and select "disassemble".
The dissambled code shows for each function, which register is used by vars.

Once registers can't be used anymore, then the "stackframe" is
used.
Now variables are accessed by an OFFSET on the current stack pointer.
Eg. if there is one local variable that's not in a register, you will see
references to SP-2, etc.
They also stand out because your disassembled code of a function will start with
the SP being
adjusted first so it can access its stackframe.


> Fourth, where would static variables within a
module end up?

In RAM, they are just like global variables, except that you cannot refer to
them from another
module.
It's just like using static functions.
Eg. what's the difference between :

void my_function (void)
{
....

and 

static void my_function (void)
{
....

Answer :
There is no difference, except that if yoiu were to call "my_function"
from another module,
the compiler will give an error.

The same applies to static variables.

 
> Fifth, where would static variables within a
function end up?

Again, as above, in RAM.
Except that now the scope of this variable will be limited to that function.
If you refer to that variable from another function, no can do...

B rgds
Kris


Dan, 

> I've got a basic question about RAM
allocation on the 
> MSP430's.  I'm using CrossWorks and would like to know how 
> the RAM gets allocated.

From the bottom up for variables with static or extern linkage.  From
the top down for variables with auto linkage.

> I think I may have a couple of 
> things slightly off.  It looks to me like static variables 
> get put at the bottom of the ram, followed by Global 
> variables.  Does the stack follow this? 

There is no distinction between "static" and "global" in
this sense.
The easiest thing is to think of linkage which is "static" and
"extern"
as opposed to "auto" or "typedef".  Any variable declared
static or
extern (or implicitly declared extern by appearing at file scope but
without extern and static) will be put at the bottom of memory assuming
a standard MSP430.

> First, where do global variables go?

Low RAM, 200h and up.

> Second, where does the stack go?

End of memory down, a00h and down (assuming a 2K stack).

> Third, would I be correct in saying that local
variables get 
> put on the stack?

Correct, in all cases if they cannot be held in registers.

> I understand that there are several 
> registers as well that may get used and would be used first 
> but lets pretend that there are to many variables for the registers.
> 
> Fourth, where would static variables within a module end up?

In low RAM.

> Fifth, where would static variables within a
function end up?

In low RAM.

The order of variables in low memory is not defined--you can assign your
own ordering by putting data into specific sections and ordering those
sections in the memory map file.

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and now MAXQ processors

Thanks to both Paul and Kris.  This helped clear things up in my mind.

Dan M

-----Original Message-----
From: msp430@msp4... [mailto:msp430@msp4...] On Behalf Of Paul Curtis
Sent: Monday, August 08, 2005 2:04 PM
To: msp430@msp4...
Subject: RE: [msp430] ram usage clarification

Dan, 

> I've got a basic question about RAM
allocation on the 
> MSP430's.  I'm using CrossWorks and would like to know how 
> the RAM gets allocated.

From the bottom up for variables with static or extern linkage.  From
the top down for variables with auto linkage.

> I think I may have a couple of 
> things slightly off.  It looks to me like static variables 
> get put at the bottom of the ram, followed by Global 
> variables.  Does the stack follow this? 

There is no distinction between "static" and "global" in
this sense.
The easiest thing is to think of linkage which is "static" and
"extern"
as opposed to "auto" or "typedef".  Any variable declared
static or
extern (or implicitly declared extern by appearing at file scope but
without extern and static) will be put at the bottom of memory assuming
a standard MSP430.

> First, where do global variables go?

Low RAM, 200h and up.

> Second, where does the stack go?

End of memory down, a00h and down (assuming a 2K stack).

> Third, would I be correct in saying that local
variables get 
> put on the stack?

Correct, in all cases if they cannot be held in registers.

> I understand that there are several 
> registers as well that may get used and would be used first 
> but lets pretend that there are to many variables for the registers.
> 
> Fourth, where would static variables within a module end up?

In low RAM.

> Fifth, where would static variables within a
function end up?

In low RAM.

The order of variables in low memory is not defined--you can assign your
own ordering by putting data into specific sections and ordering those
sections in the memory map file.

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and now MAXQ processors



.

 
Yahoo! Groups Links



 


Memfault Beyond the Launch