The MASM Forum

General => The Campus => Topic started by: StarsInTheSky on May 08, 2015, 02:29:29 PM

Title: Inline Assembly
Post by: StarsInTheSky on May 08, 2015, 02:29:29 PM
this is about using assembly to optimize some program in another language. I was under the impression this was easily done in c/c++.

Reading Reading https://msdn.microsoft.com/en-us/library/4ks26t93.aspx it says inline assembly is not supported in x64 computers anymore.

really ? is this true?  :(

Removed [Resolved]   :P
Title: Re: Inline Assembly
Post by: yq8 on May 08, 2015, 03:00:32 PM
Bullshit ^^
I've a x64 system and inline assembly works flawlessy :)
Msdn is talking about special processors I think.
Title: Re: Inline Assembly
Post by: rrr314159 on May 08, 2015, 03:45:30 PM
MSDN means it's not supported in latest releases of Visual Studio and Visual C++. The X64 processor will of course run .exe's developed with inline assembly using some other C++ compiler; it doesn't know what the source code was. And, most other C++'s do support inline

Disclaimer: I don't use C++, so if u know different I apologize; but I think I've got this right. I figure, yq8, you're not using 64-bit MS VS and VC++
Title: Re: Inline Assembly
Post by: anta40 on May 08, 2015, 04:29:36 PM
MS VC++ doesn't support inline assembly in X64 anymore.
Well you have 2 options :
1. Write both C/C++ and assembly code separately, and then link them together
2. Switch to X86. Inline assembly is still supported.


Title: Re: Inline Assembly
Post by: rrr314159 on May 08, 2015, 04:54:10 PM
Or, use a non-MS C++ 64-bit compiler, that does support inline. There must be plenty of them
Title: Re: Inline Assembly
Post by: StarsInTheSky on May 08, 2015, 05:00:11 PM
thank you all :)
Title: Re: [Resolved] Inline Assembly
Post by: MichaelW on May 08, 2015, 07:40:04 PM
FWIW, for x64 Pelles C does not support inline assembly.
Title: Re: Inline Assembly
Post by: Vortex on May 09, 2015, 04:40:33 AM
They try to avoid the assembly language. This the reason of the lack of the inline asm support.
Title: Re: Inline Assembly
Post by: rrr314159 on May 09, 2015, 05:27:13 AM
Fortunately, "the Intel C Compiler 15 has inline capability in 64bit too." - according to Silvio at stackoverflow.com (http://stackoverflow.com/questions/6166437/64bit-applications-and-inline-assembly). Unfortunately it costs big bucks
Title: Re: Inline Assembly
Post by: habran on May 09, 2015, 05:51:33 AM
Inline assembly is not the best approach to programming
In MSVC you can combine asm sources with C++ sources and run at the same time and you can debug it on a source level
You can use JWasm with JWasm targets
Title: Re: Inline Assembly
Post by: MichaelW on May 09, 2015, 07:58:12 PM
It took a long time for me to discover all of the necessary details, but Intel-syntax inline assembly works with a recent 64-bit version of gcc. Note that I did not bother determining if the processor actually supports RDRAND.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

unsigned long long int rval;

int main(void)
{
    int i;
    printf("%d\n\n",sizeof(rval));
    for(i=0;i<100;i++)
    {
        __asm__
        (
            "0:"
            "rdrand rax;"
            "jnc    0b;"
            "mov    rval, rax;"
        );
        printf("%I64x\n",rval);
    } 
    getch();
}



set path=C:\Program Files\mingw-w64\x86_64-4.9.2-win32-seh-rt_v4-rev2\mingw64\bin

gcc.exe -std=c99 -pedantic -Os -m64 -masm=intel test.c -o test.exe

pause


I specified Intel syntax in my batch file, but it's also possible to do it directly in the inline assembly by bracketing the inline assembly with appropriate directives:


{
    __asm__
    ( 
        ".intel_syntax noprefix;"
...
        ".att_syntax prefix;" 
    );
    getch();
}


And I started out to do this as inline assembly in a "naked" function (no compiler-generated prologue or epilogue), but while that capability is supported for some hardware platforms, it's not supported for x86 or x86-64.

Edit: Added a zip file that includes the C source, my batch file, the assembly output, and the EXE.