draft EVALUATE-STREAM
[rrq/rrqforth.git] / memory.asm
1 ;;; This file defines "memory access words"
2
3         WORD p_get, '@',fasm
4         ;; ( addr -- v )
5         ;; Load value v from address addr
6         pop rax
7         push qword [rax]
8         next
9
10         WORD p_put, '!',fasm
11         ;; ( v addr -- )
12         ;; Store value v at address addr.
13         pop rax
14         pop rbx
15         mov qword [rax], rbx
16         next
17
18         WORD p_Cget, 'C@',fasm
19         ;; ( addr -- v )
20         ;; Load the (unsigned) byte v from address addr.
21         pop rax
22         mov bl,[rax]
23         push 0
24         mov [rsp],bl
25         next
26         
27         WORD p_Cput, 'C!',fasm
28         ;; ( v addr -- )
29         ;; Store byte value v at address addr.
30         pop rax
31         pop rbx
32         mov byte [rax], bl
33         next
34
35         WORD p_2get, '2@',fasm
36         ;; ( addr -- v2 v1 )
37         ;; Load the cell pair {v1,v2} from address addr.
38         pop rax
39         push qword [rax+8]      ; v2
40         push qword [rax]        ; v1
41         next
42
43         WORD p_2put, '2!',fasm
44         ;; ( v2 v1 addr -- )
45         ;; Store value pair {v1,v2} at address addr.
46         pop rax
47         pop rbx
48         mov qword [rax], rbx    ; v1
49         pop rbx
50         mov qword [rax+8], rbx  ; v2
51         next
52
53         WORD p_erase, 'ERASE',fasm
54         ;; ( addr u -- )
55         ;; Clear u bytes at address addr and up.
56         pop rax
57         pop rbx
58         xor rcx,rcx
59 p_erase_loop:
60         cmp rax,8
61         jl p_erase_last
62         mov qword [rbx],0       ; mov qword[rbx],rcx
63         add rbx,8
64         sub rax,8
65         jmp p_erase_loop
66 p_erase_more:
67         mov [rbx],byte 0        ; mov byte [rbx], cl
68         inc rbx
69         dec rax
70 p_erase_last:
71         jg p_erase_more
72         next
73
74         WORD p_1plus, '1+',fasm
75         ;; ( n1 -- n2 )
76         ;; Add one (1) to n1 resulting in n2.
77         inc qword [rsp]
78         next
79
80         WORD p_plus_put, '+!',fasm
81         ;; ( n addr -- )
82         ;; Add n to the value at addr.
83         pop rax
84         pop rbx
85         add [rax],rbx
86         next
87
88         WORD p_1minus, '1-',fasm
89         ;; ( n1 -- n2 )
90         ;; Subtract one (1) from n1 resulting in n2. 
91         dec qword [rsp]
92         next
93
94         WORD p_2mult, '2*',fasm
95         ;; ( x1 -- x2 )
96         ;; x2 is the result of shifting x1 one bit toward the
97         ;; most-significant bit, filling the vacated least-significant
98         ;; bit with zero.
99         shl qword [rsp],1
100         next
101
102         WORD p_2div, '2/',fasm
103         ;; ( x1 -- x2 )
104         ;; x2 is the result of shifting x1 one bit toward the
105         ;; least-significant bit, leaving the most-significant bit
106         ;; unchanged. (signed right shift)
107         sar qword [rsp],1
108         next
109