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
49 ;; Otherwise, we need to compare strings
50 lea rdx, [rsi + 8 + 1 + 1] ; Location of character being compared in entry
51 mov rdi, [.search_buffer] ; Location of character being compared in
57 jne .next ; They don't match; try again
58 inc rdx ; These characters match; look at the
63 jmp .found ; They match! We are done.
66 mov rsi, [rsi] ; Look at the previous entry
68 jnz .loop ; If there is no previous word, exit and return 0
73 ;; Read a word from a buffer. Returns the buffer without the word, as
74 ;; well as the word that was read (including lengths).
77 ;; * rsi = Input buffer
78 ;; * rcx = Length of buffer
81 ;; * rsi = Updated buffer
82 ;; * rcx = Length of updated buffer
83 ;; * rdi = Word buffer
84 ;; * rdx = Length of word buffer
94 ;; The buffer starts with whitespace; discard the first character
101 ;; We got a character that wasn't whitespace. Now read the actual
103 mov rdi, rsi ; This is where the word starts
104 mov rdx, 1 ; Length of word
107 ;; Extract character from original buffer:
111 ;; When we hit whitespace, we are done with this word
118 ;; It wasn't whitespace; add it to word buffer
123 ;; Finally, we want to skip one whitespace character after the word.
132 ;; * rcx = Length of string
133 ;; * rdi = Pointer to string buffer
142 ;; Add (10^(rcx-1) * parse_char(rdi[length - rcx])) to the
143 ;; accumulated value for each rcx.
146 ;; First, calcuate 10^(rcx - 1)
158 ;; Now, rax = 10^(rcx - 1).
160 ;; We need to calulate the value of the character at rdi[length - rcx].
164 movzx rbx, byte [rbx]
170 ;; Multiply this value by rax to get
171 ;; (10^(rcx-1) * parse_char(rdi[length - rcx]))
172 ;; then add this to the result.
175 ;; Add that value to r8
186 print parse_number.error_msg
188 printlen rdi, [.length]
193 section '.data' readable writable
195 find.search_length dq ?
196 find.search_buffer dq ?
198 parse_number.length dq ?
199 parse_number.error_msg string "Invalid number: "