compiler working for some mathematical expressions, function calls and

simple conditionals
This commit is contained in:
2026-01-31 13:28:42 +00:00
parent e31deb594f
commit 52ef7872f0
10 changed files with 466 additions and 79 deletions
+139
View File
@@ -0,0 +1,139 @@
// GENERATED BY DSA-C COMPILER
// Generated at 2026-01-31 01:39:55
// Imports
include maths: "./lib/maths/core.dsa"
include print: "lib/io/print.dsa"
// Globals & Reserved Memory
// Entry Point
dw stack: 0x10000
db message: "Process Exited with code:"
_init:
ldw stack, bpr
mov bpr, spr
push zero
call main
call print::print_newline
lwi message, rg0
push rg0
call print::print
pop zero
call print::print_hex_word
pop zero
hlt
// Function return boilerplate
_ret:
mov bpr, spr
pop bpr
return
factorial:
push bpr
mov spr, bpr
ldw bpr, rg0, 8
lli 1, rg1
cmp rg0, rg1
lli 0, rg2
jgt _cmp_end_1
lli 1, rg2
_cmp_end_1:
cmp rg2, zero
jeq _else_3
_then_2:
lli 1, rg1
stw rg1, bpr, 8
jmp _ret
jmp _end_4
_else_3:
nop
_end_4:
push rg0
lli 1, rg1
sub rg0, rg1, rg2
push rg2
call factorial
pop rg1
pop rg0
push rg1
push rg0
call maths::multiply
pop rg2
pop zero
stw rg2, bpr, 8
jmp _ret
add_:
push bpr
mov spr, bpr
ldw bpr, rg0, 8
ldw bpr, rg1, 12
add rg0, rg1, rg2
stw rg2, bpr, 8
jmp _ret
greater:
push bpr
mov spr, bpr
ldw bpr, rg0, 8
ldw bpr, rg1, 12
add rg0, rg0, rg2
add rg1, rg1, rg3
cmp rg2, rg3
lli 0, rg4
jle _cmp_end_5
lli 1, rg4
_cmp_end_5:
cmp rg4, zero
jeq _else_7
_then_6:
stw rg0, bpr, 8
jmp _ret
jmp _end_8
_else_7:
add rg1, rg0, rg2
stw rg2, bpr, 8
jmp _ret
_end_8:
jmp _ret
main:
push bpr
mov spr, bpr
lli 5, rg0
push rg0
lli 5, rg1
push rg1
call add_
pop rg2
pop zero
push rg2
lli 5, rg0
push rg0
call greater
pop rg1
pop zero
push rg1
call print::print_num
pop rg0
lli 5, rg0
push rg0
call factorial
pop rg1
push rg1
call print::print_num
pop rg0
lli 0, rg0
stw rg0, bpr, 8
jmp _ret
+7 -45
View File
@@ -16,55 +16,17 @@ init:
dw string: "hello world"
start:
lwi 37, rg0
lwi 12, rg1
push rg0
push rg1
call maths::divmod
pop rg0 // result
pop rg1 // remainder
lwi 1, rg0
lwi 2, rg1
push rg1
push rg0
call print::print_hex_byte
call print::print_whitespace
push rg1
call maths::multiply
pop rg0
pop zero
call print::print_hex_byte
call print::print_newline
lwi string, rg0
//lwi 10, rg0
pusha 4
push rg0
call print::print
//call fib::fib_n
pop zero
call print::print_newline
popa 4
pusha 4
push rg0
call print::print
//call fib::fib_n
pop zero
call print::print_newline
popa 4
pusha 4
push rg0
call print::print
//call fib::fib_n
pop zero
call print::print_newline
popa 4
pusha 4
push rg0
call print::print
//call fib::fib_n
pop zero
call print::print_newline
popa 4
call print::print_num
pop zero
hlt