Add support for Nix Flake

This commit is contained in:
2023-12-14 12:30:12 +01:00
parent 2737e0a34e
commit 54505d4d4d
10 changed files with 178 additions and 10 deletions

20
examples/example1.S Normal file
View File

@@ -0,0 +1,20 @@
; Evaluate 2*3+5
main: push 2
push 3
call &prd
clr 2
push 5
call &sum
clr 2
halt
sum: lda 0
lda 1
add
ret
prd: lda 0
lda 1
mul
ret

9
examples/example2.S Normal file
View File

@@ -0,0 +1,9 @@
; I/O example - simple echo
read: in
dup
out
push 0x0A
sub
jne &read ; loop until the input != new line (0x0A)
halt

30
examples/example3.S Normal file
View File

@@ -0,0 +1,30 @@
; Evaluate 2^10 - loop variant
push 2
push 10
call &pow
clr 2
halt
pow: lda 1 ; base
lda 0 ; exp
push 1 ; acc
; | Stack:
.loop: ldl 1 ; if exp == 0 | exp
je &.done ; then return | exp
pop ; |
; |
ldl 2 ; Evaluate | acc
ldl 0 ; next power | acc base
mul ; | acc*base
stl 2 ; |
; |
ldl 1 ; Decrement exp | exp
push 1 ; | exp 1
sub ; | exp-1
stl 1 ; |
jmp &.loop ; |
.done: ldl 2 ; | ... acc
ret ; | acc

28
examples/example4.S Normal file
View File

@@ -0,0 +1,28 @@
; Evaluate 2^10 - recursive variant
push 2 ; base
push 10 ; exp
call &pow
clr 2
halt
pow: lda 1 ; base
lda 0 ; exp
ldl 1 ; push exp to top
je &.edge ; the edge case: if exp == 0 then return 1
pop ; pop exp
; | Stack:
ldl 0 ; | base
ldl 1 ; | base exp
push 1 ; | base exp 1
sub ; | base exp-1
call &pow ; | base exp-1 base^(exp-1)]
clr 1 ; | base base^(exp-1)
mul ; | base*base^(exp-1)
ret ; | base*base^(exp-1)
.edge: pop
push 1 ; return 1
ret

33
examples/example5.S Normal file
View File

@@ -0,0 +1,33 @@
; The N-th element of Fibbonaci sequence - recursive variant
push 6
call &fibb
clr 1
halt
fibb: lda 0 ; n | Stack:
ldl 0 ; n == 0 -> return 1 | n
je &.done0 ; | n
pop ; |
ldl 0 ; n == 1 -> return 1 | n
push 1 ; | n 1
sub ; | n-1
je &.done1 ; | n-1
dup ; Evaluate fibb | n-1 n-1
push 1 ; | n-1 n-1 1
sub ; | n-1 n-2
call &fibb ; | n-1 n-2 f(n-2)
clr 1 ; | n-1 f(n-2)
over ; | n-1 f(n-2) n-1
call &fibb ; | n-1 f(n-2) n-1 f(n-1)
clr 1 ; | n-1 f(n-2) f(n-1)
add ; | n-1 f(n-2)+f(n-1)
ret
.done1: pop
push 1
ret
.done0: pop
push 1
ret