r/C_Programming • u/Salat_Leaf • Oct 10 '26
Is there a way to inline assembly without compiler extensions?
GCC/Clang have __asm__, MSVC/ICC got __asm. Is it possible to inline assembly independently from compiler extensions? I need to retrieve random seed number on PowerPC and RISC-V, depending on the target architecture of course
17
Upvotes
12
u/awidesky Oct 10 '26
Unlike in C++, inline assembly is treated as an extension in C. It is conditionally supported and implementation defined, meaning that it may not be present and, even when provided by the implementation, it does not have a fixed meaning.
Source
11
u/BlockOfDiamond Oct 10 '26
Without any compiler extensions, and without linking separate object files: You can't. There is no way. There is nothing in the C standard about inline assembly.
25
u/runningOverA Oct 10 '26
You can write the assembly and compile it with an assembler to create an obj file (.o) and then link with it as if that was written in C.
That's the standard way and most of the C standard library is written like this.
2
u/P-p-H-d Oct 10 '26
no
2
u/FUZxxl Oct 10 '26
No.
2
u/didntplaymysummercar Oct 10 '26
You could write out your assembly into memory (binary, by hand from output of real assembler or godbolt or something) and then mark it executables and call it like a function (cast pointer to memory to a function).
Technically UB but it's like a baby's first jit. Requires nothing asm related from the compiler but you need mmap and mprotect (or virtual alloc and so on on windows) to do it.
It's also not as pretty in code as inline assembly is.
1
u/viva1831 Oct 10 '26
It looks like not. You'll probably want a header from hell defining different macros depending on the compiler and falling back to a slower function call to an assembled object file if the compiler isn't supported :/
That said: you're likely to end up depending on a particular assembler anyway, so limiting yourself to one form of compiler isn't necessarily the worst choice here
1
u/sciencekm Oct 10 '26
You can use direct machine code. Here is an example of RDTSC on x86. You need to replace the machine code bytes in this example with whatever is appropriate for your target platform. In this example, the machine code are two instructions - (1) rdtsc, and (2) ret. Essentially, this is a function.
-1
u/Radiant-Ad-9073 Oct 10 '26
Move the assembler code out of the C code; mixed language source code is Bad.
For each architecture you support, write the assembler code that deals with C calling conventions and does the thing you need. Compile this into an appropriate .o file.
Then basically add a "getrandomseed.h" header file that defines the function headers matching the functions in the .o. Depending on the architecture link against different versions of the .o -- you end up doing much the same that i.e. stdio.h does.
Alternatively, you *could* with enough voodoo and #defines define macro's that call the right flavour of inlining depending on the architecture you're compiling on; but it's fragile. If you ever change compiler that whole lot of hairy macro stuff will need redoing, and it's typically a pain.
0
u/CounterSilly3999 Oct 10 '26
Define a wrapper macro, depending on the compiler.