5 Commits
v1.1 ... master

Author SHA1 Message Date
avinal
57dfa048cf cleaned code 2020-08-22 23:52:20 +05:30
avinal
df2c013ab3 cleaned test files 2020-08-22 23:36:54 +05:30
avinal
07a7f480a8 example files 2020-08-21 19:39:55 +05:30
avinal
56b35b12ca readme updated 2020-08-21 19:39:38 +05:30
avinal
a11558120b gitignore updated 2020-08-21 19:39:23 +05:30
30 changed files with 714 additions and 179 deletions

6
.gitignore vendored
View File

@@ -35,9 +35,3 @@ VMTranslator
*.asm
.vscode
*.zip
FibonacciElement
NestedCall
SimpleFunction
StaticsTest
*.vm
*.zip

View File

@@ -1,2 +1,65 @@
# The-VM-to-HACK-Translator
Translates Virtual Machine Language into HACK Assembly language.
**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
1. Directory Structure
```shell
.
|_include # Contains headers
|_src # Contains Source Code and Main
|_Test-Files # Example HACK VM Files
```
2. Clone this repo
```shell
git clone https://github.com/avinal/The-VM-to-HACK-Translator.git
```
3. Build and run, provide only [HACK VM Language](https://www.nand2tetris.org/project08) file. Don't confuse with `.vm` extesion.
```shell
cd The-VM-to-HACK-Translator
make
./VMTranslator file.vm
```
4. Start over after modification
```shell
make clean
```
## Tips
In case you want to modify this project, you can use [Boost C++ Libraries](https://www.boost.org/), 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.
1. Trim spaces, comments, carrige returns, newline, tab and verticle tabs:
```cpp
inline std::string trim(std::string str)
{
if (str.find('/') != std::string::npos) // comments
{
str.erase(str.find('/'));
}
str.erase(0, str.find_first_not_of(" \r\t\v\n")); //prefixing
str.erase(str.find_last_not_of(" \r\t\v\n") + 1); //surfixing}
return str;
}
```
2. Tokenize a string using C++ with respect to a delimiter, space by defalt (string split):
```cpp
#include <sstream>
#include <vector>
std::vector<std::string> split(std::string str, char delimiter = ' ')
{
std::vector<std::string> tokens;
std::stringstream tokenizer(str);
std::string intermediate;
while (std::getline(tokenizer, intermediate, delimiter))
{
tokens.push_back(intermediate);
}
return tokens;
}
```
3. **Fascinating** Using Lambdas with Map to implement a function or function pointers. [See here](https://github.com/avinal/The-VM-to-HACK-Translator/blob/291b239065fc5cc0921b8592dfcd69c6f3022e52/src/code_writer.cpp#L29)

22
Test-Files/BasicLoop.vm Normal file
View File

@@ -0,0 +1,22 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/ProgramFlow/BasicLoop/BasicLoop.vm
// Computes the sum 1 + 2 + ... + argument[0] and pushes the
// result onto the stack. Argument[0] is initialized by the test
// script before this code starts running.
push constant 0
pop local 0 // initializes sum = 0
label LOOP_START
push argument 0
push local 0
add
pop local 0 // sum = sum + counter
push argument 0
push constant 1
sub
pop argument 0 // counter--
push argument 0
if-goto LOOP_START // If counter > 0, goto LOOP_START
push local 0

31
Test-Files/BasicTest.vm Normal file
View File

@@ -0,0 +1,31 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/07/MemoryAccess/BasicTest/BasicTest.vm
// Executes pop and push commands using the virtual memory segments.
push constant 10
pop local 0
push constant 21
push constant 22
pop argument 2
pop argument 1
push constant 36
pop this 6
push constant 42
push constant 45
pop that 5
pop that 2
push constant 510
pop temp 6
push local 0
push that 5
add
push argument 1
sub
push this 6
push this 6
add
sub
push temp 6
add

View File

@@ -0,0 +1,2 @@
| RAM[0] |RAM[261]|
| 262 | 3 |

View File

@@ -0,0 +1,18 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/FibonacciElement/FibonacciElement.tst
// FibonacciElement.asm results from translating both Main.vm and Sys.vm into
// a single assembly program, stored in the file FibonacciElement.asm.
load FibonacciElement.asm,
output-file FibonacciElement.out,
compare-to FibonacciElement.cmp,
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1;
repeat 6000 {
ticktock;
}
output;

View File

@@ -0,0 +1,17 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/FibonacciElement/FibonacciElementVME.tst
load, // Load all the VM files from the current directory
output-file FibonacciElement.out,
compare-to FibonacciElement.cmp,
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1;
set sp 261,
repeat 110 {
vmstep;
}
output;

View File

@@ -0,0 +1,30 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/FibonacciElement/Main.vm
// Computes the n'th element of the Fibonacci series, recursively.
// n is given in argument[0]. Called by the Sys.init function
// (part of the Sys.vm file), which also pushes the argument[0]
// parameter before this code starts running.
function Main.fibonacci 0
push argument 0
push constant 2
lt // checks if n<2
if-goto IF_TRUE
goto IF_FALSE
label IF_TRUE // if n<2, return n
push argument 0
return
label IF_FALSE // if n>=2, returns fib(n-2)+fib(n-1)
push argument 0
push constant 2
sub
call Main.fibonacci 1 // computes fib(n-2)
push argument 0
push constant 1
sub
call Main.fibonacci 1 // computes fib(n-1)
add // returns fib(n-1) + fib(n-2)
return

View File

@@ -0,0 +1,16 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/FibonacciElement/Sys.vm
// Pushes a constant, say n, onto the stack, and calls the Main.fibonacii
// function, which computes the n'th element of the Fibonacci series.
// Note that by convention, the Sys.init function is called "automatically"
// by the bootstrap code.
function Sys.init 0
push constant 4
call Main.fibonacci 1 // computes the 4'th fibonacci element
label WHILE
goto WHILE // loops infinitely

View File

@@ -0,0 +1,49 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/ProgramFlow/FibonacciSeries/FibonacciSeries.vm
// Puts the first argument[0] elements of the Fibonacci series
// in the memory, starting in the address given in argument[1].
// Argument[0] and argument[1] are initialized by the test script
// before this code starts running.
push argument 1
pop pointer 1 // that = argument[1]
push constant 0
pop that 0 // first element in the series = 0
push constant 1
pop that 1 // second element in the series = 1
push argument 0
push constant 2
sub
pop argument 0 // num_of_elements -= 2 (first 2 elements are set)
label MAIN_LOOP_START
push argument 0
if-goto COMPUTE_ELEMENT // if num_of_elements > 0, goto COMPUTE_ELEMENT
goto END_PROGRAM // otherwise, goto END_PROGRAM
label COMPUTE_ELEMENT
push that 0
push that 1
add
pop that 2 // that[2] = that[0] + that[1]
push pointer 1
push constant 1
add
pop pointer 1 // that += 1
push argument 0
push constant 1
sub
pop argument 0 // num_of_elements--
goto MAIN_LOOP_START
label END_PROGRAM

View File

@@ -0,0 +1,2 @@
| RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] | RAM[5] | RAM[6] |
| 261 | 261 | 256 | 4000 | 5000 | 135 | 246 |

View File

@@ -0,0 +1,65 @@
// Test file for NestedCall test.
load NestedCall.asm,
output-file NestedCall.out,
compare-to NestedCall.cmp,
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[5]%D1.6.1 RAM[6]%D1.6.1;
set RAM[0] 261,
set RAM[1] 261,
set RAM[2] 256,
set RAM[3] -3,
set RAM[4] -4,
set RAM[5] -1, // test results
set RAM[6] -1,
set RAM[256] 1234, // fake stack frame from call Sys.init
set RAM[257] -1,
set RAM[258] -2,
set RAM[259] -3,
set RAM[260] -4,
set RAM[261] -1, // Initialize stack to check for local segment
set RAM[262] -1, // being cleared to zero.
set RAM[263] -1,
set RAM[264] -1,
set RAM[265] -1,
set RAM[266] -1,
set RAM[267] -1,
set RAM[268] -1,
set RAM[269] -1,
set RAM[270] -1,
set RAM[271] -1,
set RAM[272] -1,
set RAM[273] -1,
set RAM[274] -1,
set RAM[275] -1,
set RAM[276] -1,
set RAM[277] -1,
set RAM[278] -1,
set RAM[279] -1,
set RAM[280] -1,
set RAM[281] -1,
set RAM[282] -1,
set RAM[283] -1,
set RAM[284] -1,
set RAM[285] -1,
set RAM[286] -1,
set RAM[287] -1,
set RAM[288] -1,
set RAM[289] -1,
set RAM[290] -1,
set RAM[291] -1,
set RAM[292] -1,
set RAM[293] -1,
set RAM[294] -1,
set RAM[295] -1,
set RAM[296] -1,
set RAM[297] -1,
set RAM[298] -1,
set RAM[299] -1,
repeat 4000 {
ticktock;
}
output;

View File

@@ -0,0 +1,70 @@
// Test file for NestedCall test.
load Sys.vm,
output-file NestedCall.out,
compare-to NestedCall.cmp,
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1 RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[5]%D1.6.1 RAM[6]%D1.6.1;
set RAM[0] 261,
set RAM[1] 261,
set RAM[2] 256,
set RAM[3] -3,
set RAM[4] -4,
set RAM[5] -1, // test results
set RAM[6] -1,
set RAM[256] 1234, // fake stack frame from call Sys.init
set RAM[257] -1,
set RAM[258] -2,
set RAM[259] -3,
set RAM[260] -4,
set RAM[261] -1, // Initialize stack to check for local segment
set RAM[262] -1, // being cleared to zero.
set RAM[263] -1,
set RAM[264] -1,
set RAM[265] -1,
set RAM[266] -1,
set RAM[267] -1,
set RAM[268] -1,
set RAM[269] -1,
set RAM[270] -1,
set RAM[271] -1,
set RAM[272] -1,
set RAM[273] -1,
set RAM[274] -1,
set RAM[275] -1,
set RAM[276] -1,
set RAM[277] -1,
set RAM[278] -1,
set RAM[279] -1,
set RAM[280] -1,
set RAM[281] -1,
set RAM[282] -1,
set RAM[283] -1,
set RAM[284] -1,
set RAM[285] -1,
set RAM[286] -1,
set RAM[287] -1,
set RAM[288] -1,
set RAM[289] -1,
set RAM[290] -1,
set RAM[291] -1,
set RAM[292] -1,
set RAM[293] -1,
set RAM[294] -1,
set RAM[295] -1,
set RAM[296] -1,
set RAM[297] -1,
set RAM[298] -1,
set RAM[299] -1,
set sp 261,
set local 261,
set argument 256,
set this 3000,
set that 4000;
repeat 50 {
vmstep;
}
output;

View File

@@ -0,0 +1,63 @@
// Sys.vm for NestedCall test.
// Sys.init()
//
// Calls Sys.main() and stores return value in temp 1.
// Does not return. (Enters infinite loop.)
function Sys.init 0
push constant 4000 // test THIS and THAT context save
pop pointer 0
push constant 5000
pop pointer 1
call Sys.main 0
pop temp 1
label LOOP
goto LOOP
// Sys.main()
//
// Sets locals 1, 2 and 3, leaving locals 0 and 4 unchanged to test
// default local initialization to 0. (RAM set to -1 by test setup.)
// Calls Sys.add12(123) and stores return value (135) in temp 0.
// Returns local 0 + local 1 + local 2 + local 3 + local 4 (456) to confirm
// that locals were not mangled by function call.
function Sys.main 5
push constant 4001
pop pointer 0
push constant 5001
pop pointer 1
push constant 200
pop local 1
push constant 40
pop local 2
push constant 6
pop local 3
push constant 123
call Sys.add12 1
pop temp 0
push local 0
push local 1
push local 2
push local 3
push local 4
add
add
add
add
return
// Sys.add12(int n)
//
// Returns n+12.
function Sys.add12 0
push constant 4002
pop pointer 0
push constant 5002
pop pointer 1
push argument 0
push constant 12
add
return

22
Test-Files/PointerTest.vm Normal file
View File

@@ -0,0 +1,22 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/07/MemoryAccess/PointerTest/PointerTest.vm
// Executes pop and push commands using the
// pointer, this, and that segments.
push constant 3030
pop pointer 0
push constant 3040
pop pointer 1
push constant 32
pop this 2
push constant 46
pop that 6
push pointer 0
push pointer 1
add
push this 2
sub
push that 6
add

9
Test-Files/SimpleAdd.vm Normal file
View File

@@ -0,0 +1,9 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/07/StackArithmetic/SimpleAdd/SimpleAdd.vm
// Pushes and adds two constants.
push constant 7
push constant 8
add

View File

@@ -0,0 +1,2 @@
| RAM[0] | RAM[1] | RAM[2] | RAM[3] | RAM[4] |RAM[310]|
| 311 | 305 | 300 | 3010 | 4010 | 1196 |

View File

@@ -0,0 +1,29 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/SimpleFunction/SimpleFunction.tst
load SimpleFunction.asm,
output-file SimpleFunction.out,
compare-to SimpleFunction.cmp,
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1
RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[310]%D1.6.1;
set RAM[0] 317,
set RAM[1] 317,
set RAM[2] 310,
set RAM[3] 3000,
set RAM[4] 4000,
set RAM[310] 1234,
set RAM[311] 37,
set RAM[312] 1000,
set RAM[313] 305,
set RAM[314] 300,
set RAM[315] 3010,
set RAM[316] 4010,
repeat 300 {
ticktock;
}
output;

View File

@@ -0,0 +1,16 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/SimpleFunction/SimpleFunction.vm
// Performs a simple calculation and returns the result.
function SimpleFunction.test 2
push local 0
push local 1
add
not
push argument 0
add
push argument 1
sub
return

View File

@@ -0,0 +1,29 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/SimpleFunction/SimpleFunctionVME.tst
load SimpleFunction.vm,
output-file SimpleFunction.out,
compare-to SimpleFunction.cmp,
output-list RAM[0]%D1.6.1 RAM[1]%D1.6.1 RAM[2]%D1.6.1
RAM[3]%D1.6.1 RAM[4]%D1.6.1 RAM[310]%D1.6.1;
set sp 317,
set local 317,
set argument 310,
set this 3000,
set that 4000,
set argument[0] 1234,
set argument[1] 37,
set argument[2] 9,
set argument[3] 305,
set argument[4] 300,
set argument[5] 3010,
set argument[6] 4010,
repeat 10 {
vmstep;
}
output;

45
Test-Files/StackTest.vm Normal file
View File

@@ -0,0 +1,45 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/07/StackArithmetic/StackTest/StackTest.vm
// Executes a sequence of arithmetic and logical operations
// on the stack.
push constant 17
push constant 17
eq
push constant 17
push constant 16
eq
push constant 16
push constant 17
eq
push constant 892
push constant 891
lt
push constant 891
push constant 892
lt
push constant 891
push constant 891
lt
push constant 32767
push constant 32766
gt
push constant 32766
push constant 32767
gt
push constant 32766
push constant 32766
gt
push constant 57
push constant 31
push constant 53
add
push constant 112
sub
neg
and
push constant 82
or
not

17
Test-Files/StaticTest.vm Normal file
View File

@@ -0,0 +1,17 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/07/MemoryAccess/StaticTest/StaticTest.vm
// Executes pop and push commands using the static segment.
push constant 111
push constant 333
push constant 888
pop static 8
pop static 3
pop static 1
push static 3
push static 1
sub
push static 8
add

View File

@@ -0,0 +1,20 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/StaticsTest/Class1.vm
// Stores two supplied arguments in static[0] and static[1].
function Class1.set 0
push argument 0
pop static 0
push argument 1
pop static 1
push constant 0
return
// Returns static[0] - static[1].
function Class1.get 0
push static 0
push static 1
sub
return

View File

@@ -0,0 +1,20 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/StaticsTest/Class2.vm
// Stores two supplied arguments in static[0] and static[1].
function Class2.set 0
push argument 0
pop static 0
push argument 1
pop static 1
push constant 0
return
// Returns static[0] - static[1].
function Class2.get 0
push static 0
push static 1
sub
return

View File

@@ -0,0 +1,2 @@
| RAM[0] |RAM[261]|RAM[262]|
| 263 | -2 | 8 |

View File

@@ -0,0 +1,17 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/StaticsTest/StaticsTest.tst
load StaticsTest.asm,
output-file StaticsTest.out,
compare-to StaticsTest.cmp,
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1 RAM[262]%D1.6.1;
set RAM[0] 256,
repeat 2500 {
ticktock;
}
output;

View File

@@ -0,0 +1,17 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/StaticsTest/StaticsTestVME.tst
load, // loads all the VM files from the current directory.
output-file StaticsTest.out,
compare-to StaticsTest.cmp,
output-list RAM[0]%D1.6.1 RAM[261]%D1.6.1 RAM[262]%D1.6.1;
set sp 261,
repeat 36 {
vmstep;
}
output;

View File

@@ -0,0 +1,20 @@
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/08/FunctionCalls/StaticsTest/Sys.vm
// Tests that different functions, stored in two different
// class files, manipulate the static segment correctly.
function Sys.init 0
push constant 6
push constant 8
call Class1.set 2
pop temp 0 // Dumps the return value
push constant 23
push constant 15
call Class2.set 2
pop temp 0 // Dumps the return value
call Class1.get 0
call Class2.get 0
label WHILE
goto WHILE

View File

@@ -1,4 +1,3 @@
#include "parser.hpp"
#include <functional>
#include <string>
@@ -30,6 +29,7 @@ private:
public:
code_writer(lots_of_files name);
void write_assembly();
void write_push(std::string segment, int index, bool debug);
void write_push(std::string segment, bool debug);
void write_pop(std::string segment, int index, bool debug);
@@ -40,17 +40,6 @@ public:
void write_neg_not(std::string op, bool debug);
void write_eq_lt_gt(std::string op, bool debug);
/*
void write_add(bool debug);
void write_sub(bool debug);
void write_neg(bool debug);
void write_and(bool debug);
void write_or(bool debug);
void write_not(bool debug);
void write_eq(bool debug);
void write_lt(bool debug);
void write_gt(bool debug);
*/
void write_init(bool debug);
void write_label(std::string label, int index, bool debug);

View File

@@ -359,167 +359,6 @@ void code_writer::write_eq_lt_gt(std::string op, bool debug)
this->ins_no += 6;
}
/*
void code_writer::write_add(bool debug)
{
if (debug)
{
outfile << "// add" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=D+M" << std::endl;
this->ins_no += 6;
}
void code_writer::write_sub(bool debug)
{
if (debug)
{
outfile << "// sub" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=M-D" << std::endl;
this->ins_no += 6;
}
void code_writer::write_neg(bool debug)
{
if (debug)
{
outfile << "// neg" << std::endl;
}
outfile << "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=-M" << std::endl;
this->ins_no += 3;
}
void code_writer::write_and(bool debug)
{
if (debug)
{
outfile << "// and" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=D&M" << std::endl;
this->ins_no += 6;
}
void code_writer::write_or(bool debug)
{
if (debug)
{
outfile << "// or" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=D|M" << std::endl;
this->ins_no += 6;
}
void code_writer::write_not(bool debug)
{
if (debug)
{
outfile << "// not" << std::endl;
}
outfile << "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=!M" << std::endl;
this->ins_no += 3;
}
void code_writer::write_eq(bool debug)
{
if (debug)
{
outfile << "// eq" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "A=A-1" << std::endl
<< "D=M-D" << std::endl;
this->ins_no += 5;
outfile << "@" << this->ins_no + 6 << std::endl
<< "D;JEQ" << std::endl
<< "D=0" << std::endl;
this->ins_no += 3;
outfile << "@" << this->ins_no + 4 << std::endl
<< "0;JMP" << std::endl
<< "D=-1" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=D" << std::endl;
this->ins_no += 6;
}
void code_writer::write_lt(bool debug)
{
if (debug)
{
outfile << "// lt" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "A=A-1" << std::endl
<< "D=M-D" << std::endl;
this->ins_no += 5;
outfile << "@" << this->ins_no + 6 << std::endl
<< "D;JLT" << std::endl
<< "D=0" << std::endl;
this->ins_no += 3;
outfile << "@" << this->ins_no + 4 << std::endl
<< "0;JMP" << std::endl
<< "D=-1" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=D" << std::endl;
this->ins_no += 6;
}
void code_writer::write_gt(bool debug)
{
if (debug)
{
outfile << "// gt" << std::endl;
}
outfile << "@SP" << std::endl
<< "AM=M-1" << std::endl
<< "D=M" << std::endl
<< "A=A-1" << std::endl
<< "D=M-D" << std::endl;
this->ins_no += 5;
outfile << "@" << this->ins_no + 6 << std::endl
<< "D;JGT" << std::endl
<< "D=0" << std::endl;
this->ins_no += 3;
outfile << "@" << this->ins_no + 4 << std::endl
<< "0;JMP" << std::endl
<< "D=-1" << std::endl
<< "@SP" << std::endl
<< "A=M-1" << std::endl
<< "M=D" << std::endl;
this->ins_no += 6;
}
*/
void code_writer::write_init(bool debug)
{
if (debug)