DSPRelated.com
Forums

C runtime environment initialization

Started by ivan...@hotmail.com June 26, 2008
Hi,

I have to call a C function from a SHARC assembly routine.
I have read about assembly and C interfacing in C/C++ Compiler
and Library manual for SHARC where I came across the ccall macro.
Using this macro I can pass parameters to the C function through
registers r4 and r8. I can also see that the result appears
in registers r0. However, the program does not return from the C
function due to incorrect values in the stack pointer(i7) and frame
pointer(i6). The manual says that the C runtime environment must be initialized properly and that's probably what's missing in my program. This is what it says about setting up the stack:

"The stack must be set up correctly before calling a C/C++-callable function.If you call other functions, maintaining the basic stack model also facilitates the use of the debugger. The easiest way to do this is to define a C or C++ main program to initialize the run-time system; hold the stack until it is needed by the C/C++ function being called from the assembly language program; and then hold that stack until it is needed to call back into C/C++, making sure the dedicated registers are correct. You do not need to set the FP prior to the call; the caller’s FP is never used by the callee."

I don't quite understand this. Does this mean that I just have to have a C main function and then the compiler will take care of initialization or the C main function has to call an initialization routine? Also, the main routine in my program is currently written in assembly and I would rather leave it like that. Is there another way to initialize C runtime environment?

Thanks,
Ivan
First off, I've never mixed C and assembler on the sharc, so I'm kinda
guessing here.

It sounds like you need to start with a C main. It's really just a
wrapper for the compiler. You might try something like

extern main_asm();

main()
{
main_asm();
}

and all the rest of your code is assembler for the main function.
I don't know what it means by "hold the stack until it is needed"
Maybe "don't touch those registers or you'll be sorry!"
:-)

Patience, persistence, truth,
Dr. mike

On Thu, 26 Jun 2008 i...@hotmail.com wrote:

> Hi,
>
> I have to call a C function from a SHARC assembly routine.
> I have read about assembly and C interfacing in C/C++ Compiler
> and Library manual for SHARC where I came across the ccall macro.
> Using this macro I can pass parameters to the C function through
> registers r4 and r8. I can also see that the result appears
> in registers r0. However, the program does not return from the C
> function due to incorrect values in the stack pointer(i7) and frame
> pointer(i6). The manual says that the C runtime environment must be
>initialized properly and that's probably what's missing in my program.
>This is what it says about setting up the stack:
>
> "The stack must be set up correctly before calling a C/C++-callable
>function.If you call other functions, maintaining the basic stack model
>also facilitates the use of the debugger. The easiest way to do this is
>to define a C or C++ main program to initialize the run-time system; hold
>the stack until it is needed by the C/C++ function being called from the
>assembly language program; and then hold that stack until it is needed to
>call back into C/C++, making sure the dedicated registers are correct.
>You do not need to set the FP prior to the call; the caller’s FP is
>never used by the callee."
>
> I don't quite understand this. Does this mean that I just have to have a C main function and then the compiler will take care of initialization or the C main function has to call an initialization routine? Also, the main routine in my program is currently written in assembly and I would rather leave it like that. Is there another way to initialize C runtime environment?
>
> Thanks,
> Ivan
>
Hi Ivan,
I am including a simple example. In the example given,c file calls
an assembly function,which inturn calls a c function.the c function
compares two variables.

------
//C code Example
#include
void asm_func (void) ;

void c_func(void);

int c_var ;

extern int asm_var ;

int c_var=10;
main()
{
asm_func();

}

/*-------------------------*/
void c_func(void)
{

if(c_var!=asm_var)
exit(1);
else
exit(0);
}

//End of C code Example

-----
//ASM code Example

#include

.segment /dm seg_dmda;
.var _asm_var=0;
.global _asm_var;

.endseg;

.segment /pm seg_pmco;
.global _asm_func;
.extern _c_func ;
.extern _c_var ;

_asm_func:
entry;
r8=dm(_c_var);
dm(_asm_var)=r8;
ccall(_c_func);
exit;
.endseg;

//End of code Example
----

i used the following to complie both the files
g21k -a primes.ach -g number.asm larnum.c -o larnum.exe

and got the expected results.
hope thsi may be of some help.

regards,
Santanu
Quoting Mike Rosing :

> First off, I've never mixed C and assembler on the sharc, so I'm kinda
> guessing here.
>
> It sounds like you need to start with a C main. It's really just a
> wrapper for the compiler. You might try something like
>
> extern main_asm();
>
> main()
> {
> main_asm();
> }
>
> and all the rest of your code is assembler for the main function.
> I don't know what it means by "hold the stack until it is needed"
> Maybe "don't touch those registers or you'll be sorry!"
> :-)
>
> Patience, persistence, truth,
> Dr. mike
>
> On Thu, 26 Jun 2008 i...@hotmail.com wrote:
>
>> Hi,
>>
>> I have to call a C function from a SHARC assembly routine.
>> I have read about assembly and C interfacing in C/C++ Compiler
>> and Library manual for SHARC where I came across the ccall macro.
>> Using this macro I can pass parameters to the C function through
>> registers r4 and r8. I can also see that the result appears
>> in registers r0. However, the program does not return from the C
>> function due to incorrect values in the stack pointer(i7) and frame
>> pointer(i6). The manual says that the C runtime environment must be
>> initialized properly and that's probably what's missing in my program.
>> This is what it says about setting up the stack:
>>
>> "The stack must be set up correctly before calling a C/C++-callable
>> function.If you call other functions, maintaining the basic stack model
>> also facilitates the use of the debugger. The easiest way to do this is
>> to define a C or C++ main program to initialize the run-time system; hold
>> the stack until it is needed by the C/C++ function being called from the
>> assembly language program; and then hold that stack until it is needed to
>> call back into C/C++, making sure the dedicated registers are correct.
>> You do not need to set the FP prior to the call; the caller’s FP is
>> never used by the callee."
>>
>> I don't quite understand this. Does this mean that I just have to
>> have a C main function and then the compiler will take care of
>> initialization or the C main function has to call an initialization
>> routine? Also, the main routine in my program is currently written
>> in assembly and I would rather leave it like that. Is there
>> another way to initialize C runtime environment?
>>
>> Thanks,
>> Ivan
>>
I am now trying to create a C main hoping that the run-time
environment will be taken care of but the linker throws the following
error message:

[Error li1021] The following symbols referenced in processor 'p0' could not be resolved: '__lib_prog_term [___lib_prog_term]' referenced from 'libc36x.dlb[exit2136x_rev_0.0.doj]'

I didn't have this problem when I built a small project consisting
of C files only. Can anyone guess what's going on? I suppose I have to do something in the ldf file or change the compiler settings.

Thanks,
Ivan

Hi,
>
>I have to call a C function from a SHARC assembly routine.
>I have read about assembly and C interfacing in C/C++ Compiler
>and Library manual for SHARC where I came across the ccall macro.
>Using this macro I can pass parameters to the C function through
>registers r4 and r8. I can also see that the result appears
>in registers r0. However, the program does not return from the C
>function due to incorrect values in the stack pointer(i7) and frame
>pointer(i6). The manual says that the C runtime environment must be initialized properly and that's probably what's missing in my program. This is what it says about setting up the stack:
>
>"The stack must be set up correctly before calling a C/C++-callable function.If you call other functions, maintaining the basic stack model also facilitates the use of the debugger. The easiest way to do this is to define a C or C++ main program to initialize the run-time system; hold the stack until it is needed by the C/C++ function being called from the assembly language program; and then hold that stack until it is needed to call back into C/C++, making sure the dedicated registers are correct. You do not need to set the FP prior to the call; the caller’s FP is never used by the callee."
>
>I don't quite understand this. Does this mean that I just have to have a C main function and then the compiler will take care of initialization or the C main function has to call an initialization routine? Also, the main routine in my program is currently written in assembly and I would rather leave it like that. Is there another way to initialize C runtime environment?
>
>Thanks,
>Ivan
>
>
QXJlIHlvdSB1c2luZyB5b3VyIG93biAubGRyIGZpbGU/ICBJZiBzbyB5b3UgbWF5IG5lZWQg
dG8gZXhwbGljaXRseSBhZGQgdGhlCnBhcnRpY3VsYXIgaGVhZGVyIGZpbGUgdG8gdGhlIGxp
YnJhcnkuICBJIGFtIHVzaW5nIHRoZSBBRFNQLTIxMzY0IGFuZCBJIGhhZAp0aGUgc2FtZSBw
cm9ibGVtLiAgSSBhbSB0cnlpbmcgdG8gaW50ZWdyYXRlIEMgY29kZSBpbnRvCmFzc2VtYmx5
IGNvZGUuICBUaGUgYXNzZW1ibHkgY29kZSB1c2VkIGl0J3Mgb3duIC5sZHIgZmlsZSwgd2hl
cmVhcyBJIHdhcwp1c2luZyB0aGUgZGVmYXVsdCAubGRyIGZpbGUgd2hlbiBJIHdhcyBqdXN0
IHVzaW5nIG15IEMgY29kZSBzdGFuZC1hbG9uZS4KV2hlbiBJIGNvbWJpbmVkIHRoZSBDIGFu
ZCBhc3NlbWJseSBjb2RlIEkgaGFkIHRvIGNoYW5nZQphc3NlbWJseSBtYWluIGludG8gYSBm
dW5jdGlvbiBjYWxsIGFuZCBjYWxsIGl0IGZyb20gbXkgQyBtYWluLiAgV2hlbiBJIGRpZAp0
aGlzIEkgZ290IGVycm9ycyB1bnRpbCBJIGFkZGVkIHRoZSBmaWxlICIzNjRfaGRyLmRvaiIg
dG8gbXkgLmxkciBMaWJyYXJ5CmZpbGVzLiAgKFRoaXMgd2lsbCBiZSBkaWZmZXJlbnQgZGVw
ZW5kaW5nIG9uIHRoZSBEU1AgeW91IGFyZSB1c2luZykuCgotQW1pZQoKCgogICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
IAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICBUbyAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgYWRzcEB5YWhvb2dyb3Vwcy5jb20gICAgICAgICAgICAgICAgCiAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgIGNjIAogICAgIGl2YW5fcmFkb2pldmljQGhvdG1hICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICBpbC5jb20gICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFN1Ympl
Y3QgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFthZHNwXSBSZTog
QyBydW50aW1lIGVudmlyb25tZW50ICAgIAogICAgIFNlbnQgYnk6ICAgICAgICAgICAgICAg
ICAgICAgICAgICBpbml0aWFsaXphdGlvbiAgICAgICAgICAgICAgICAgICAgICAKICAgICBh
ZHNwQHlhaG9vZ3JvdXBzLmNvbSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
Q2xhc3NpZmljYXRpb24gCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgIDIwMDgtMDctMDMg
MDg6MDIgUE0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgIFBsZWFzZSByZXNwb25kIHRvICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAg
IGl2YW5fcmFkb2pldmljQGhvdG1hICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgaWwuY29tICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKCgoKCkkgYW0gbm93IHRyeWluZyB0byBj
cmVhdGUgYSBDIG1haW4gaG9waW5nIHRoYXQgdGhlIHJ1bi10aW1lCmVudmlyb25tZW50IHdp
bGwgYmUgdGFrZW4gY2FyZSBvZiBidXQgdGhlIGxpbmtlciB0aHJvd3MgdGhlIGZvbGxvd2lu
ZwplcnJvciBtZXNzYWdlOgoKW0Vycm9yIGxpMTAyMV0gIFRoZSBmb2xsb3dpbmcgc3ltYm9s
cyByZWZlcmVuY2VkIGluIHByb2Nlc3NvciAncDAnIGNvdWxkCm5vdCBiZSByZXNvbHZlZDog
J19fbGliX3Byb2dfdGVybSBbX19fbGliX3Byb2dfdGVybV0nIHJlZmVyZW5jZWQgZnJvbQon
bGliYzM2eC5kbGJbZXhpdDIxMzZ4X3Jldl8wLjAuZG9qXScKCkkgZGlkbid0IGhhdmUgdGhp
cyBwcm9ibGVtIHdoZW4gSSBidWlsdCBhIHNtYWxsIHByb2plY3QgY29uc2lzdGluZwpvZiBD
IGZpbGVzIG9ubHkuIENhbiBhbnlvbmUgZ3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