Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Gammastrahler

asm: Problem with register

This topic is 5772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i want to get the return value from a function out of register ST0, a floating point value. from Game Programming Gems, i have copied this code:
      
__declspec (naked) DWORD GetST0(void)
{
	DWORD	f;
	
	__asm
	{
		fstp	dword ptr [f]
		mov		eax, dword ptr [f]
		ret
	}
}
      
unfortunately, the code does not work! i always get zero as the return result if i call Call_cdecl(&myArgs, numArgs, (DWORD)MyFunction); float myFloat = (float)GetST0(); what is wrong here? i have absolutely no concept of assembly language, so i can´t figure this out. Would be very nice if someone could help me on this problem! Thanks Gammastrahler P.S. by the way why are i´m not able to declare the function header of GetST0() in a header file?? this is very bad since more source files need access on it. i get the error: "naked" only allowed for function definitions (code C2488). [edited by - Gammastrahler on August 29, 2002 3:28:00 AM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Gammastrahler
Hi, i want to get the return value from a function out of register ST0, a floating point value.



As far as I can remember, STO is not a register but a command.

quote:

P.S. by the way why are i´m not able to declare the function header of GetST0() in a header file?? this is very bad since more source files need access on it. i get the error: "naked" only allowed for function definitions (code C2488).

[edited by - Gammastrahler on August 29, 2002 3:28:00 AM]


You are able too, just don''t put specify that it''s naked in the declaration. As for your error, I never tried using inline ASM in a project. I usually use an external assembler.



"DaHjajmajQa''jajHeghmeH!"

Cyberdrek
danielc@iquebec.com
Founder
Laval Linux

/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash

Share this post


Link to post
Share on other sites
st0 is a register, the one at the top of the floating point stack. As for your original problem, I don''t get why you would ever want to do what you did there, and I''m stumped on your error.

Share this post


Link to post
Share on other sites
Firstly - what the hell are you trying to do!?!?

Secondly, here is a solution to your problem....



        
#include <iostream>


inline void setST0( float value )
{
__asm fld value
}

inline float getST0(void)
{
float f;
__asm
{
fstp f
}

return f;

}


int main( void )
{

setST0( 1.555f );

std::cout << getST0( ) << std::endl; // should print 1.555


getchar( );

return 0;

}


I think the problem with your version is that you are declaring it as a naked function which according to the VC++ help:

quote:

To ensure that no initialization code for local variables appears before the prolog sequence, initialized local variables are not permitted at function scope. In particular, the declaration of C++ objects is not permitted at function scope. There may, however, be initialized data in a nested scope.



In your code you have a variable declared there which i'm guessing never gets intialised.

The other problem that I can see is that you are trying to return the value in eax, which as far as I can remember is the wrong way of returning a floating point value.


[edit]

And to answer you other question about the header file thing:
when you declare it in the header file, just do:

extern float getST0( void );

and don't put in the "__declspec( naked )" bit..

i think that is what is causing your error



[edited by - Jx on August 29, 2002 9:07:19 AM]

Share this post


Link to post
Share on other sites
@Jx:

thanks, it works fine with your code!

The code i used before was directly copied from the Book "Game Programming Gems". Therefore i was wondering that it didn´t work.

I use these calling of procedures for my scripting engine, it´s just more convenient than having something like

typedef int (*MyProc) (param1, param2,.... paramN);

thanks
Gammastrahler

Share this post


Link to post
Share on other sites
I''ve just had a look in my copy of Game Programming Gems, and sure enough - the code there is exactly the same as Gammastrahler posted.

I do think that this is a bug with the code from the book so beware.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!