mirror of
https://github.com/usatiuk/nand2tetris.git
synced 2025-10-29 00:27:49 +01:00
395 lines
5.0 KiB
Plaintext
395 lines
5.0 KiB
Plaintext
function Math.init 0
|
|
push constant 16
|
|
call Array.new 1
|
|
pop static 0
|
|
push constant 0
|
|
push static 0
|
|
add
|
|
push constant 1
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 1
|
|
push static 0
|
|
add
|
|
push constant 2
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 2
|
|
push static 0
|
|
add
|
|
push constant 4
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 3
|
|
push static 0
|
|
add
|
|
push constant 8
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 4
|
|
push static 0
|
|
add
|
|
push constant 16
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 5
|
|
push static 0
|
|
add
|
|
push constant 32
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 6
|
|
push static 0
|
|
add
|
|
push constant 64
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 7
|
|
push static 0
|
|
add
|
|
push constant 128
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 8
|
|
push static 0
|
|
add
|
|
push constant 256
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 9
|
|
push static 0
|
|
add
|
|
push constant 512
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 10
|
|
push static 0
|
|
add
|
|
push constant 1024
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 11
|
|
push static 0
|
|
add
|
|
push constant 2048
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 12
|
|
push static 0
|
|
add
|
|
push constant 4096
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 13
|
|
push static 0
|
|
add
|
|
push constant 8192
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 14
|
|
push static 0
|
|
add
|
|
push constant 16384
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 15
|
|
push static 0
|
|
add
|
|
push constant 16384
|
|
push constant 16384
|
|
add
|
|
pop temp 0
|
|
pop pointer 1
|
|
push temp 0
|
|
pop that 0
|
|
push constant 0
|
|
return
|
|
function Math.bit 0
|
|
push argument 0
|
|
push argument 1
|
|
push static 0
|
|
add
|
|
pop pointer 1
|
|
push that 0
|
|
and
|
|
push constant 0
|
|
eq
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push constant 0
|
|
return
|
|
goto IF_END0
|
|
label IF_FALSE0
|
|
push constant 0
|
|
not
|
|
return
|
|
label IF_END0
|
|
function Math.abs 0
|
|
push argument 0
|
|
push constant 0
|
|
lt
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push argument 0
|
|
neg
|
|
return
|
|
goto IF_END0
|
|
label IF_FALSE0
|
|
push argument 0
|
|
return
|
|
label IF_END0
|
|
function Math.multiply 3
|
|
push constant 0
|
|
pop local 0
|
|
push argument 0
|
|
pop local 1
|
|
push constant 0
|
|
pop local 2
|
|
label WHILE_EXP0
|
|
push local 2
|
|
push constant 16
|
|
lt
|
|
not
|
|
if-goto WHILE_END0
|
|
push argument 1
|
|
push local 2
|
|
call Math.bit 2
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push local 0
|
|
push local 1
|
|
add
|
|
pop local 0
|
|
label IF_FALSE0
|
|
push local 1
|
|
push local 1
|
|
add
|
|
pop local 1
|
|
push local 2
|
|
push constant 1
|
|
add
|
|
pop local 2
|
|
goto WHILE_EXP0
|
|
label WHILE_END0
|
|
push local 0
|
|
return
|
|
function Math.divide 2
|
|
push argument 0
|
|
push constant 0
|
|
lt
|
|
push argument 1
|
|
and
|
|
push constant 0
|
|
lt
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push argument 0
|
|
call Math.abs 1
|
|
push argument 1
|
|
call Math.abs 1
|
|
call Math.divide 2
|
|
return
|
|
label IF_FALSE0
|
|
push argument 0
|
|
push constant 0
|
|
lt
|
|
push argument 1
|
|
or
|
|
push constant 0
|
|
lt
|
|
if-goto IF_TRUE1
|
|
goto IF_FALSE1
|
|
label IF_TRUE1
|
|
push argument 0
|
|
call Math.abs 1
|
|
push argument 1
|
|
call Math.abs 1
|
|
call Math.divide 2
|
|
neg
|
|
return
|
|
label IF_FALSE1
|
|
push argument 1
|
|
push argument 0
|
|
gt
|
|
if-goto IF_TRUE2
|
|
goto IF_FALSE2
|
|
label IF_TRUE2
|
|
push constant 0
|
|
return
|
|
label IF_FALSE2
|
|
push argument 1
|
|
push constant 0
|
|
lt
|
|
if-goto IF_TRUE3
|
|
goto IF_FALSE3
|
|
label IF_TRUE3
|
|
push constant 0
|
|
return
|
|
label IF_FALSE3
|
|
push argument 0
|
|
push constant 2
|
|
push argument 1
|
|
call Math.multiply 2
|
|
call Math.divide 2
|
|
pop local 0
|
|
push argument 0
|
|
push constant 2
|
|
push local 0
|
|
call Math.multiply 2
|
|
push argument 1
|
|
call Math.multiply 2
|
|
sub
|
|
push argument 1
|
|
lt
|
|
if-goto IF_TRUE4
|
|
goto IF_FALSE4
|
|
label IF_TRUE4
|
|
push constant 2
|
|
push local 0
|
|
call Math.multiply 2
|
|
return
|
|
goto IF_END4
|
|
label IF_FALSE4
|
|
push constant 2
|
|
push local 0
|
|
call Math.multiply 2
|
|
push constant 1
|
|
add
|
|
return
|
|
label IF_END4
|
|
function Math.sqrt 2
|
|
push constant 0
|
|
pop local 0
|
|
push constant 7
|
|
pop local 1
|
|
label WHILE_EXP0
|
|
push local 1
|
|
push constant 1
|
|
neg
|
|
gt
|
|
not
|
|
if-goto WHILE_END0
|
|
push local 0
|
|
push constant 2
|
|
push local 1
|
|
call Math.pow 2
|
|
add
|
|
push constant 2
|
|
call Math.pow 2
|
|
push argument 0
|
|
gt
|
|
not
|
|
push local 0
|
|
push constant 2
|
|
push local 1
|
|
call Math.pow 2
|
|
add
|
|
push constant 2
|
|
call Math.pow 2
|
|
push constant 0
|
|
gt
|
|
and
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push local 0
|
|
push constant 2
|
|
push local 1
|
|
call Math.pow 2
|
|
add
|
|
pop local 0
|
|
label IF_FALSE0
|
|
push local 1
|
|
push constant 1
|
|
sub
|
|
pop local 1
|
|
goto WHILE_EXP0
|
|
label WHILE_END0
|
|
push local 0
|
|
return
|
|
function Math.pow 2
|
|
push constant 0
|
|
pop local 1
|
|
push constant 1
|
|
pop local 0
|
|
label WHILE_EXP0
|
|
push local 1
|
|
push argument 1
|
|
lt
|
|
not
|
|
if-goto WHILE_END0
|
|
push local 0
|
|
push argument 0
|
|
call Math.multiply 2
|
|
pop local 0
|
|
push local 1
|
|
push constant 1
|
|
add
|
|
pop local 1
|
|
goto WHILE_EXP0
|
|
label WHILE_END0
|
|
push local 0
|
|
return
|
|
function Math.max 0
|
|
push argument 0
|
|
push argument 1
|
|
gt
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push argument 0
|
|
return
|
|
goto IF_END0
|
|
label IF_FALSE0
|
|
push argument 1
|
|
return
|
|
label IF_END0
|
|
function Math.min 0
|
|
push argument 0
|
|
push argument 1
|
|
gt
|
|
if-goto IF_TRUE0
|
|
goto IF_FALSE0
|
|
label IF_TRUE0
|
|
push argument 1
|
|
return
|
|
goto IF_END0
|
|
label IF_FALSE0
|
|
push argument 0
|
|
return
|
|
label IF_END0
|