Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FPU + external C file - how to return double

Status
Not open for further replies.

stebel

Programmer
Apr 20, 2005
4
PL
Hi

I'm working on a piece of assembly procedure that would enable me to return a double value (generated in asm module) to C code.

Up to now never produced such a code, so would be grateful if you could assist me a bit. I know that I should leave the value in st(0)

currently wrote a piece of C :

#include <stdio.h>

extern double test(void) ;

void main ( void )
{
double p ;
p = test () ;
printf ( "The num is %f \n", p ) ;
}

When I write and leave purre integer there's no problem. But how to leave 64bit value in ST ?

I start with

finit
fld... and here comes the problem

thanks for any assistance
Stebel.
 
Hi Stebel,

this is an example of how to interact ftp assembly with c language (the example is not mine) but it works fine using visual studio 6.0 c++

Code:
void main(void)
{
int angle = 0;
int radian = 180;
double ttable[46];
double numa = 311.0001;
double numb = 1.0001;
_asm {

	mov	esi, 0	; index register init
	mov	cx, 46	; 46 values
	finit		; inits fpu
	fld		numa
more:	fadd	numb
	fstp	ttable[esi] ; store results
	fld	ttable[esi]
	add	esi, 08d
	loop	more
	fwait

	}

	cout << "numa		Add \n\n";
	for (int i = 0; i<46; i++) {
		cout << i << "\t" << ttable[i] << "\n";
	}

Hope this helps,

Rick

 
Hi Rick and thank you for your interest.

Actually I was thinking of sth different.

Here's a piece of code :
C side:
Code:
#include <stdio.h>

extern float test(void) ;

void main ( void )

{
float p ;
p = test () ;
printf ( "Double numer is %f \n", p ) ;
}
Asm side:
Code:
.386
PUBLIC _test

_DATA SEGMENT WORD PUBLIC 'DATA'

Num dq (?)

_DATA ENDS


_TEXT SEGMENT WORD PUBLIC 'CODE'

ASSUME CS: _TEXT,DS:_DATA

_test PROC

	finit
	mov Num,345
	fild Num

ret
_test ENDP
_TEXT ENDS
END

And I wanna compile it just like tcc C_code.c ASM_code.asm

but the problem is that I do not know how to move correctly value to ST register. The one with mov Num,345 doesn't work.

Directly I intend to get this value from char array passed to asm procedure. There it would be transformed to value, put into ST and return into double in C.

But just tried this tiny piece of code and have trouble from scratch. I guess I'm missing sth important but not know what yet ??

Thanks


 
Hi Stebel,

The code you are showing me will not work because you are not returning the fp value from the stack to the "C" part of the program

Why don´t you try passing the parameters to the test function as a reference?

e.g. test(double Num)... or test(float Num)

by the way, you have to be careful with the precision of the floating points you are using... floats or doubles,

hope this helps,

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top