update
[rrq/rrqforth.git] / memory.asm
1 ;;; This file defines "memory access words"
2
3         WORD p_cfa2flags_get,'CFA>FLAGS@',fasm
4         ;; ( cfa -- flags )
5         pop rax
6         cfa2tfa rax
7         push qword[rax+16]
8         next
9
10         WORD p_tfa2cfa,'TFA>CFA',fasm
11         ;; ( tfa -- cfa )
12         ;; Advance a word tfa pointer to the cfa field
13         mov rax,qword[rsp]
14         tfa2cfa rax
15         mov qword [rsp],rax
16         next
17         
18         WORD p_tfa2dfa,'TFA>DFA',fasm
19         ;; ( tfa -- dfa )
20         ;; Advance a word tfa pointer to the dfa field
21         mov rax,qword[rsp]
22         tfa2dfa rax
23         mov qword [rsp],rax
24         next
25
26         WORD p_tfa2flags_get,'TFA>FLAGS@',fasm
27         ;; ( cfa -- flags )
28         pop rax
29         push qword[rax+16]
30         next
31
32         WORD p_dfa2tfa,'DFA>TFA',fasm
33         ;; ( dfa -- tfa )
34         ;; Advance a word tfa pointer to the dfa field
35         mov rax,qword[rsp]
36         mov rax,qword [rax-24]  ; tfa
37         mov qword [rsp],rax
38         next
39
40         WORD p_get, '@',fasm
41         ;; ( addr -- v )
42         ;; Load value v from address addr
43         pop rax
44         push qword [rax]
45         next
46
47         WORD p_put, '!',fasm
48         ;; ( v addr -- )
49         ;; Store value v at address addr.
50         pop rax
51         pop rbx
52         mov qword [rax], rbx
53         next
54
55         WORD p_Cget, 'C@',fasm
56         ;; ( addr -- v )
57         ;; Load the (unsigned) byte v from address addr.
58         pop rax
59         mov bl,[rax]
60         push 0
61         mov [rsp],bl
62         next
63         
64         WORD p_Cput, 'C!',fasm
65         ;; ( v addr -- )
66         ;; Store byte value v at address addr.
67         pop rax
68         pop rbx
69         mov byte [rax], bl
70         next
71
72         WORD p_2get, '2@',fasm
73         ;; ( addr -- v2 v1 )
74         ;; Load the cell pair {v1,v2} from address addr.
75         pop rax
76         push qword [rax+8]      ; v2
77         push qword [rax]        ; v1
78         next
79
80         WORD p_2put, '2!',fasm
81         ;; ( v2 v1 addr -- )
82         ;; Store value pair {v1,v2} at address addr.
83         pop rax
84         pop rbx
85         mov qword [rax], rbx    ; v1
86         pop rbx
87         mov qword [rax+8], rbx  ; v2
88         next
89
90         WORD p_erase, 'ERASE',fasm
91         ;; ( addr u -- )
92         ;; Clear u bytes at address addr and up.
93         pop rax
94         pop rbx
95         xor rcx,rcx
96 p_erase_loop:
97         cmp rax,8
98         jl p_erase_last
99         mov qword [rbx],0       ; mov qword[rbx],rcx
100         add rbx,8
101         sub rax,8
102         jmp p_erase_loop
103 p_erase_more:
104         mov [rbx],byte 0        ; mov byte [rbx], cl
105         inc rbx
106         dec rax
107 p_erase_last:
108         jg p_erase_more
109         next
110
111         WORD p_1plus, '1+',fasm
112         ;; ( n1 -- n2 )
113         ;; Add one (1) to n1 resulting in n2.
114         inc qword [rsp]
115         next
116
117         WORD p_plus_put, '+!',fasm
118         ;; ( n addr -- )
119         ;; Add n to the value at addr.
120         pop rax
121         pop rbx
122         add [rax],rbx
123         next
124
125         WORD p_1minus, '1-',fasm
126         ;; ( n1 -- n2 )
127         ;; Subtract one (1) from n1 resulting in n2. 
128         dec qword [rsp]
129         next
130
131         WORD p_2mult, '2*',fasm
132         ;; ( x1 -- x2 )
133         ;; x2 is the result of shifting x1 one bit toward the
134         ;; most-significant bit, filling the vacated least-significant
135         ;; bit with zero.
136         shl qword [rsp],1
137         next
138
139         WORD p_2div, '2/',fasm
140         ;; ( x1 -- x2 )
141         ;; x2 is the result of shifting x1 one bit toward the
142         ;; least-significant bit, leaving the most-significant bit
143         ;; unchanged. (signed right shift)
144         sar qword [rsp],1
145         next
146