1 section '.text' code readable executable
3 macro printlen msg, len {
21 printlen msg, msg#.len
29 ;; Find the given word in the dictionary of words. If no such word exists,
33 ;; * [find.search_length] = Length of the word in bytes.
34 ;; * [find.search_buffer] = Pointer to the string containing the word.
35 ;; * rsi = Pointer to the last entry in the dictionary.
38 ;; * rsi = Pointer to the found entry in the dictionary or 0.
40 ;; Clobbers rcx, rdx, rdi, rax.
42 ;; RSI contains the entry we are currently looking at
44 movzx rcx, byte [rsi + 8 + 1] ; Length of word being looked at
45 cmp rcx, [.search_length]
46 jne .next ; If the words don't have the same length, we have the wrong word
48 ;; Otherwise, we need to compare strings
49 lea rdx, [rsi + 8 + 1 + 1] ; Location of character being compared in entry
50 mov rdi, [.search_buffer] ; Location of character being compared in search buffer
55 jne .next ; They don't match; try again
56 inc rdx ; These characters match; look at the next ones
60 jmp .found ; They match! We are done.
63 mov rsi, [rsi] ; Look at the previous entry
65 jnz .loop ; If there is no previous word, exit and return 0
70 ;; Read a word from a buffer. Returns the buffer without the word, as well as
71 ;; the word that was read (including lengths).
74 ;; * rsi = Input buffer
75 ;; * rcx = Length of buffer
78 ;; * rsi = Updated buffer
79 ;; * rcx = Length of updated buffer
80 ;; * rdi = Word buffer
81 ;; * rdx = Length of word buffer
91 ;; The buffer starts with whitespace; discard the first character from the buffer.
97 ;; We got a character that wasn't whitespace. Now read the actual word.
98 mov rdi, rsi ; This is where the word starts
99 mov rdx, 1 ; Length of word
102 ;; Extract character from original buffer:
106 ;; When we hit whitespace, we are done with this word
113 ;; It wasn't whitespace; add it to word buffer
118 ;; Finally, we want to skip one whitespace character after the word.
127 ;; * rcx = Length of string
128 ;; * rdi = Pointer to string buffer
137 ;; Add (10^(rcx-1) * parse_char(rdi[length - rcx])) to the accumulated value
141 ;; First, calcuate 10^(rcx - 1)
153 ;; Now, rax = 10^(rcx - 1).
155 ;; We need to calulate the value of the character at rdi[length - rcx].
159 movzx rbx, byte [rbx]
165 ;; Multiply this value by rax to get (10^(rcx-1) * parse_char(rdi[length - rcx])),
166 ;; then add this to the result.
169 ;; Add that value to r8
180 print parse_number.error_msg
182 printlen rdi, [.length]
187 section '.data' readable writable
189 find.search_length dq ?
190 find.search_buffer dq ?
192 parse_number.length dq ?
193 parse_number.error_msg string "Invalid number: "