-
Notifications
You must be signed in to change notification settings - Fork 1
Home
cppasm
is a C++ generator for x86 assembly code.
Generated assembly source code syntax is: AT&T
.
The idea behind this generator is to encapsulate every instruction and CPU register
in its own C++
class. Every class name can be found in the Intel developer's manuals, e.g.
imm8
class represents an 8-bit immediate value, m16
class represents a memory address of
a 16-bit data word.
All instructions are implemented as function objects, e.g.
instruction instance of MOV
can be used like this:
#include "cppasm.h"
int main(int argc, char *argv[])
{
MOV(EAX, EBX);
return 0;
}
cppasm
provides global instances for many instructions and CPU registers, so
EAX
and EBX
are global instances of corresponding CPU registers and the output of the above code is
simply (AT&T
syntax):
mov %ebx, %eax
Take a look at the examples
to see how to use the generator. The examples follow the same pattern. There is a generate.cpp
,
generator for some assembly function in the output subdirectory, and an example.cpp
, a simple
application using generated assembly function.
Following examples are available:
- clflush
- collatz
- cpuid
- div-arithm
- fpuconst
- isbn-10
- isbn-13
- lea-arithm
- macheps
- rc4
- rdtsc
- thousand-nops
- xorshift-rng
- xorshift-rng-2
The generator source code files can be found under src.
I am using mingw-w64 to compile the C++ source files and assemble generated assembly files.
The idea behind testing the generator is to generate as much as possible of supported
instructions and to assemble it using GNU assembler.
The test is successful, if the assembler does not complain.
Additionally, the generated assembler code has been reviewed and added to the git repository,
so git status
output must be checked for any changes.
The tests are divided into 32-bit
and 64-bit tests.
The test code is also compiled with
Visual Studio 2022, but
the source code is just compiled only and the assembly source code output is ignored, because
the AT&T
syntax is not compatible with Visual Studio tools.
Improved tests can be found using googletest framework.
This C++ generator does not attempt to convince people to write assembly source code. Programmers, who write functions or programs in assembly language, have their reasons for it.
I hope, anyone, a hobby programmer or someone, who is just tired thinking about portability of his code all the time, will find this generator helpful.