This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Passing float variables to a C-callable assembly routine

Hello all

A quick question about calling an assembly function from C on a C6713 DSK.

I've already looked at sections 7.3 to 7.5 in http://www.ti.com/lit/ug/spru187o/spru187o.pdf.

I am writing a function to multiply two complex numbers together in assembly, but first I wanted to be able to access two floating point numbers and find the product using the MPYSP instruction.

When I step through my program I can't see the values I thought I passed to my assembly (2.1 and 1.3) in the registers A4 and B4 as expected, I get 1.067869798e+09 and 1.074161254e+09 instead. I don't know what I am doing wrong.

See below for my code.

Many thanks,
Jack

				.global		_cmplx_mult_sp	;ASM function callable from C
_cmplx_mult_sp:	
				;LDW		.D1	A4,A3		; Load first floating point number? - not sure if this is required
				;LDW		.D2	B4,A2		; Load second floating point number?
				;NOP 		4
				MPYSP 		A4,B4,B3		; Multiply the two floating point numbers
				NOP 		5
				.end

Simplified C code:

extern float cmplx_mult_sp(float x, float y);

void main(void)

{

float test = 0.0, a = 2.1, b = 1.3;
test = cmplx_mult_sp(a,b);

}

  • Have you considered writing linear assembly instead?  That's much easier.  This is documented in the chapter titled Using the Assembly Optimizer in the C6000 compiler manual.

    Jack Harris1 said:
    When I step through my program I can't see the values I thought I passed to my assembly

    How are you looking at the values?  Assembly step (not C step) all the way to the MPYSP instruction.  Does that change things?

    One other note ... You need to return the result in A4.

    Thanks and regards,

    -George

  • Hi George,

    Thanks for reply.

    I've written a linear assembly routine which works, thanks for your help.

    Jack

    ps. I wondered if there was a way of returning the real and imaginary parts of the complex number? The code below only returns the real part.

    					.def	_cmplx_mult_sp;
    _cmplx_mult_sp:		.cproc 	a_0,b_0
    					.reg 	a,b,c,d,aminusb,res_r,res_i
    					LDW *a_0++,a			;Load variables from structure
    					LDW *a_0++,b			;
    					SUBSP a,b,aminusb		;
    					NOP 3
    					LDW *b_0++,c			;
    					LDW *b_0++,d			;
    					;NOP 3
    					MPYSP aminusb,d,aminusb	;
    					NOP 3
    					SUBSP c,d,res_r					;
    					MPYSP res_r,a,res_r				;
    					NOP 3
    					ADDSP c,d,res_i	
    					MPYSP res_i,b,res_i	
    					NOP 3
    					ADDSP res_i,aminusb,res_i		;
    					ADDSP res_r,aminusb,res_r		;
    					
    										
    					.return res_r
    					.endproc	

  • You don't need any NOP instructions in linear assembly.  The tools schedule the instructions for you.

    Jack Harris1 said:
    I wondered if there was a way of returning the real and imaginary parts of the complex number? The code below only returns the real part.

    A .cproc directive, similar to a function call in C, can only return one argument.  One idea to consider ... Pass in a pointer to a memory location and copy the complex and real parts of the result into that memory location.

    Thanks and regards,

    -George