CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    __asm equivalent for 64-bit programming

    I'm trying to update a program (previously built only as 32-bit) so that it'll build as 64-bit in VS2019. One of the functions uses this small section of assembly language - which is somehow connected to finding out some stuff about the FPU:-

    Code:
    char *buf = *fxbuf;
    __asm {
    	mov eax, buf
    		fxsave   [eax]
    		};
    But AFAICT __asm is only valid for 32-bit compilation. Is there something equivalent for 64-bit ?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: __asm equivalent for 64-bit programming

    AFAIK, you can't use in-line assembler code with a 64bit compile. You'll need to add an assembler language file to the existing project.

    For a starter, see https://docs.microsoft.com/en-us/cpp...?view=msvc-160
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: __asm equivalent for 64-bit programming

    Why not use an exposed api to "find out something about the fpu"? Then asm is not required.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: __asm equivalent for 64-bit programming

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: __asm equivalent for 64-bit programming

    Some good reading material there - many thanks guys. I guess I might have missed something but I'm surprised VC++ has nothing equivalent to an __asm64 instruction

    And it got me wondering if this is a Microsoft specific restriction or if it affects other compilers for Windows/x86/x64. For example there's a compiler called minGW (which can also handle inline assembler) but I've no idea if it has the same restriction.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: __asm equivalent for 64-bit programming

    The idea is, at least on Windows, is for user level apps should not be directly accessing the hardware, but should access hardware through a device driver.

    Microsoft has been steadily locking this down on the OS level starting from Win 2000. It wouldn't surprise me for them to no longer support asm calls at the compiler level.

  7. #7

    Re: __asm equivalent for 64-bit programming

    Thanks for the interesting content on 64-bit programming.

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: __asm equivalent for 64-bit programming

    I've just been reading about X64 intrinsics which are apparently intended to help with the lack of support for inline assembly. This was the original code - does any know if there's an intrinsic function available which would do the same thing??

    Code:
    char *buf = *fxbuf;
    __asm {
    	mov eax, buf
    		fxsave   [eax]
    		};
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: __asm equivalent for 64-bit programming

    Probably this one https://software.intel.com/sites/lan.../#text=_fxsave which was suggested in my post #4.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: __asm equivalent for 64-bit programming

    Apologies 2kaud - I hadn't yet got my head around it back then!!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  11. #11
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: __asm equivalent for 64-bit programming

    Dumb question coming up...

    https://software.intel.com/sites/lan.../#text=_fxsave

    Can you see what the difference is between _fxsave() and _fxsave64() ? Is _fxsave() intended to accept a 32-bit memory pointer maybe??

    [Edit...] I just came across this description (for the original asm code):- https://www.felixcloutier.com/x86/fxsave - which states:-

    Additionally, there are two different layouts of the FXSAVE map in 64-bit mode, corresponding to FXSAVE64 (which requires REX.W=1) and FXSAVE (REX.W=0).
    Any idea what that all means ???
    Last edited by John E; January 6th, 2021 at 06:32 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: __asm equivalent for 64-bit programming

    Originally, how is buf/fxbuf used in the code?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: __asm equivalent for 64-bit programming

    Here's how it looks:-

    Code:
    char** fxbuf = 0;
    
    #ifndef HAVE_POSIX_MEMALIGN
    	fxbuf = (char **) _aligned_malloc (sizeof (char *), 16);
    	assert (fxbuf);
    	*fxbuf = (char *) _aligned_malloc (512, 16);
    	assert (*fxbuf);
    #else
    	(void) posix_memalign ((void **) &fxbuf, 16, sizeof (char *));
    	assert (fxbuf);
    	(void) posix_memalign ((void **) fxbuf, 16, 512);
    	assert (*fxbuf);
    #endif
    
    	memset (*fxbuf, 0, 512);
    
    	char* buf = *fxbuf;
    	__asm	{
    			mov eax, buf
    			fxsave   [eax]
    		};
    
    	uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: __asm equivalent for 64-bit programming

    OK - so mxcsr_mask is byte 28, which is byte 3 of the MXCSR register (lower 8 bits of the 4 byte MXCSR).

    See https://software.intel.com/content/w...or-states.html

    The documentation for MXCSR is on page 250 of https://software.intel.com/content/w...-3d-and-4.html

    It looks like mxcsr_mask is used to determine if any errors have occurred (ie divide by zero, underflow, overflow etc).

    How is mxcsr_mask used??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: __asm equivalent for 64-bit programming

    Quote Originally Posted by 2kaud View Post
    How is mxcsr_mask used??
    It looks like it's for determining whether floating point denormals are enabled. Here's what comes next:-

    Code:
    	uint32_t mxcsr_mask = *((uint32_t*) &((*fxbuf)[28]));
    
    	/* if the mask is zero, set its default value (from intel specs) */
    
    	if (mxcsr_mask == 0) {
    		mxcsr_mask = 0xffbf;
    	}
    
    	if (mxcsr_mask & (1<<6)) {
    		_flags = Flags (_flags | HasDenormalsAreZero);
    	}
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured