implement prefix calculator

This commit is contained in:
jakob 2026-02-13 12:49:40 +01:00
parent 09178c6d94
commit 76257daea8
3 changed files with 226 additions and 2 deletions

31
calculator/README.md Normal file
View file

@ -0,0 +1,31 @@
# Calculator
This program accepts an arithmetic expression in prefix notation
on standard input and prints the result on standard output.
Supported operations:
- Addition
- Subtraction
- Multiplication
- Division
All calculations are preformed on Integers in range $[-2^{23}, 2^{23} - 1]$. Negative numbers can be written as `(- 0 n)`, where `n` is the number
Here are some examples:
```{bash}
(* 21 2)
00000042
(/ 6 (* 2 (+ 2 1)))
00000001
(* (/ 6 2) (+ 2 1))
00000009
(- 33 (* 4 25))
-00000067
```
- Source code: calc.asm
- Frequency: any (faster is faster)
- Author: Jakob Jesenko, 2026

193
calculator/calc.asm Normal file
View file

@ -0,0 +1,193 @@
calc START 0
nextln LDA #stack
STA sp
CLEAR A
STA ldigit
readlp TD stdin
JEQ readlp
CLEAR A
RD stdin
COMP #0
JEQ halt
COMP white .whitespace (parse number?)
JEQ newexp
COMP opn . (
JEQ newexp
COMP cls . )
JEQ endexp
COMP plus . +
JEQ pplus
COMP minus . -
JEQ pminus
COMP times . *
JEQ ptimes
COMP slash . /
JEQ pslash
COMP lnfeed . \n
JEQ prtres
J pdigit
newexp CLEAR A
STA ldigit
J readlp
endexp CLEAR A
STA ldigit
LDA sp
SUB #3
STA sp
LDS @sp
SUB #3
STA sp
LDT @sp
SUB #3
STA sp
LDA @sp
COMP plus . +
JEQ xplus
COMP minus . -
JEQ xminus
COMP times . *
JEQ xtimes
COMP slash . /
JEQ xslash
pres STT @sp
LDA sp
ADD #3
STA sp
J readlp
pplus CLEAR A
STA ldigit
LDA plus
STA @sp
LDA sp
ADD #3
STA sp
J readlp
pminus CLEAR A
STA ldigit
LDA minus
STA @sp
LDA sp
ADD #3
STA sp
J readlp
ptimes CLEAR A
STA ldigit
LDA times
STA @sp
LDA sp
ADD #3
STA sp
J readlp
pslash CLEAR A
STA ldigit
LDA slash
STA @sp
LDA sp
ADD #3
STA sp
J readlp
pdigit SUB zero
RMO A,B
LDA ldigit
COMP #1
JEQ pndgit
pcdgit RMO B,A
STA @sp
LDA sp
ADD #3
STA sp
LDA #1
STA ldigit
J readlp
pndgit LDA sp
SUB #3
STA sp
LDA @sp
MUL #10
ADDR A,B
J pcdgit
xplus ADDR S,T
J pres
xminus SUBR S,T
J pres
xtimes MULR S,T
J pres
xslash DIVR S,T
J pres
prtres LDB #base
STB bp
LDA sp
SUB #3
STA sp
LDA @sp
AND negsgn
COMP negsgn
JEQ prtneg
prtnxt LDA @bp
COMP #0
JEQ prtext
LDA @sp
DIV @bp
ADD zero
WD stdout
SUB zero
LDB @bp
MULR B,A
LDB @sp
SUBR A,B
STB @sp
LDA bp
ADD #3
STA bp
J prtnxt
prtext
LDA lnfeed
WD stdout
J nextln
prtneg
LDA minus
WD stdout
LDA #0
SUB #1
LDS @sp
MULR S,A
STA @sp
J prtnxt
halt J halt
stdin BYTE 0x00
stdout BYTE 0x01
zero WORD 48
lnfeed WORD 10
white WORD 32
opn WORD 40
cls WORD 41
plus WORD 43
minus WORD 45
times WORD 42
slash WORD 47
ldigit WORD 0
negsgn WORD 0x800000
bp RESW 1
base WORD 10000000
WORD 1000000
WORD 100000
WORD 10000
WORD 1000
WORD 100
WORD 10
WORD 1
WORD 0
sp RESW 1
stack RESW 256

View file

@ -1,9 +1,10 @@
# PPM image viewer
Reads a ppm image from an input device (AF.dev by default)
Reads a ppm image from an input device (AF.dev by default)
and displays it on SicTools builtin graphical screen.
To resize and convert your image you can use ffmpeg:
```{sh}
ffmpeg -i image.png -vf scale="64x-1" AF.ppm && mv AF.ppm AF.dev
```
@ -13,4 +14,3 @@ ffmpeg -i image.png -vf scale="64x-1" AF.ppm && mv AF.ppm AF.dev
- dimensions can be adjusted by changing their values in code
- Frequency: any
- Author: Jakob Jesenko, 2026