nand2tetris - chapter 8 - part 1

.. implementing label, goto and if-goto. I will probably put the whole interpreter code I have later..

This is the code I have added:
private static String translateLabel(String vmLine, String fileName, String currenctFunctionName) {
    if (currenctFunctionName == null) {
        currenctFunctionName = "NULL";
    }
    vmLine = vmLine.substring(vmLine.indexOf(' ') + 1, vmLine.length());
    final String hackLabel = fileName.concat(".").concat(currenctFunctionName).concat(".").concat(vmLine);
    return "(" + hackLabel + ")\n";
}
 
private static String translateGoto(String vmLine, String fileName, String currenctFunctionName) {
    if (currenctFunctionName == null) {
        currenctFunctionName = "NULL";
    }
    vmLine = vmLine.substring(vmLine.indexOf(' ') + 1, vmLine.length());
 
    return
            "@" + fileName + "." + currenctFunctionName + "." + vmLine + "\n" +
                    "0;JMP\n";
}
 
private static String translateIfGoto(String vmLine, String fileName, String currenctFunctionName) {
    if (currenctFunctionName == null) {
        currenctFunctionName = "NULL";
    }
    vmLine = vmLine.substring(vmLine.indexOf(' ') + 1, vmLine.length());
    return
            "@R0\n" +
                    "M = M - 1\n" +
                    "A = M\n" +
                    "D = M\n" +
                    "@" + fileName + "." + currenctFunctionName + "." + vmLine + "\n" +
                    "D;JNE\n";
}

I will first test if I was successful at implementing label, so lets have this code to test:
function foo 0
label bar

And when translated, result will be:
(koray.foo.bar)

Looks good enough to me!!!

Let 's see if our goto works with doing our test with the code below:
function foo 0
label bar
push constant 9
goto bar

And the translated code will be as follows:
(koray.foo.bar)
// Push Constant
@9
D = A
@R0
A = M
M = D
@R0
D = M
D = D + 1
@R0
M = D
@0
D = A
// End Push Constant
@koray.foo.bar
0;JMP

So basically, when I run this code in the CPUEmulator, I am expecting to see 9 being pushed on the stack quite a few times. I will manually set R0 (Stack Pointer) to 20 before running this code on CPUEmulator..

Yes, exactly..

Now we will be testing if-goto.

Let 's first test the successful case:
function foo 0
push constant 9
push constant 9
eq
if-goto equalstrue
push constant 777
label equalstrue
push constant 888

I will again manually set the stack pointer to RAM[25] when the translated code is running on the CPUEmulator.

I am expecting to see the value 888 on RAM[25].

This is the translated code:
// Push Constant
@9
D = A
@R0
A = M
M = D
@R0
D = M
D = D + 1
@R0
M = D
@0
D = A
// End Push Constant
@9
D = A
@R0
A = M
M = D
@R0
D = M
D = D + 1
@R0
M = D
@0
D = A
// End Push Constant
@R0
M = M - 1
@R0
A = M
D = M
@R0
M = M - 1
@R0
A = M
D = M - D
@R0
A = M
M = D
@R0
D = M
D = D + 1
@R0
M = D
@0
D = A
// End sub
// equals 
@R0
M = M - 1
@R0
A = M
D = M
@equalstrue.1
D;JEQ
@equalsfalse.1
D;JNE
(equalstrue.1)
@1
A = - A
D = A
@0
A = M
M = D
@equalsend.1
0;JMP
(equalsfalse.1)
@0
D = A
@0
A = M
M = D
@equalsend.1
0;JMP
(equalsend.1)
@R0
M = M + 1
// end equals
@R0
M = M - 1
A = M
D = M
@koray.foo.equalstrue
D;JNE
// Push Constant
@777
D = A
@R0
A = M
M = D
@R0
D = M
D = D + 1
@R0
M = D
@0
D = A
// End Push Constant
(koray.foo.equalstrue)
// Push Constant
@888
D = A
@R0
A = M
M = D
@R0
D = M
D = D + 1
@R0
M = D
@0
D = A
// End Push Constant

And the result is as follows:

Which is expected. Now, lets modify the code just a little, and expect to see both the values 777 and 888 in the stack.
function foo 0
push constant 10
push constant 9
eq
if-goto equalstrue
push constant 777
label equalstrue
push constant 888

The translated code will be very similar to what you see above, lets just see the result:

All seems fine, I hope this was helpful!!!