Sign in

username:

password:



Not a member?

Search msp430



Search tips

Subscribe to msp430



Ads

Discussion Groups

Discussion Groups | MSP430 | Passing arrays

The purpose of this group is to foster exchange of information on the Texas Instruments MSP430 family of microcontrollers and related tools. Everyone welcome, all levels of familiarity/expertise.

Passing arrays - Doug - Aug 25 0:44:24 2008

Gentlemen,

I've been designing hardware and writing firmware for years but I'm a
newcomer to the MSP430 family and the IAR toolset. I have a question
about passing arrays as arguments to a function.

Oversimplifying the situation a bit, I have the following situation:

int main()
{
UINT8 Data[32];

GetData(Data);
}

void GetData(UINT8* Data)
{
}

What seems strange to me is that when I have the debugger break just
after entering GetData, and peek at the stack, the entire array of
Data is on the stack. Fortunately, the array is indeed filled
correctly when control is passed back to main. I wouldn't care too
much about this, except that a) I'm in a time-sensitive application
and don't want to waste time pushing stuff onto the stack if I don't
need to, and b) I'm getting odd stack overflow errors and I think this
may be part of the problem.

I've even tried calling the function with "GetData(&Data[0]);", and I
still see the entire array on the stack!

Has anybody else seen this type of behavior? Is there a switch setting
or an optimization I need to change?
------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )


Re: Passing arrays - old_cow_yellow - Aug 25 0:57:43 2008

This has nothing to do with "passing arrays".

Your Data[32] is a local variable inside main(), thus main() allocated
32 bytes on its stack.

When you call GetData(), the address of the argument is passed by R12.

You are using c and yet you do not like the way c works. Either do not
use c or work around it.

--- In m...@yahoogroups.com, "Doug" wrote:
>
> Gentlemen,
>
> I've been designing hardware and writing firmware for years but I'm a
> newcomer to the MSP430 family and the IAR toolset. I have a question
> about passing arrays as arguments to a function.
>
> Oversimplifying the situation a bit, I have the following situation:
>
> int main()
> {
> UINT8 Data[32];
>
> GetData(Data);
> }
>
> void GetData(UINT8* Data)
> {
> }
>
> What seems strange to me is that when I have the debugger break just
> after entering GetData, and peek at the stack, the entire array of
> Data is on the stack. Fortunately, the array is indeed filled
> correctly when control is passed back to main. I wouldn't care too
> much about this, except that a) I'm in a time-sensitive application
> and don't want to waste time pushing stuff onto the stack if I don't
> need to, and b) I'm getting odd stack overflow errors and I think this
> may be part of the problem.
>
> I've even tried calling the function with "GetData(&Data[0]);", and I
> still see the entire array on the stack!
>
> Has anybody else seen this type of behavior? Is there a switch setting
> or an optimization I need to change?
>

------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )

RE: Re: Passing arrays - Microbit_P43000 - Aug 25 3:29:36 2008

True what OCY says, but to complete or concise the answer - you are only
passing a pointer on the stack frame, so you're not wasting CPU cycles
passing the whole array. That's (as you of course know) the whole idea of
passing the pointer in the first place anyway.

So the bottom line is that it doesn't really matter much whether the array
is local to main() or global. Or at least not on an MSP430 and not for the
purpose of this exercise. It permanently uses RAM in both cases, and that
RAM is taking at runtime once you enter main(). On eg. an AVR it would make
a big difference (efficiency wise).
Scope wise it's a different story of course. It's the same principle as what
the difference is between a static local in main() and a global variable.

> need to, and b) I'm getting odd stack overflow errors and I think this
> may be part of the problem.

32 bytes on the stack local to main shouldn't hurt. Surely the default stack
size is much more than that.

I'm not sure whether you're interpreting the debugger properly or whether
it's displaying wrong : The argument is just the pointer - strictly speaking
the debugger should - once it is in your caller function - just display a
single referenced character, not the whole array.

Best Regards,
Kris
-----Original Message-----
From: m...@yahoogroups.com [mailto:m...@yahoogroups.com] On Behalf Of
old_cow_yellow
Sent: Monday, 25 August 2008 2:58 PM
To: m...@yahoogroups.com
Subject: [msp430] Re: Passing arrays

This has nothing to do with "passing arrays".

Your Data[32] is a local variable inside main(), thus main() allocated
32 bytes on its stack.

When you call GetData(), the address of the argument is passed by R12.

You are using c and yet you do not like the way c works. Either do not
use c or work around it.

--- In m...@yahoogroups.com, "Doug" wrote:
>
> Gentlemen,
>
> I've been designing hardware and writing firmware for years but I'm a
> newcomer to the MSP430 family and the IAR toolset. I have a question
> about passing arrays as arguments to a function.
>
> Oversimplifying the situation a bit, I have the following situation:
>
> int main()
> {
> UINT8 Data[32];
>
> GetData(Data);
> }
>
> void GetData(UINT8* Data)
> {
> }
>
> What seems strange to me is that when I have the debugger break just
> after entering GetData, and peek at the stack, the entire array of
> Data is on the stack. Fortunately, the array is indeed filled
> correctly when control is passed back to main. I wouldn't care too
> much about this, except that a) I'm in a time-sensitive application
> and don't want to waste time pushing stuff onto the stack if I don't
> need to, and b) I'm getting odd stack overflow errors and I think this
> may be part of the problem.
>
> I've even tried calling the function with "GetData(&Data[0]);", and I
> still see the entire array on the stack!
>
> Has anybody else seen this type of behavior? Is there a switch setting
> or an optimization I need to change?
>

------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )

Re: Passing arrays - Rick Low - Aug 26 16:59:00 2008

Along with what the others have said, you could also declare the array
as "static UINT8 Data[32]" and get it off the stack altogether.

--rick

--- In m...@yahoogroups.com, "Doug" wrote:
>
> Gentlemen,
>
> I've been designing hardware and writing firmware for years but I'm a
> newcomer to the MSP430 family and the IAR toolset. I have a question
> about passing arrays as arguments to a function.
>
> Oversimplifying the situation a bit, I have the following situation:
>
> int main()
> {
> UINT8 Data[32];
>
> GetData(Data);
> }
>
> void GetData(UINT8* Data)
> {
> }
>
> What seems strange to me is that when I have the debugger break just
> after entering GetData, and peek at the stack, the entire array of
> Data is on the stack. Fortunately, the array is indeed filled
> correctly when control is passed back to main. I wouldn't care too
> much about this, except that a) I'm in a time-sensitive application
> and don't want to waste time pushing stuff onto the stack if I don't
> need to, and b) I'm getting odd stack overflow errors and I think this
> may be part of the problem.
>
> I've even tried calling the function with "GetData(&Data[0]);", and I
> still see the entire array on the stack!
>
> Has anybody else seen this type of behavior? Is there a switch setting
> or an optimization I need to change?
>

------------------------------------



(You need to be a member of msp430 -- send a blank email to msp430-subscribe@yahoogroups.com )