Shimon (or one of his underlings - maybe Yaron)
expression
if-goto IF_TRUE0
goto IF_FALSE0
label IF_TRUE0
if-statements
goto IF_END0
label IF_FALSE0
else-statements
label IF_END0
when all you really need is :
expression
if-goto IF_TRUE0
else-statements
goto IF_END0
label IF_TRUE0
if-statements
label IF_END0
So, why would they choose to implement the less optimal thing?
Showing posts with label shimon shocken. Show all posts
Showing posts with label shimon shocken. Show all posts
Friday, August 25, 2017
Saturday, April 22, 2017
Nand2Tetris : Terminal and Non-Terminal Rules
My opinion is that a separate function for each statement type is not necessary. Having worked in perl for so long, I'm sure there's an easy way to whack this out. But, being committed to py just for the sake of learning it (why?)
Saturday, April 15, 2017
Nand2Tetris Common Issues
../../tools/JackCompiler.bat Fraction/
Compiling "C:\Users....\09\Fraction\"
Could not find file or directory: C:\Users..
Problem - the '/' provided along with directory name. Yes, lame, I know :) Shimon's response : our purpose is didactic, not production :)
while( 1 ) {
}
use while( true ) ...
Aha - API claims drawCircle can handle radius of 181... Nice try there. I just found out you have to stay below 128 (127 is ok)
In Circle.jack (line 23): In subroutine PlotCircle: Expected (
Most likely reason - you used a "do" when you meant to use "let" :) Don't you miss Perl's errors/warnings :) ?
Gotcha - you HAVE to declare ALL variables before any "action".
var int xyz;
let xyz = 0;
var int pqr;
won't fly :)
Compiling "C:\Users....\09\Fraction\"
Could not find file or directory: C:\Users..
Problem - the '/' provided along with directory name. Yes, lame, I know :) Shimon's response : our purpose is didactic, not production :)
while( 1 ) {
}
use while( true ) ...
Aha - API claims drawCircle can handle radius of 181... Nice try there. I just found out you have to stay below 128 (127 is ok)
In Circle.jack (line 23): In subroutine PlotCircle: Expected (
Most likely reason - you used a "do" when you meant to use "let" :) Don't you miss Perl's errors/warnings :) ?
Gotcha - you HAVE to declare ALL variables before any "action".
var int xyz;
let xyz = 0;
var int pqr;
won't fly :)
Labels:
hack computer,
jack language,
nand2tetris,
noam nisan,
project 09,
shimon shocken
Sunday, April 9, 2017
Jack Peculiarities
must be used in assignments: let x = 0;
° The keyword do:
must be used for calling a method or a function outside an expression:
do reduce();
° The body of a statement must be within curly brackets, even if it
contains a single statement: if (a > a) {return 3} else {return -a};
° All subroutine must end with a return
° N0 operator priority:
-- The following value if unpredictable: 2 + 3 * 4
-- To enforce priority of operations, use parentheses: 2 + (3 * 4)
° The language is weakly typed
Labels:
jack language,
nand2tetris,
noam nisan,
shimon shocken
Jack Language Cheat Sheet
class, constructor, method, function Program components
int, boolean, char, void Primitive types
var, static, field Variable declarations
let, do, if, else, while, return Statements
true, false, null Constant values
this Object reference
int, boolean, char, void Primitive types
var, static, field Variable declarations
let, do, if, else, while, return Statements
true, false, null Constant values
this Object reference
Labels:
constants,
identifiers,
jack language,
keywords,
nand2tetris,
noam nisan,
shimon shocken,
symbols
Saturday, April 8, 2017
Shame on Yaron Ukrainitz
Sick
AM = M - 1
works. But
MA = M - 1
gives
In line XYZ, destination expected.
AM = M - 1
works. But
MA = M - 1
gives
In line XYZ, destination expected.
Nand2Tetris : if-goto (influences stack?)
Yes!!
You want to push something onto the stack to test it.
So, if you don't restore the stack-pointer with the if-goto, then you're leaky..
You want to push something onto the stack to test it.
So, if you don't restore the stack-pointer with the if-goto, then you're leaky..
Labels:
coursera,
if-goto,
nand2tetris,
noam nisan,
shimon shocken,
stuxnet
Sunday, April 2, 2017
Thanks Yaron Ukrainotz : Unexpected End of Line
In line 1 : Unexpected end of line
Okay, not hard to debug, but would be easier if you point out that you expect a constant after "@" in line so and so :)
Okay, not hard to debug, but would be easier if you point out that you expect a constant after "@" in line so and so :)
Sunday, March 26, 2017
Nand2Tetris 2 : Function Implementation Contract
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
Labels:
functions,
nand2tetris,
noam nisan,
segment,
shimon shocken,
stack,
virtual machine
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.
@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
// (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
// divide.asm
// Divides R0 by R1 and stores the dividend in R2 and remainder in R3
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
@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
// Divides R0 by R1 and stores the dividend in R2 and remainder in R3
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
@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
Friday, March 10, 2017
Nand2Tetris Hack Language Min Function
Make a more efficient mult:)
// finds min of R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// how : start off assuming R0 is smaller. Then, subtract R1 from R0 and, if
// result is > 0 => R1 is smaller, put R1 in R2.
@R0
D=M
@R2
M=D // until we find that R1 is smaller
@R1
D=M
@R0
M=M-D // we have R0 - R1
@R0 // if > 0, then we need to put R1 in R2
D=M
@R1SMALLER
D, JGT // if R1 is smaller than R0, this will be > 0 ..
@END
0, JMP // else, we're done
(R1SMALLER)
@R1
D=M
@R2
M=D
(END)
@END
0, JMP
// finds min of R0 and R1 and stores the result in R2.
// (R0, R1, R2 refer to RAM[0], RAM[1], and RAM[2], respectively.)
// how : start off assuming R0 is smaller. Then, subtract R1 from R0 and, if
// result is > 0 => R1 is smaller, put R1 in R2.
@R0
D=M
@R2
M=D // until we find that R1 is smaller
@R1
D=M
@R0
M=M-D // we have R0 - R1
@R0 // if > 0, then we need to put R1 in R2
D=M
@R1SMALLER
D, JGT // if R1 is smaller than R0, this will be > 0 ..
@END
0, JMP // else, we're done
(R1SMALLER)
@R1
D=M
@R2
M=D
(END)
@END
0, JMP
nand2tetris Hilf Help Shimon!
I load a program into the CPU emulator and nothing happens.
Might need to drag the lower right corners south-east a little to see the status line - you'll probably see a message there. Why the hell they don't have a View > Parser Log through the Menu just beats me. But hey, it's free :)
I do
@LOOP
JMP
and it doesn't like it. Ya, coz you HAVE to supply the core part of a C (compute) instruction always - only the jump and destination are optional. So, in this case, just do
0, JMP
Might need to drag the lower right corners south-east a little to see the status line - you'll probably see a message there. Why the hell they don't have a View > Parser Log through the Menu just beats me. But hey, it's free :)
I do
@LOOP
JMP
and it doesn't like it. Ya, coz you HAVE to supply the core part of a C (compute) instruction always - only the jump and destination are optional. So, in this case, just do
0, JMP
Sunday, June 5, 2016
Subscribe to:
Posts (Atom)