Compare commits

...
Sign in to create a new pull request.

7 commits

Author SHA1 Message Date
zanostro
fc0c680930 added stack 2025-11-15 12:17:44 +01:00
zanostro
63d5b3448f added stack 2025-11-15 12:16:56 +01:00
zanostro
47f3dcfc12 added cat.asm 2025-11-15 12:02:22 +01:00
zanostro
fb9dd4d7ec added poly.asm 2025-11-15 11:45:14 +01:00
zanostro
39aed69969 added arithr 2025-11-15 11:36:32 +01:00
zanostro
312c953151 added arith 2025-11-15 11:32:52 +01:00
zanostro
06aa39473a added rec and updated gitignore to ignore device files 2025-11-15 11:18:11 +01:00
8 changed files with 470 additions and 0 deletions

2
.gitignore vendored
View file

@ -19,5 +19,7 @@ node_modules/
__pycache__/
*.pyc
*.dev
autotester
sictools.jar

49
ass1/arith.asm Normal file
View file

@ -0,0 +1,49 @@
arith START 0
. seštevek: sum = x + y
LDA x
ADD y
STA sum
. razlika: diff = x - y
LDA x
SUB y
STA diff
. produkt: prod = x * y
LDA x
MUL y
STA prod
. količnik: quot = x / y
LDA x
DIV y .
STA quot
. ostanek: mod = x - y * (x / y)
STA qtemp
LDA y
MUL qtemp
STA prodtmp
LDA x
SUB prodtmp
STA mod
HALT J HALT
.data
x WORD 5
y WORD 2
sum RESW 1
diff RESW 1
prod RESW 1
quot RESW 1
mod RESW 1
qtemp RESW 1
prodtmp RESW 1
END arith

59
ass1/arithr.asm Normal file
View file

@ -0,0 +1,59 @@
.code
arithr START 0
LDA x ; A = x
LDB y ; B = y
RMO A,S ; S = x
RMO B,T ; T = y
. sum = x + y
RMO S,A ; A = x
ADDR T,A ; A = x + y
STA sum
. diff = x - y
RMO S,A ; A = x
SUBR T,A ; A = x - y
STA diff
. prod = x * y
RMO S,A ; A = x
RMO T,B ; B = y
MULR B,A ; A = x * y
STA prod
. quot = x / y
RMO S,A ; A = x
RMO T,B ; B = y
DIVR B,A ; A = x / y
STA quot
. mod = x % y = x - (x / y)*y
RMO S,A
RMO T,B
DIVR B,A ; A = q = x/y
RMO A,L ; L = q (shrani)
RMO T,A ; A = y
RMO L,B ; B = q
MULR B,A ; A = y*q = produkt
RMO S,B ; B = x
SUBR A,B ; B = x - (y*q)
STB mod
HALT J HALT
.data
x WORD 5
y WORD 2
sum RESW 1
diff RESW 1
prod RESW 1
quot RESW 1
mod RESW 1
END arithr

View file

@ -3,6 +3,7 @@
cat START 0
CLEAR X
read RD #0
STCH BUFF, X

57
ass1/poly.asm Normal file
View file

@ -0,0 +1,57 @@
poly START 0
. potence x^1 .. x^4
LDA x
STA x1
MUL x
STA x2
MUL x
STA x3
MUL x
STA x4
. x3 = 2 * x^3
LDA x3
LDB #2
MULR A,B
STB x3
. x2 = 3 * x^2
LDA x2
LDB #3
MULR A,B
STB x2
. x1 = 4 * x
LDA x1
LDB #4
MULR A,B
STB x1
. vsota vseh
LDA x0
ADD x1
ADD x2
ADD x3
ADD x4
STA result
HALT J HALT
END poly
.data
x WORD 2 ; vrednost x
x4 RESW 1
x3 RESW 1
x2 RESW 1
x1 RESW 1
x0 WORD 5 ; konstanta
result RESW 1
END poly

23
ass1/print.asm Normal file
View file

@ -0,0 +1,23 @@
.code
prog START 0
LDX #0
loop LDCH msg,X
WD #0xAA
TIX msglen
JLT loop
LDA #0x0D
WD #0xAA
LDA #0x0A
WD #0xAA
halt J halt
.data
msg BYTE C'SIC/XE'
msglen WORD 6
END prog

223
ass1/rec.asm Normal file
View file

@ -0,0 +1,223 @@
prog START 0
.-------------------------------------------
. MAIN LOOP
.
. Psevdo:
. sp = 0
. while true:
. n = readFA()
. if n == 0: halt
. acc = 1
. fact() ; rekurzivno: acc = n!
. printStdout(acc)
.-------------------------------------------
CLEAR A
STA sp
loop JSUB readFA
COMP #0
JEQ halt
STA n
LDA #1
STA acc
JSUB fact
LDA acc
JSUB printStdout
J loop
halt J halt
.-------------------------------------------
. readFA
.
. Psevdo:
. B = 0
. while true:
. ch = RD(FA)
. if ch == CR or ch == LF: break
. digit = ch - '0'
. B = B * 10 + digit
. return B
.-------------------------------------------
readFA CLEAR B
LDS #10
rd_loopFA RD #0xFA
COMP #0x0D . CR?
JEQ rd_doneCR_FA
COMP #0x0A . LF?
JEQ rd_doneFA
SUB #0x30
MULR S,B . B = B * 10
ADDR A,B . B = B + digit
J rd_loopFA
rd_doneCR_FA RD #0xFA . pogoltni LF po CR
rd_doneFA CLEAR A
RMO B,A
RSUB
.-------------------------------------------
. fact
.
. Psevdo (globalni n, acc, sklad L):
. fact():
. push(L)
. if n <= 1:
. pop(L); return
. acc = acc * n
. n = n - 1
. fact()
. pop(L); return
.-------------------------------------------
fact . push L
LDA sp
ADD #3
STA sp
LDX sp
STL stackL,X
LDA n
COMP #1
JGT fact_rec
. base case: n <= 1
LDX sp
LDL stackL,X
LDA sp
SUB #3
STA sp
RSUB
fact_rec . recursive case: acc *= n; n--; fact()
LDB acc
LDS n
MULR S,B
STB acc
LDA n
SUB #1
STA n
JSUB fact
. pop L in return to caller
LDX sp
LDL stackL,X
LDA sp
SUB #3
STA sp
RSUB
.-------------------------------------------
. printStdout
.
. Psevdo:
. if A == 0:
. print "0\n"
. return
. ps_val = A
. ps_len = 0
. while ps_val > 0:
. q = ps_val / 10
. r = ps_val % 10
. buf[ps_len] = '0' + r
. ps_len++
. ps_val = q
. for i = ps_len-1 .. 0:
. print buf[i]
. print "\r\n"
.-------------------------------------------
printStdout COMP #0
JEQ ps_zero
STA ps_val
LDA #0
STA ps_len
LDS #10
LDT #0x30 . '0'
ps_div LDA ps_val
COMP #0
JEQ ps_divdone
RMO A,B
DIVR S,B . kvocient v B
RMO B,X . X = kvocient
MULR S,B
SUBR B,A . A = ostanek
ADDR T,A . A = '0' + ostanek
STA psdigit
LDA ps_len
STA ps_idx
LDA #psbuf
ADD ps_idx
STA ps_ptr
LDA psdigit
STCH @ps_ptr
LDA ps_len
ADD #1
STA ps_len
RMO X,A
STA ps_val
J ps_div
ps_divdone LDA ps_len
SUB #1
STA ps_idx
ps_print LDA ps_idx
COMP #0
JLT ps_end
LDA #psbuf
ADD ps_idx
STA ps_ptr
LDCH @ps_ptr
WD #1
LDA ps_idx
SUB #1
STA ps_idx
J ps_print
ps_end LDA #0x0D . CR
WD #1
LDA #0x0A . LF
WD #1
RSUB
ps_zero LDA #0x30 . "0"
WD #1
LDA #0x0D
WD #1
LDA #0x0A
WD #1
RSUB
.data
. rekurzija faktoriala
sp WORD 0 . stack pointer
n WORD 0
acc WORD 0 . akumulator za faktorial
stackL RESB 60
. printStdout
ps_val WORD 0
ps_len WORD 0
ps_idx WORD 0
psdigit WORD 0
ps_ptr WORD 0
psbuf RESB 12
END prog

56
ass1/stack.asm Normal file
View file

@ -0,0 +1,56 @@
.code
stack START 0
LDA #9
STA @stackptr
JSUB stackpush
JSUB stackpop
LDA @stackptr
halt J halt
stackinit
STA stacktmp
LDA #STACK
STA stackptr
LDA stacktmp
RSUB
stackpush
STA stacktmp
LDA stackptr
ADD #3
STA stackptr
LDA stacktmp
RSUB
stackpop
STA stacktmp
LDA stackptr
SUB #3
STA stackptr
LDA stacktmp
RSUB
.data
stackptr WORD 0
stacktmp WORD 0
STACKSIZE EQU 50
STACK RESW STACKSIZE
END stack