3 ms·
All you really need is an instruction set reference, such as: https://www.felixcloutier.com/x86/index.html https://www.felixcloutier.com/x86/index.html and have
by WalterBright 3y ago
All you really need is an instruction set reference, such as:
https://www.felixcloutier.com/x86/index.html https://www.felixcloutier.com/x86/index.html
and have a compiler that supports an inline assembler, like the D compiler, with Intel assembler syntax and use it like:
private uint asmBitswap32(uint x) @trusted pure
{
asm pure nothrow @nogc { naked; }
version (D_InlineAsm_X86_64)
{
version (Win64)
asm pure nothrow @nogc { mov EAX, ECX; }
else
asm pure nothrow @nogc { mov EAX, EDI; }
}
asm pure nothrow @nogc
{
// Author: Tiago Gasiba.
mov EDX, EAX;
shr EAX, 1;
and EDX, 0x5555_5555;
and EAX, 0x5555_5555;
shl EDX, 1;
or EAX, EDX;
mov EDX, EAX;
shr EAX, 2;
and EDX, 0x3333_3333;
and EAX, 0x3333_3333;
shl EDX, 2;
or EAX, EDX;
mov EDX, EAX;
shr EAX, 4;
and EDX, 0x0f0f_0f0f;
and EAX, 0x0f0f_0f0f;
shl EDX, 4;
or EAX, EDX;
bswap EAX;
ret;
}
}
The compiler will handle all the program setup and teardown, and you can just concentrate on the assembler part. You can also compile programs with the -vasm switch and the compiler will emit the asm corresponding to the code:
int square(int x) { return x * x; }
compiling:
dmd -c test.d -vasm
prints:
_D4test6squareFiZi:
0000: 0F AF C0 imul EAX,EAX
0003: C3 ret
By trying simple expressions like `x * x` and looking at what the compiler generates, and looking at the instructions in the referenced link, you'll get the hang of it pretty quick.
- JonChesterfield 3y agoReading that makes the GCC inline asm format look really ugly. Some envy. Too used to inline asm looking like __asm__ volatile("syscall" : "=a"(ret) : "a"(n), "D"(a0), "S"(a1), "d"(a2), "r"(r10), "r"(r8), "r"(r9) : "rcx", "r11", "memory");
- sebazzz 3y agoYou could also assemble the assembly using an assembler into a separate object file and export a symbol from there, and let the linker do the job, right?
- bitwize 3y agoGCC inline assembly looks incredibly cursed. Back in the day, the Borland tool suite (Turbo/Borland C++, Turbo Pascal) had inline assembly that looked more like the D compiler example above. GCC does, however, know what to do with a .s file, so you can write your assembly routines outside your C(++) source and just compile them in like a C module, which is what I did last time I was hardcore slinging x86 opcodes.
- JonChesterfield 3y agoIt's the attempt to tell the host language what you're doing with the arguments that makes a real mess. Module scope is roughly the same as putting it in a separate file, i.e. less horrible. C++ has better string literal escaping options which would help. It's very easy to get the constraints wrong and have the aggregate still "work", until unrelated changes months later perturb the register allocation slightly such that it no longer runs as hoped. It's documented and usable but working with it is never a very good time.
- WalterBright 3y agoThe inline asm syntax was designed to match the Intel asm documentation. A hidden feature of the D inline assembler is the compiler knows which registers are read/written, so there's no need for the programmer to explicitly specify it. The compiler will also figure out the right addressing mode for local variables, so the programmer is relieved of that, too.
- JonChesterfield 3y agoDeriving the read/write behaviour from the instruction definition is so far superior to the gcc approach that I wonder how we ended up here. That is a very good call by the D toolchain.
- WalterBright 3y agoIt's a small investment into the compiler to add it, and a large payoff for the user to not have bugs from manually doing it. I proposed doing the same for printf - have the compiler example the arguments and insert the appropriate formats in the format string.
- clausecker 3y agoOn the other hand, this inline assembly meshes very well with the register allocator and instruction scheduler in modern compilers. So it's perfect for teaching the compiler about the few special purpose instructions it doesn't know about without compromising the performance of the rest of the code. The one in the parent comment does not; it's basically a black box and the compiler has to save and restore the entire state of the function around it. Total performance killer unless you write large chunks of code in inline assembly (and then you're probably better off just using an assembly file).
- P_I_Staker 3y agoInline assembly arguably not needed, and of debatable merit, vs. writing assembly code. > an instruction set reference I think the programming manual just as important, especially if you're working with mixed asm and c code. One way to think about it: the C programming language has a runtime environment that is everyone's responsibility to maintain. A bug-free compilation should ensure this is protected. Without the all seeing machine help, it's all on you not to interfere with this environment by following the specified calling conventions. Also, don't try to get too tricky. I saw a really bad bug where someone thought it wise to use a macro, to place a (C language) add operation directly before a function, then it calls it, and first instruction checks the overflow flag. The thing is C has no idea what you're doing, and doesn't guarantee that it will happen like that. In fact the assembly language couldn't even do certain register swaps for function calls without clearing the flag. So, basically it "seemed to work" for years. A new compiler caused it to fail testing. Yes, this was a terrible idea and you should no better, but it's important to understand this challenging to predict and "testing resistant" risk. Extreme misuse can help illustrate the point. P.S. I actually have come to favor asm code for asm, for the writing of asm code that is not trivial. It's mostly a preference and a style thing. Developers are more likely to think about the fact that the code has to play nice with C, instead of assuming some help. You should know better, but it can feel like everything is playing nice.