summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkitty <nepeta@canaglie.net>2026-03-20 01:03:51 +1100
committerkitty <nepeta@canaglie.net>2026-03-20 01:03:51 +1100
commita654a2e0418b0f143cb4d6c55776c844e7254e22 (patch)
tree6116fe042b689df325c36c475dba8c74eb904d44
parentae2bb07e11ce6582cc9c7d45d1b8137c355e9db7 (diff)
stack and math words
-rw-r--r--readme.md38
-rw-r--r--sanctuary.s83
2 files changed, 121 insertions, 0 deletions
diff --git a/readme.md b/readme.md
index 7e93f2f..c168abe 100644
--- a/readme.md
+++ b/readme.md
@@ -17,6 +17,9 @@ sanctuary is a 64-bit subroutine threaded forth for amd64 linux systems.
the following is a list of words available in this forth.
+### `! ( u a -- )`
+store the 64 bit value u into the memory address a.
+
### `#tib ( -- a )`
variable containing the amount of characters in the input buffer.
@@ -35,9 +38,18 @@ remainder is in n3, result is in n4
### `+ ( u1 u2 -- u )`
add u2 to u1.
+### `+! ( a -- )`
+add one to the value at memory address a.
+
+### `, ( u -- )`
+write a 64 bit value to user memory and increment the user memory pointer.
+
### `- ( u1 u2 -- u )`
subtract u2 from u1.
+### `-! ( a -- )`
+subtract one from the value at memory address a.
+
### `-rot ( u1 u2 u3 -- u3 u1 u2 )`
rotate the three topmost values on the stack so that the topmost value
is moved to the third highest.
@@ -57,6 +69,9 @@ start compilation of the word 'name'.
### `; ( -- ) IMMEDIATE`
end compilation of the currently compiling word.
+### `@ ( a -- u )`
+fetch the 64 bit value at memory address a.
+
### `>body ( xt -- a )`
yield the code field of xt.
@@ -67,6 +82,12 @@ in the input buffer.
### `>r ( u -- ) ( R: -- u )`
move a value from the working stack to the return stack.
+### `1+ ( u -- u')`
+add one to u.
+
+### `1- ( u -- u')`
+subtract one from u.
+
### `2drop ( u1 u2 -- )`
remove the two topmost values from the stack.
@@ -82,6 +103,23 @@ yields current program break.
### `bye ( -- )`
exits the forth system.
+### `c, ( c -- )`
+write an 8 bit value to user memory and increment the user memory pointer.
+
+### `c! ( u a -- )`
+store the 8 bit value u into the memory address a.
+
+### `c@ ( a -- c )`
+fetch the 8 bit value at memory address a.
+
+### `cmove ( a1 a2 u -- )`
+copy u bytes of memory from a1 to a2.
+bytes are copied in low memory to high memory order.
+
+### `cmove> ( a1 a2 u -- )`
+copy u bytes of memory from a1 to a2.
+bytes are copied in high memory to low memory order.
+
### `dp ( -- a )`
a variable that contains the lowest free byte of memory in user memory.
diff --git a/sanctuary.s b/sanctuary.s
index 44cd4af..1b9fd0f 100644
--- a/sanctuary.s
+++ b/sanctuary.s
@@ -661,6 +661,81 @@ defcode "rdrop", rdrop, 0
ret
; }}}
+; memory access {{{
+defcode "@", fetch, 0
+ pspop r11
+ mov r12, qword [r11]
+ pspush r12
+ ret
+
+defcode "c@", cfetch, 0
+ pspop r11
+ xor r12, r12
+ mov r12b, byte [r11]
+ pspush r12
+ ret
+
+defcode "!", store, 0
+ pspop r11
+ pspop r12
+ mov qword [r11], r12
+ ret
+
+defcode "c!", cstore, 0
+ pspop r11
+ pspop r12
+ mov byte [r11], r12b
+ ret
+
+defcode "+!", plusstore, 0
+ pspop r11
+ pspop r12
+ add qword [r11], r12
+ ret
+
+defcode "-!", minusstore, 0
+ pspop r11
+ pspop r12
+ sub qword [r11], r12
+ ret
+
+defcode ",", comma, 0
+ pspop r11
+ mov r12, [here]
+ mov qword [r12], r11
+ add r12, 8
+ mov qword [here], r12
+ ret
+
+defcode "c,", c_comma, 0
+ pspop r11
+ mov r12, [here]
+ mov byte [r12], r11b
+ inc r12
+ mov qword [here], r12
+ ret
+
+defcode "cmove", _cmove, 0
+ pspop rcx
+ pspop rdi
+ pspop rsi
+
+ rep movsb
+ ret
+
+defcode "cmove>", cmove_to, 0
+ std
+ pspop rcx
+ pspop rdi
+ add rdi, rcx
+ pspop rsi
+ add rsi, rcx
+
+ rep movsb
+ cld
+ ret
+; }}}
+
; math + comparison {{{
; i believe some of these could be improved by direct accesses to [r15]
defcode "+", plus, 0
@@ -677,6 +752,14 @@ defcode "-", minus, 0
pspush r12
ret
+defcode "1+", oneplus, 0
+ inc qword [r15]
+ ret
+
+defcode "1-", oneminus, 0
+ dec qword [r15]
+ ret
+
defcode "*", _times, 0
pspop r11
pspop r12