3 segment readable executable
5 macro printlen msg, len {
25 printlen msg, msg#.len
39 ;; Find the given word in the dictionary of words. If no such word exists,
43 ;; * [find.search_length] = Length of the word in bytes.
44 ;; * [find.search_buffer] = Pointer to the string containing the word.
45 ;; * rsi = Pointer to the last entry in the dictionary.
48 ;; * rsi = Pointer to the found entry in the dictionary or 0.
50 ;; Clobbers rcx, rdx, rdi, rax.
52 ;; RSI contains the entry we are currently looking at
54 movzx rcx, byte [rsi + 8 + 1] ; Length of word being looked at
55 cmp rcx, [.search_length]
56 jne .next ; If the words don't have the same length, we have the wrong word
58 ;; Otherwise, we need to compare strings
59 lea rdx, [rsi + 8 + 1 + 1] ; Location of character being compared in entry
60 mov rdi, [.search_buffer] ; Location of character being compared in search buffer
65 jne .next ; They don't match; try again
66 inc rdx ; These characters match; look at the next ones
70 jmp .found ; They match! We are done.
73 mov rsi, [rsi] ; Look at the previous entry
75 jnz .loop ; If there is no previous word, exit and return 0
80 ;; Read a word from standard input. Returns pointer to string containing word as
84 ;; * rdx = Length of string
85 ;; * rdi = Pointer to string buffer
87 ;; Clobbers pretty much everything.
90 ;; Read characters into .char_buffer until one of them is not whitespace.
97 ;; We consider newlines and spaces to be whitespace.
98 cmp [.char_buffer], ' '
100 cmp [.char_buffer], $A
104 ;; We got a character that wasn't whitespace. Now read the actual word.
108 mov al, [.char_buffer]
117 mov rsi, .char_buffer
121 cmp [.char_buffer], ' '
123 cmp [.char_buffer], $A
135 ;; * rcx = Length of string
136 ;; * rdi = Pointer to string buffer
145 ;; Add (10^(rcx-1) * parse_char(rdi[length - rcx])) to the accumulated value
149 ;; First, calcuate 10^(rcx - 1)
161 ;; Now, rax = 10^(rcx - 1).
163 ;; We need to calulate the value of the character at rdi[length - rcx].
167 movzx rbx, byte [rbx]
173 ;; Multiply this value by rax to get (10^(rcx-1) * parse_char(rdi[length - rcx])),
174 ;; then add this to the result.
177 ;; Add that value to r8
188 print parse_number.error_msg
190 printlen rdi, [.length]
194 segment readable writable
196 find.search_length dq ?
197 find.search_buffer dq ?
199 read_word.max_size = $FF
200 read_word.buffer rb read_word.max_size
201 read_word.length db ?
202 read_word.char_buffer db ?
204 parse_number.length dq ?
205 parse_number.error_msg string "Invalid number: "