3 section '.text' code 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 a buffer. Returns the buffer without the word, as well as
73 ;; the word that was read (including lengths).
76 ;; * rsi = Input buffer
77 ;; * rcx = Length of buffer
80 ;; * rsi = Updated buffer
81 ;; * rcx = Length of updated buffer
82 ;; * rdi = Word buffer
83 ;; * rdx = Length of word buffer
93 ;; The buffer starts with whitespace; discard the first character from the buffer.
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
104 ;; Extract character from original buffer:
108 ;; When we hit whitespace, we are done with this word
115 ;; It wasn't whitespace; add it to word buffer
120 ;; Finally, we want to skip one whitespace character after the word.
129 ;; * rcx = Length of string
130 ;; * rdi = Pointer to string buffer
139 ;; Add (10^(rcx-1) * parse_char(rdi[length - rcx])) to the accumulated value
143 ;; First, calcuate 10^(rcx - 1)
155 ;; Now, rax = 10^(rcx - 1).
157 ;; We need to calulate the value of the character at rdi[length - rcx].
161 movzx rbx, byte [rbx]
167 ;; Multiply this value by rax to get (10^(rcx-1) * parse_char(rdi[length - rcx])),
168 ;; then add this to the result.
171 ;; Add that value to r8
182 print parse_number.error_msg
184 printlen rdi, [.length]
188 section '.data' readable writable
190 find.search_length dq ?
191 find.search_buffer dq ?
193 parse_number.length dq ?
194 parse_number.error_msg string "Invalid number: "