3 segment readable executable
5 macro printlen msg, len {
23 printlen msg, msg#.len
31 ;; Find the given word in the dictionary of words. If no such word exists,
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.
40 ;; * rsi = Pointer to the found entry in the dictionary or 0.
42 ;; Clobbers rcx, rdx, rdi, rax.
44 ;; RSI contains the entry we are currently looking at
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
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
57 jne .next ; They don't match; try again
58 inc rdx ; These characters match; look at the next ones
62 jmp .found ; They match! We are done.
65 mov rsi, [rsi] ; Look at the previous entry
67 jnz .loop ; If there is no previous word, exit and return 0
72 ;; Read a word from standard input. Returns pointer to string containing word as
76 ;; * rdx = Length of string
77 ;; * rdi = Pointer to string buffer
79 ;; Clobbers pretty much everything.
82 ;; Read characters into .char_buffer until one of them is not whitespace.
86 ;; We consider newlines and spaces to be whitespace.
87 cmp [.char_buffer], ' '
89 cmp [.char_buffer], $A
93 ;; We got a character that wasn't whitespace. Now read the actual word.
97 mov al, [.char_buffer]
104 mov rsi, .char_buffer
107 cmp [.char_buffer], ' '
109 cmp [.char_buffer], $A
118 ;; Read a word from a buffer. Returns the buffer without the word, as well as
119 ;; the word that was read (including lengths).
122 ;; * rsi = Input buffer
123 ;; * rcx = Length of buffer
126 ;; * rsi = Updated buffer
127 ;; * rcx = Length of updated buffer
128 ;; * rdi = Word buffer
129 ;; * rdx = Length of word buffer
139 ;; The buffer starts with whitespace; discard the first character from the buffer.
145 ;; We got a character that wasn't whitespace. Now read the actual word.
146 mov rdi, rsi ; This is where the word starts
147 mov rdx, 1 ; Length of word
150 ;; Extract character from original buffer:
154 ;; When we hit whitespace, we are done with this word
161 ;; It wasn't whitespace; add it to word buffer
166 ;; Finally, we want to skip one whitespace character after the word.
175 ;; * rcx = Length of string
176 ;; * rdi = Pointer to string buffer
185 ;; Add (10^(rcx-1) * parse_char(rdi[length - rcx])) to the accumulated value
189 ;; First, calcuate 10^(rcx - 1)
201 ;; Now, rax = 10^(rcx - 1).
203 ;; We need to calulate the value of the character at rdi[length - rcx].
207 movzx rbx, byte [rbx]
213 ;; Multiply this value by rax to get (10^(rcx-1) * parse_char(rdi[length - rcx])),
214 ;; then add this to the result.
217 ;; Add that value to r8
228 print parse_number.error_msg
230 printlen rdi, [.length]
234 segment readable writable
236 find.search_length dq ?
237 find.search_buffer dq ?
239 read_word.max_size = $FF
240 read_word.buffer rb read_word.max_size
241 read_word.length db ?
242 read_word.char_buffer db ?
244 parse_number.length dq ?
245 parse_number.error_msg string "Invalid number: "