News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Inline Assembly

Started by StarsInTheSky, May 08, 2015, 02:29:29 PM

Previous topic - Next topic

StarsInTheSky

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

yq8

Bullshit ^^
I've a x64 system and inline assembly works flawlessy :)
Msdn is talking about special processors I think.

rrr314159

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++
I am NaN ;)

anta40

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.



rrr314159

Or, use a non-MS C++ 64-bit compiler, that does support inline. There must be plenty of them
I am NaN ;)

StarsInTheSky


MichaelW

FWIW, for x64 Pelles C does not support inline assembly.
Well Microsoft, here's another nice mess you've gotten us into.

Vortex

They try to avoid the assembly language. This the reason of the lack of the inline asm support.

rrr314159

Fortunately, "the Intel C Compiler 15 has inline capability in 64bit too." - according to Silvio at stackoverflow.com. Unfortunately it costs big bucks
I am NaN ;)

habran

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
Cod-Father

MichaelW

#10
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.
Well Microsoft, here's another nice mess you've gotten us into.