dd7fc9dcbc4b70a896d523bde9c89b8cc95baec2
[rrq/jonasforth.git] / impl.asm
1 ;; vim: syntax=fasm
2
3 section '.text' code readable executable
4
5 macro printlen msg, len {
6   push rsi
7   add rsp, 8
8
9   mov rcx, msg
10   mov rdx, len
11   call os_print_string
12
13   sub rsp, 8
14   pop rsi
15 }
16
17 macro newline {
18   push $A
19   printlen rsp, 1
20 }
21
22 macro print msg {
23   printlen msg, msg#.len
24 }
25
26 struc string bytes {
27   . db bytes
28   .len = $ - .
29 }
30
31 ;; Find the given word in the dictionary of words. If no such word exists,
32 ;; return 0.
33 ;;
34 ;; Parameters:
35 ;;   * [find.search_length] = Length of the word in bytes.
36 ;;   * [find.search_buffer] = Pointer to the string containing the word.
37 ;;   * rsi = Pointer to the last entry in the dictionary.
38 ;;
39 ;; Results:
40 ;;   * rsi = Pointer to the found entry in the dictionary or 0.
41 ;;
42 ;; Clobbers rcx, rdx, rdi, rax.
43 find:
44   ;; RSI contains the entry we are currently looking at
45 .loop:
46   movzx rcx, byte [rsi + 8 + 1]    ; Length of word being looked at
47   cmp rcx, [.search_length]
48   jne .next    ; If the words don't have the same length, we have the wrong word
49
50   ;; Otherwise, we need to compare strings
51   lea rdx, [rsi + 8 + 1 + 1]    ; Location of character being compared in entry
52   mov rdi, [.search_buffer]     ; Location of character being compared in search buffer
53 .compare_char:
54   mov al, [rdx]
55   mov ah, [rdi]
56   cmp al, ah
57   jne .next                     ; They don't match; try again
58   inc rdx                       ; These characters match; look at the next ones
59   inc rdi
60   loop .compare_char
61
62   jmp .found                    ; They match! We are done.
63
64 .next:
65   mov rsi, [rsi]                ; Look at the previous entry
66   cmp rsi, 0
67   jnz .loop                    ; If there is no previous word, exit and return 0
68
69 .found:
70   ret
71
72 ;; Read a word from a buffer. Returns the buffer without the word, as well as
73 ;; the word that was read (including lengths).
74 ;;
75 ;; Inputs:
76 ;;   * rsi = Input buffer
77 ;;   * rcx = Length of buffer
78 ;;
79 ;; Outputs:
80 ;;   * rsi = Updated buffer
81 ;;   * rcx = Length of updated buffer
82 ;;   * rdi = Word buffer
83 ;;   * rdx = Length of word buffer
84 pop_word:
85 .skip_whitespace:
86   mov al, [rsi]
87   cmp al, ' '
88   je .got_whitespace
89   cmp al, $A
90   je .got_whitespace
91   jmp .alpha
92 .got_whitespace:
93   ;; The buffer starts with whitespace; discard the first character from the buffer.
94   inc rsi
95   dec rcx
96   jmp .skip_whitespace
97
98 .alpha:
99   ;; We got a character that wasn't whitespace. Now read the actual word.
100   mov rdi, rsi ; This is where the word starts
101   mov rdx, 1   ; Length of word
102
103 .read_alpha:
104   ;; Extract character from original buffer:
105   inc rsi
106   dec rcx
107
108   ;; When we hit whitespace, we are done with this word
109   mov al, [rsi]
110   cmp al, ' '
111   je .end
112   cmp al, $A
113   je .end
114
115   ;; It wasn't whitespace; add it to word buffer
116   inc rdx
117   jmp .read_alpha
118
119 .end:
120   ;; Finally, we want to skip one whitespace character after the word.
121   inc rsi
122   dec rcx
123
124   ret
125
126 ;; Parses a string.
127 ;;
128 ;; Parameters:
129 ;;   * rcx = Length of string
130 ;;   * rdi = Pointer to string buffer
131 ;;
132 ;; Results:
133 ;;   * rax = Value
134 ;;
135 ;; Clobbers
136 parse_number:
137   mov r8, 0                     ; Result
138
139   ;; Add (10^(rcx-1) * parse_char(rdi[length - rcx])) to the accumulated value
140   ;; for each rcx.
141   mov [.length], rcx
142 .loop:
143   ;; First, calcuate 10^(rcx - 1)
144   mov rax, 1
145
146   mov r9, rcx
147   .exp_loop:
148     dec r9
149     jz .break
150     mov rbx, 10
151     mul rbx
152     jmp .exp_loop
153   .break:
154
155   ;; Now, rax = 10^(rcx - 1).
156
157   ;; We need to calulate the value of the character at rdi[length - rcx].
158    mov rbx, rdi
159   add rbx, [.length]
160   sub rbx, rcx
161   movzx rbx, byte [rbx]
162   sub rbx, '0'
163
164   cmp rbx, 10
165   jae .error
166
167   ;; Multiply this value by rax to get (10^(rcx-1) * parse_char(rdi[length - rcx])),
168   ;; then add this to the result.
169   mul rbx
170
171   ;; Add that value to r8
172   add r8, rax
173
174   dec rcx
175   jnz .loop
176
177   mov rax, r8
178   ret
179
180 .error:
181   push rdi
182   print parse_number.error_msg
183   pop rdi
184   printlen rdi, [.length]
185   newline
186   mov rax, 100
187   call os_terminate
188
189 section '.data' readable writable
190
191 find.search_length dq ?
192 find.search_buffer dq ?
193
194 parse_number.length dq ?
195 parse_number.error_msg string "Invalid number: "
196