Search code examples
c++assemblyclangcompiler-explorer

Can I add my own assembly file/instructions to a godbolt project?


Just like the title suggests. Say, I want to do this with a Godbolt project:

main.cpp

#include <iostream>

extern "C" int test_111();

int main()
{
    int r = test_111();
    
    std::cout << "result=" << r << std::endl;
    return 0;
}

and then in asm1.asm:

.code

test_111 PROC

    ; 64-bit assembly
    xor eax, eax
    inc rax

    ret

test_111 ENDP

END

Trying it under "x86-64 clang" (any version).

But I can't seem to figure out how to do this. Any suggestions?

PS. The goal is to inject my own assembly instructions into the output.


EDIT:

After a suggestion in the comments, I tried the asm volatile key words, but it simply adds whatever gibberish I put in there into the assembly output:

enter image description here


Solution

  • Using the Tree mode, you can set up a CMake project with multiple files, including asm:

    Godbolt Link

    To make it work, I had to enable nasm support in the CMakeLists.txt file and manually set the path to the assembler

    set(CMAKE_ASM_NASM_COMPILER /opt/compiler-explorer/nasm-2.14.02/bin/nasm)
    enable_language(ASM_NASM)
    set(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")