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