mirror of
https://github.com/avinal/The-Hack-Assembler.git
synced 2026-01-07 21:58:32 +05:30
master
The-Hack-Assembler
Assembler developed to convert The HACK assembly language to machine code.
Important: If you are taking this course then I suggest you to try to write the assembler yourself first, it is not very easy and not very tough too. At least not impossible, because I did it 😘
Get Started with the project
- Directory Structure
. |_include # Contains headers |_src # Contains Source Code and Main |_tests # Unit tests for modules | |_catch # Catch2 Header only Unit Test Files |_example_file # Example HACK Assembly Files - Clone this repo
git clone https://github.com/avinal/The-Hack-Assembler.git - Build and run, provide only HACK Assembly Language file. Don't confuse with
.asmextesion.cd The-Hack-Assembler make ./assembler file.asm - Tests are written using header only unit test framework Catch2. Not needed if you just want to use the project.
make test ./test - Start over after modification
make clean
Tips
In case you want to modify this project, you can use Boost C++ Libraries, they can simplify many tasks needed by this project.
Some Words
This was a nice project and taugh me many things. Given below are some miraculous code snippets that proved to be really useful and are well researched. Hope you will find them useful.
- Check if a
std::stringonly contains digits or not.#include <algorithm> inline bool isNumber(std::string str) { return (!str.empty() && std::all_of(str.begin(), str.end(), ::isdigit)); } - Trim a string of spaces, tabs, carrige return, vertical tabs, newline chars.
inline std::string trim(std::string str) { str.erase(0, str.find_first_not_of(" \r\t\v\n")); //prefixing str.erase(std::min(str.find_first_of(" \r\t\v\n"), str.size())); //surfixing return str; } - Convert a Decimal number to Binary String of certain number of bits. (Most Interesting)
#include <bitset> inline std::string toBinaryString(int decimal) { return std::bitset<16>(decimal).to_string(); // 16 is the number of bits required }
Languages
C++
68.7%
Assembly
31.2%