Skip to content

Instantly share code, notes, and snippets.

@lambdaofgod
Last active August 29, 2015 14:05
Show Gist options
  • Save lambdaofgod/20ff775766a50e87e21c to your computer and use it in GitHub Desktop.
Save lambdaofgod/20ff775766a50e87e21c to your computer and use it in GitHub Desktop.
// Kuba 'lambdaofgod' Bartczuk
// stack implemented in assembly language from Elements of Computing Systems (Nisan & Schocken)
// http://www.nand2tetris.org
// didnt even read the whole book (actually a bummer)
@R0 // initialize 0th register to 0
M=0
(STACK)
// D = (keyboard) input
@KBD
D=M
@STACK // if D = 0 do nothing
D;JEQ
@80
D=D-A
@POP
D;JEQ
//if D = 80 (D = 'p') pop else push
// push nonzero input
(PUSH)
@0
M=M+1 // increment 0th register
@0
A=M // set address to content of 0th register
M=D // register[A] = D : set register[A] to keyboard input
@0
A=M
@STACK
0;JMP
//pop stack
(POP)
@0 // if register[0] = 0 it means the stack is empty - can't pop it
D=M
M;JEQ
@0 // set address
A=M
M=0
@0 // modify stack size accordingly
M=M-1
@STACK
0;JM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment