Files
nand2tetris/projects/12/MathTest/Math.vm
T
QkoSad 7894b48931 .
2025-07-16 13:00:37 +03:00

389 lines
4.5 KiB
Plaintext
Executable File

function Math.init 1
push constant 16
call Array.new 1
pop static 0
push static 0
push constant 0
push constant 1
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
label L0
push local 0
push constant 15
lt
not
if-goto L1
push local 0
push constant 1
add
pop local 0
push static 0
push local 0
push static 0
push local 0
push constant 1
sub
add
pop pointer 1
push that 0
push static 0
push local 0
push constant 1
sub
add
pop pointer 1
push that 0
add
pop temp 1
add
pop pointer 1
push temp 1
pop that 0
goto L0
label L1
push constant 0
return
function Math.abs 0
push argument 0
push constant 0
gt
not
if-goto L2
push argument 0
return
goto L3
label L2
label L3
push argument 0
neg
return
function Math.multiply 5
push argument 0
push constant 0
lt
push argument 1
push constant 0
gt
and
push argument 0
push constant 0
gt
push argument 1
push constant 0
lt
and
or
pop local 4
push argument 0
call Math.abs 1
pop argument 0
push argument 1
call Math.abs 1
pop argument 1
push argument 0
push argument 1
lt
not
if-goto L4
push argument 0
pop local 1
push argument 1
pop argument 0
push local 1
pop argument 1
goto L5
label L4
label L5
push argument 1
push constant 1
eq
not
if-goto L6
push local 4
not
if-goto L7
push argument 0
neg
return
goto L8
label L7
label L8
push argument 0
return
goto L9
label L6
label L9
label L10
push local 2
push constant 1
sub
push argument 1
push constant 1
sub
lt
not
if-goto L11
push static 0
push local 3
add
pop pointer 1
push that 0
push argument 1
and
push constant 0
gt
not
if-goto L12
push local 0
push argument 0
add
pop local 0
push local 2
push static 0
push local 3
add
pop pointer 1
push that 0
add
pop local 2
goto L13
label L12
label L13
push argument 0
push argument 0
add
pop argument 0
push local 3
push constant 1
add
pop local 3
goto L10
label L11
push local 4
not
if-goto L14
push local 0
neg
pop local 0
goto L15
label L14
label L15
push local 0
return
function Math.divide 2
push static 2
push constant 0
eq
not
if-goto L16
push constant 0
pop static 1
push argument 0
push constant 0
lt
push argument 1
push constant 0
gt
and
push argument 0
push constant 0
gt
push argument 1
push constant 0
lt
and
or
pop local 1
push argument 0
call Math.abs 1
pop argument 0
push argument 1
call Math.abs 1
pop argument 1
push argument 1
push constant 0
eq
not
if-goto L17
push constant 3
call Sys.error 1
pop temp 0
goto L18
label L17
label L18
push constant 1
pop static 2
goto L19
label L16
label L19
push argument 1
push argument 0
gt
push argument 1
push constant 0
lt
or
not
if-goto L20
push constant 0
return
goto L21
label L20
label L21
push argument 0
push argument 1
push argument 1
add
call Math.divide 2
pop local 0
push constant 0
pop static 2
push local 0
call Math.abs 1
pop local 0
push local 0
push constant 1
and
push constant 1
eq
not
if-goto L22
push static 1
push argument 1
add
push argument 1
add
pop static 1
goto L23
label L22
label L23
push argument 0
push static 1
sub
push argument 1
lt
not
if-goto L24
push local 1
not
if-goto L25
push local 0
push local 0
add
neg
return
goto L26
label L25
label L26
push local 0
push local 0
add
return
goto L27
label L24
push local 1
not
if-goto L28
push local 0
push local 0
add
push constant 1
add
neg
return
goto L29
label L28
label L29
push local 0
push local 0
add
push constant 1
add
return
label L27
function Math.sqrt 4
push argument 0
push constant 0
lt
not
if-goto L30
push constant 4
call Sys.error 1
pop temp 0
goto L31
label L30
label L31
push constant 7
pop local 0
label L32
push local 0
push constant 1
neg
gt
not
if-goto L33
push local 3
push static 0
push local 0
add
pop pointer 1
push that 0
add
pop local 2
push local 2
push local 2
call Math.multiply 2
pop local 1
push local 1
push argument 0
gt
not
push local 1
push constant 0
gt
and
not
if-goto L34
push local 2
pop local 3
goto L35
label L34
label L35
push local 0
push constant 1
sub
pop local 0
goto L32
label L33
push local 3
return
function Math.max 0
push argument 0
push argument 1
gt
not
if-goto L36
push argument 0
return
goto L37
label L36
label L37
push argument 1
return
function Math.min 0
push argument 0
push argument 1
lt
not
if-goto L38
push argument 0
return
goto L39
label L38
label L39
push argument 1
return