DSPRelated.com
Forums

Wrapper function (C interface to asm routine on Blackfin)

Started by John McDermick July 15, 2011
It's been a while so please bear with me...


If I have some asm routine (lets call it _mysub) which ...just for the
sake of simplicity....adds 1 to what ever is in R2...how do I declare
a wrapper function (or prototype) in C which loads the value I am
calling with into register R2?

I tried doing it like this:


void wrapper(register r2)
{
    _mysub;
}


void main()
{
   wrapper(5);
}



but 5 just gets loaded into R0...




On 7/15/2011 8:31 AM, John McDermick wrote:
> It's been a while so please bear with me... > > > If I have some asm routine (lets call it _mysub) which ...just for the > sake of simplicity....adds 1 to what ever is in R2...how do I declare > a wrapper function (or prototype) in C which loads the value I am > calling with into register R2? > > I tried doing it like this: > > > void wrapper(register r2) > { > _mysub; > } > > > void main() > { > wrapper(5); > } > > > > but 5 just gets loaded into R0... > > > >
I think the answer to that is pretty compiler specific; which are you using? Also, it might be easier to change your ASM routine to conform to the C calling convention for that platform than the other way around. -- Rob Gaddi, Highland Technology Email address is currently out of order

John McDermick wrote:

> It's been a while so please bear with me... >
To begin with, which compiler are you using? There are at least three different ones.
> If I have some asm routine (lets call it _mysub) which ...just for the > sake of simplicity....adds 1 to what ever is in R2...how do I declare > a wrapper function (or prototype) in C which loads the value I am > calling with into register R2?
The VDSP manual has entire chapter about interfacing C to assembly. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
> The VDSP manual has entire chapter about interfacing C to assembly.
Yes, but I wasn't able to find any information about how the C interface function arguments can be manually assigned to registers of the programmer's choice. The compiler is C/C++ Compiler for Blackfin (VisualDSP)
> > I think the answer to that is pretty compiler specific; which are you > using? �Also, it might be easier to change your ASM routine to conform > to the C calling convention for that platform than the other way around. >
If I have 100 asm routines (which at the time of their creation adhered to one calling convention) is moved to a system which has another calling convention, it is probably easier just to change the interfaces of those asm routines instead of changing 100 asm routines? Or am I missing something here?
John McDermick wrote:
>... I wasn't able to find any information about how the C >interface function arguments can be manually assigned to registers of >the programmer's choice. >The compiler is C/C++ Compiler for Blackfin (VisualDSP)
In the general case, that is not possible. (I want to say never, but I have not tried every C compiler under the Sun.) The compiler as a well defined calling mechanism, and the assembly code needs to adapt to it, not the other way around. I only used the SHARC family, not Blackfin, but took a peek at the documentation installed with VisualDSP. Opening "blackfin.compiler.library.mn" and searchin for "assembly" lead me to the section titled "Calling Assembly Subroutines From C/C++ Programs" They do not provide full details here, but they suggest, (and it is a good suggestion,) to write a dummy C function with the same signature of the assembly function you need. Then, inspect the generated assembly code for the function to see how the arguments are accessed *from the stack* Then you can copy them to registers if needed. Other relevant sections (sorry, no page numbers in that help file): "C/C++ Run-Time Model and Environment" and from there to: * Register usage conventions &#4294967295;Dedicated Registers&#4294967295; &#4294967295;Call-Preserved Registers&#4294967295; &#4294967295;Scratch Registers&#4294967295; &#4294967295;Stack Registers&#4294967295; * Program control conventions &#4294967295;Managing the Stack&#4294967295; &#4294967295;Transferring Function Arguments and Return Value&#4294967295; <======= -- Roberto Waltman [ Please reply to the group. Return address is invalid ]
On 07/15/2011 10:43 AM, John McDermick wrote:
> >> >> I think the answer to that is pretty compiler specific; which are you >> using? Also, it might be easier to change your ASM routine to conform >> to the C calling convention for that platform than the other way around. >> > > If I have 100 asm routines (which at the time of their creation > adhered to > one calling convention) is moved to a system which has another calling > convention, it is probably easier just to change the interfaces > of those asm routines instead of changing 100 asm routines? Or am I > missing > something here?
Hey -- now there's a good reason to somehow parameterize your register references! -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
On 07/15/2011 08:31 AM, John McDermick wrote:
> It's been a while so please bear with me... > > > If I have some asm routine (lets call it _mysub) which ...just for the > sake of simplicity....adds 1 to what ever is in R2...how do I declare > a wrapper function (or prototype) in C which loads the value I am > calling with into register R2? > > I tried doing it like this: > > > void wrapper(register r2) > { > _mysub; > } > > > void main() > { > wrapper(5); > } > > > > but 5 just gets loaded into R0...
Does the compiler have an 'asm' pseudo-instruction that lets you insert assembly into your C code? The gnu version is very versatile, and would let you do this. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html

John McDermick wrote:

>>The VDSP manual has entire chapter about interfacing C to assembly. > > > > Yes, but I wasn't able to find any information about how the C > interface function arguments can be manually assigned to registers of > the programmer's choice. > > The compiler is C/C++ Compiler for Blackfin (VisualDSP)
The VDSP environment always places first three arguments of a C-callable function correspondingly to R0,R1,R2 registers. The only way to change that in VDSP is by a GNU-style inline assembly. You can explicitly specify the registers which are used for passing the parameters into inline function. That's bad style and cryptic syntax; I would avoid that. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
On 07/15/2011 10:43 AM, John McDermick wrote:
> >> >> I think the answer to that is pretty compiler specific; which are you >> using? Also, it might be easier to change your ASM routine to conform >> to the C calling convention for that platform than the other way around. >> > > If I have 100 asm routines (which at the time of their creation > adhered to > one calling convention) is moved to a system which has another calling > convention, it is probably easier just to change the interfaces > of those asm routines instead of changing 100 asm routines? Or am I > missing > something here?
Depending on your assembler, perhaps you can write a sed script that changes all register references to something more abstract "reg0, reg1, ...", or if you can more meaningful "param0, param1, scratch0, scratch1, ...". Then make an include file that maps those functional names to the actual registers used by your compiler. This will make the bugs much more subtle and hard to detect, thereby insuring your continued employment for years to come. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html