This project is the final part of the programming and computer architecture course by Ilya Dedinsky (aka ded32).
This program is a binary translator from bytecode generated by my assembler into x86-64 machine code. Execution of the machine code is implemented as a JIT-compiler. You can find information about my processor instructions and their nasm and x86-64 equivalents here (pay special attention to in and out).
Binary_Translator is released for Linux only.
Step 1: Clone this repository.
git clone [email protected]:KetchuppOfficial/Binary_Translator.git
cd Binary_Translator
Step 2: Clone submodule.
git submodule init
git submodule update
Step 3: Build the project.
username@machine:~/Binary_Translator$ make
Collecting dependencies for "src/Binary_Translator.c"...
Collecting dependencies for "src/main.c"...
Compiling "src/main.c"...
Compiling "src/Binary_Translator.c"...
Building library...
ar: creating My_Lib.a
Linking project...
Some options are supported:
- You can turn on all MY_ASSERT macros from My_Lib (kind of debug mode):
make OPT=-DDEBUG
- Some basic compiler options such as optimization flag -O2 can be used:
make OPT=-O2
- Binary translator supports stress test mode (program is executed for 10 000 000 times):
make OPT=-DSTRESS_TEST
- It's possible to use previous options simultaneously:
make OPT=-DDEBUG\ -DSTRESS_TEST\ -O2 # don't forget backslash!
Step 4: Running
make run IN=input_file_name
The program won't work if you don't specify input_file_name.
My virtual processor shows low performance in many cases. Programs on my assembler language are executed indirectly: not on hardware CPU itself but via the C program. Let's try to boost programs written on my assembler by translating them into x86-64 machine code. So the main criterion of binary translation quality is the execution boost.
The goal is to compare execution time and find out, how faster the binary translator is comparing to the processor emulator. I carried out the measurements by tool time. The measurement error caused by translation bytecode into machine code (~0.002) is negligible comparing to the whole execution time.
The performance of binary translator and processor emulator was tested on two programs. The first one solves the good old quadratic equation 124.1 * x^2 - 2345.8 * x + 294.4 = 0. You can find source code here. The second one calculates factorial of 10. You can find source code here.
Quadratic equation | Factorial | |
---|---|---|
Emulator execution time, s | 74.7 +/- 0.4 | 120.8 +/- 0.5 |
Hardware CPU execution time, s | 3.276 +/- 0.003 | 5.71 +/- 0.01 |
Performance boost, times | 22.8 +/- 0.1 | 21.2 +/- 0.1 |
First of all, it's reasonable to perform some optimizations on the machine code before execution. It will reduce the number of instructions and, consequently, the performance will increase.
The second and the last, this task can be continued it terms of generating an ELF file as a result of binary translation.