Sunday, March 26, 2017

Nand2Tetris 2 : Function Implementation Contract

https://www.coursera.org/learn/nand2tetris2/lecture/R5080/unit-2-3-functions-abstraction
For each function call during run-time, the implementation has to ...
• Pass parameters from the calling function to the called function;
• Determine the return address within the caller's code;
• Save the caller's return address, stack and memory segments;
• Jump to execute the called function;

For each function return during run-time. the implementation has to...
• Return to the caller the value computed by the called function;
• Recycle the memory resources used by the called function;
• Reinstate the caller's stack and memory segments;
• Jump to the return address in the caller's code.

courtesy of http://www.onlineocr.net/. Note that bullet one above for the function (callee) means you HAVE to push a value onto the stack before returning..

VM Implementation of Call and Return



Saturday, March 25, 2017

Nand2Tetris 2 : Project 07 Cheat Sheet

If you're like me wondering if documentation for project 7 could be improved - the answer is a resounding yes. Do C_ARITHMETIC commands include the logical ones? YES!! Book says so clearly on Pg 127. But, you can watch the videos twice and still be left with this q.

How about implementing the gt, lt, eq? You have to use jumps, but how do you do that - you're putting out code with a tonne of @DONE's.

And remember (?), the VM represents true as -1 or 0xFFFF and not 1 :) How should you know and what significance does this have? I don't know..

Tip : if using python, you should use raw strings (r prefix) for the match pattern so you can pass special tokens to the regex engine using backslashes that don't get interpreted by the parser itself. (Is it poss for me to confuse you more:) ?)

Saturday, March 18, 2017

Shimon's More Efficient Push - Nice

In the video, where he does push constant 17, he shows

@17
D=A
@SP
A=M
M=D
@SP
M=M+1

Notice that needs 3 A instructions. But, if you pay attention, during the demo, with BasicTest.vm, for  push constant 10, his implementation is :

@10
D=A
@SP
AM=M+1
A=A-1
M=D

Saved an instruction! Nice. Ha - did you think Stuxnet was easy to build my man? Truly, since he scrolls down in his ASM file, he's given you the implementation of "pop argument 2" and so it should be okay to put that down here - but that would be mean :)

Not that they're hard to implement, but he's given you add and sub as well :) Both cute, and using 5 instructions each. Booleans are super easy. For the conditionals, you'll have to use the jumps to conditionally write the appropriate value to the stack.

Sunday, March 12, 2017

Nand2Tetris Hack Divide By 2 (Right Shift)

// Divides R0 by 2 and stores the dividend in R1 and remainder in R2
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)

// how : counter goes from 2 to 32768 ( doubling each time )
// start with 1 and dump result of the AND in R2
// start off setting R1 to 0 and after that, based on result of the AND of
// counter with R0, you either OR R1 with counter or do nothing

@R1
M = 0
D = 1
@R0
D = M & D
@R2
M = D // remainder captured
@lagcount
M = 1
@2
D = A
@counter
M = D

(LOOP)
@R0
D = M & D // we're expecting D (pre) to already have the counter value based on knowing our code
@NO_ACTION
D, JEQ
@lagcount // else, we need to OR R1 with the lagging counter
D = M
@R1
M = M | D
(NO_ACTION)
@16384 // can only load a 15 bit number :)
D = A
D = D + A
@counter
D = M - D
@END
D, JEQ
// else, we need to double counter and double lagcount
@lagcount
D = M
M = M + D
@counter
D = M
MD = M + D

@LOOP
0, JMP

(END)
@END
0, JMP

Saturday, March 11, 2017

Shimon : Once Again I Have to Remind You..


that, if you don't understand this code, you have to stop and convince yourself that you understand it.

Make sure that you understand this intricate business of indirect addressing using the A register.

Nand2Tetris Hack Assembly Language Divide

Copied!
// divide.asm
// Divides R0 by R1; quotient in R2, remainder in R3.
// R0, R1, R2 and R3 refer to RAM[0] through RAM[3].

    @R2
    M = 0
    @R3
    M = 0
    @R0
    D = M
    @END
    D, JEQ
    @store
    M = D        // store to restore

(LOOP)
    @R1
    D = D - M
    @REMAINDER
    D, JLT
    @R2
    M = M + 1
    @EVENLY
    D, JEQ
    @LOOP
    0, JMP

(REMAINDER)
    @R1
    D = D + M
    @R3
    M = D

(EVENLY)
    @store
    D = M
    @R0
    M = D

(END)
    @END
    0, JMP

Nand2Tetris Screen Addressing

http://www.nand2tetris.org/lectures/PDF/lecture%2005%20computer%20architecture.pdf


So, you have 256 rows each 1 pixel thick.
You have 32 columns each 16 bits thick, or 512 columns, each 1 pixel thick.

So, to set pixel at x,y where x,y within (0-511, 0-255) :

Set (black) the y % 16 bit of the word addressed by Screen[ x*32 + y/16]

where Screen[X] implies memory address 16384 + X.