DSPRelated.com
Forums

Calling Assembly within assembly

Started by wilbur March 7, 2007
Hi All,

What precautions does one need to take when calling an
assembly function within the scope of another assembly
function in linear assembly. Does the compiler take
care of this? or do I have to store the values of the
registers that I require?.

Another question I have is...do I need to align the
stack pointer in anyway for C64xx when I enter and
exit the assembly function? My data is getting
corrupted in some way..specifically the upper two
bytes of the LSB data and the lower two bytes of my
packed MSB data. To give you some more information..I
call an assembly function from C. Inside this assembly
function I call another assembly function which does
some scaling routine.Then I return to my program in C.
What I observe here is the next chunk of data i.e lets
say the next 256 data values in my C program itself
has the alignment problem I described above!! Has
anyone faced this issue? Any suggestions will be
helpful. Thanks !

Warm Regards,
Wilbur
Hi All,

A Correction to the data values getting corrupted...
The first byte of data is consistently getting
corrupted for the packed data.The second byte is
correct. I initially mentioned it as
> the upper two bytes of the LSB data and the lower
two bytes of my packed MSB data get corrupted. Sorry
bout tht if someone was thinkin bout this :)

What sort of alignment needs to be done?

Regards,
Wilbur

--- wilbur wrote:

> Hi All,
>
> What precautions does one need to take when calling
> an
> assembly function within the scope of another
> assembly
> function in linear assembly. Does the compiler take
> care of this? or do I have to store the values of
> the
> registers that I require?.
>
> Another question I have is...do I need to align the
> stack pointer in anyway for C64xx when I enter and
> exit the assembly function? My data is getting
> corrupted in some way..specifically the upper two
> bytes of the LSB data and the lower two bytes of my
> packed MSB data. To give you some more
> information..I
> call an assembly function from C. Inside this
> assembly
> function I call another assembly function which does
> some scaling routine.Then I return to my program in
> C.
> What I observe here is the next chunk of data i.e
> lets
> say the next 256 data values in my C program itself
> has the alignment problem I described above!! Has
> anyone faced this issue? Any suggestions will be
> helpful. Thanks !
>
> Warm Regards,
> Wilbur
>
>

____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
Here is a couple of key questions..

1) does the stack pointer have exactly the same value when it returns to the C
program as it did before the call to the assembly program?

2) are the Assembly programs defined with the Pascal interface or the C
interface? This makes a big difference on which program pushes parameters on
the stack. I.E before the call or upon entry to the assembly program.
The same program that pushed the parameters on the stack must be the same one
the pops them back off the stack.

It is a definite plus to use the 'stack frame' facility in your call/return
operations, so the stack pointer is not corrupted across calls/returns.

Yes, you have to handle the saving/restoring, in the assembly functions, of
any registers that are modified in the assembly function and then restore
those registers afterwards.

R. Williams

---------- Original Message -----------
From: wilbur
To: c...
Sent: Mon, 5 Mar 2007 11:22:36 -0800 (PST)
Subject: [c6x] Calling Assembly within assembly

> Hi All,
>
> What precautions does one need to take when calling an
> assembly function within the scope of another assembly
> function in linear assembly. Does the compiler take
> care of this? or do I have to store the values of the
> registers that I require?.
>
> Another question I have is...do I need to align the
> stack pointer in anyway for C64xx when I enter and
> exit the assembly function? My data is getting
> corrupted in some way..specifically the upper two
> bytes of the LSB data and the lower two bytes of my
> packed MSB data. To give you some more information..I
> call an assembly function from C. Inside this assembly
> function I call another assembly function which does
> some scaling routine.Then I return to my program in C.
> What I observe here is the next chunk of data i.e lets
> say the next 256 data values in my C program itself
> has the alignment problem I described above!! Has
> anyone faced this issue? Any suggestions will be
> helpful. Thanks !
>
> Warm Regards,
> Wilbur
------- End of Original Message -------
wilbur,

here is a thought
Have you checked to assure that something (just ahead of the corrupted bytes)
is perhaps getting written past the end of its' data area and thereby writing
on the first bytes of the array that is getting corrupted?

R. Williams

---------- Original Message -----------
From: wilbur
To: c...
Sent: Wed, 7 Mar 2007 13:59:30 -0800 (PST)
Subject: Re: [c6x] Calling Assembly within assembly

> Hi All,
>
> A Correction to the data values getting corrupted...
> The first byte of data is consistently getting
> corrupted for the packed data.The second byte is
> correct. I initially mentioned it as
> > the upper two bytes of the LSB data and the lower
> two bytes of my packed MSB data get corrupted. Sorry
> bout tht if someone was thinkin bout this :)
>
> What sort of alignment needs to be done?
>
> Regards,
> Wilbur
>
> --- wilbur wrote:
>
> > Hi All,
> >
> > What precautions does one need to take when calling
> > an
> > assembly function within the scope of another
> > assembly
> > function in linear assembly. Does the compiler take
> > care of this? or do I have to store the values of
> > the
> > registers that I require?.
> >
> > Another question I have is...do I need to align the
> > stack pointer in anyway for C64xx when I enter and
> > exit the assembly function? My data is getting
> > corrupted in some way..specifically the upper two
> > bytes of the LSB data and the lower two bytes of my
> > packed MSB data. To give you some more
> > information..I
> > call an assembly function from C. Inside this
> > assembly
> > function I call another assembly function which does
> > some scaling routine.Then I return to my program in
> > C.
> > What I observe here is the next chunk of data i.e
> > lets
> > say the next 256 data values in my C program itself
> > has the alignment problem I described above!! Has
> > anyone faced this issue? Any suggestions will be
> > helpful. Thanks !
> >
> > Warm Regards,
> > Wilbur
> >
> >
> >
>
Wilbur-

> What precautions does one need to take when calling an
> assembly function within the scope of another assembly
> function in linear assembly. Does the compiler take
> care of this? or do I have to store the values of the
> registers that I require?.
>
> Another question I have is...do I need to align the
> stack pointer in anyway for C64xx when I enter and
> exit the assembly function? My data is getting
> corrupted in some way..specifically the upper two
> bytes of the LSB data and the lower two bytes of my
> packed MSB data. To give you some more information..I
> call an assembly function from C. Inside this assembly
> function I call another assembly function which does
> some scaling routine.Then I return to my program in C.
> What I observe here is the next chunk of data i.e lets
> say the next 256 data values in my C program itself
> has the alignment problem I described above!! Has
> anyone faced this issue? Any suggestions will be
> helpful. Thanks !

Strongly advise to find a TI assembly code example that calls asm functions, then do
exactly as they do. It's important to follow overall compiler conventions, including
creating .bss space for local variables, using a stack frame (see other post by
Richard), and keeping the stack pointer on 4-byte alignment.

Also, make absolutely sure both asm functions are using only registers "permitted" by
the C compiler. This is well-explained in TI documentation.

Regarding your specific problem, does the first assembly program return to C
correctly? If so, and you call a second one that's just a "stub"; i.e. does nothing
but return, does it still work? If that works, can you call it 1000 times with no
issue? You can continue this type of step-by-step thinking as a 'debug strategy' and
you will soon find your problem(s).

-Jeff
Jeff--

On 08 Mar 2007 04:21:10 -0800, Jeff Brower wrote:
> Wilbur-
>
> Richard), and keeping the stack pointer on 4-byte alignment.

Isnt it 8 byte alignment for stack pointer that is reccommended by TI?

-Bhooshan
On 3/9/07, Richard Williams wrote:
> 8 byte alignment, I don't know, I have seen plenty of C code from the Code
> composer that did not achieve the 8 byte alignment. In fact all they
> achieved was even address boundary.

For data I presume?

Iam talking specifically about the stack pointer. TI recommends that
it ALWAYS be aligned to 8byte boundary. I have a vague recollection of
why but wont go into it now.

--Bhooshan

>
> R. Williams
>
> ---------- Original Message -----------
> From: "Bhooshan Iyer"
> To: "Jeff Brower"
> Cc: Wilbur , c...
> Sent: Thu, 8 Mar 2007 18:24:08 +0530
> Subject: Re: [c6x] Calling Assembly within assembly
>
> > Jeff--
> >
> > On 08 Mar 2007 04:21:10 -0800, Jeff Brower wrote:
> > >
> > >
> > >
> > >
> > >
> > >
> > > Wilbur-
> > >
> > > Richard), and keeping the stack pointer on 4-byte alignment.
> >
> > Isnt it 8 byte alignment for stack pointer that is reccommended by TI?
> >
> > -Bhooshan
> ------- End of Original Message -------
--
-------------------------------
"I've missed more than 9000 shots in my career.
I've lost almost 300 games. 26 times I've been trusted to take the
game winning shot and missed.
I've failed over and over again in my life.
And that is why I succeed."
-- Michael Jordan
--------------------------------
8 byte alignment, I don't know, I have seen plenty of C code from the Code
composer that did not achieve the 8 byte alignment. In fact all they
achieved was even address boundary.

R. Williams

---------- Original Message -----------
From: "Bhooshan Iyer"
To: "Jeff Brower"
Cc: Wilbur , c...
Sent: Thu, 8 Mar 2007 18:24:08 +0530
Subject: Re: [c6x] Calling Assembly within assembly

> Jeff--
>
> On 08 Mar 2007 04:21:10 -0800, Jeff Brower wrote:
> >
> >
> >
> >
> >
> >
> > Wilbur-
> >
> > Richard), and keeping the stack pointer on 4-byte alignment.
>
> Isnt it 8 byte alignment for stack pointer that is reccommended by TI?
>
> -Bhooshan
------- End of Original Message -------
Hello Guys,
Thanks a lot for your inputs. Unfortunately or
fortunately the error was a coding error which was
overwriting those values. Also while using linear
assembly, I dont have to worry about the values of the
registers before calling the assembly function. I
guess the compiler takes care of this. Thanks again!

Regards,
Wilbur
--- Richard Williams wrote:

> 8 byte alignment, I don't know, I have seen plenty
> of C code from the Code
> composer that did not achieve the 8 byte alignment.
> In fact all they
> achieved was even address boundary.
>
> R. Williams
>
> ---------- Original Message -----------
> From: "Bhooshan Iyer"
> To: "Jeff Brower"
> Cc: Wilbur ,
> c...
> Sent: Thu, 8 Mar 2007 18:24:08 +0530
> Subject: Re: [c6x] Calling Assembly within assembly
>
> > Jeff--
> >
> > On 08 Mar 2007 04:21:10 -0800, Jeff Brower
> wrote:
> > >
> > >
> > >
> > >
> > >
> > >
> > > Wilbur-
> > >
> > > Richard), and keeping the stack pointer on
> 4-byte alignment.
> >
> > Isnt it 8 byte alignment for stack pointer that is
> reccommended by TI?
> >
> > -Bhooshan
> ------- End of Original Message -------

____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
Hi Wilbur,

Most probably that the problem with incorrect alignement is
due to an incorrect address calculation or modification.

The rule of thumb for alignments is:

1 byte data addresses are aligned on 1 byte boundary
2 byte data addresses are aligned on 2 byte boundary
etc.

Stack pointer is always aligned on 8 byte boundary (see 8.1.2 note
and 8.5.1 of spru187n, two notes towards the end of the section).

I'd recommend to read spru187n chapter 8 Run-time environment,
sections
8.1.2 System stack
8.3. Register conventions
8.4. Function structure and calling conventions
8.5.1 Using assembly language modules

Please also note a funny cross-reference in sections 8.1.2 and 8.4.3
regarding the system stack :)

Hope this helps,

Andrew

> From: wilbur
> To: c...
> Sent: Wed, 7 Mar 2007 13:59:30 -0800 (PST)
> Subject: Re: [c6x] Calling Assembly within assembly
>
>> What precautions does one need to take when calling an
>> assembly function within the scope of another assembly
>> function in linear assembly. Does the compiler take
>> care of this? or do I have to store the values of the
>> registers that I require?.
>>
>> Another question I have is...do I need to align the
>> stack pointer in anyway for C64xx when I enter and
>> exit the assembly function? My data is getting
>> corrupted in some way..specifically the upper two
>> bytes of the LSB data and the lower two bytes of my
>> packed MSB data. To give you some more information..I
>> call an assembly function from C. Inside this assembly
>> function I call another assembly function which does
>> some scaling routine.Then I return to my program in C.
>> What I observe here is the next chunk of data i.e lets
>> say the next 256 data values in my C program itself
>> has the alignment problem I described above!! Has
>> anyone faced this issue? Any suggestions will be
>> helpful. Thanks !
>