Showing posts with label hebrew university. Show all posts
Showing posts with label hebrew university. Show all posts

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.

Saturday, March 11, 2017

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

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