-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.asm
More file actions
250 lines (206 loc) · 3.63 KB
/
utility.asm
File metadata and controls
250 lines (206 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
%include "common.asm"
;TODO
extern stdout
extern stdin
; UTILITIES
; =============================================================================
section .text
; Functions:
; try_parse_number(buffer, length)
; expect_token(buf)
; range(value, lower, upper)
; is_whitespace(val)
; consume_whitespace(stream)
; consume_non_whitespace(stream)
; print(s)
; strncmp(buf_a, buf_b, length)
; IN: buffer, length
; OUT: eax = negative on not number, edx = parsed number if success
try_parse_number:
_prologue
push esi
push edi
mov esi, 0 ;result
mov edi, 0 ;count
.loop:
cmp edi, _ARG(1)
jge .finish
mov eax, _ARG(0)
add eax, edi
movzx eax, byte [eax]
push eax
?number eax, jz .error
pop eax
sub eax, '0'
imul esi, 10
add esi, eax
inc edi
jmp .loop
.finish:
mov eax, 0
mov edx, esi
jmp .ret
.error:
mov eax, -1
.ret:
lea esp, _SLOT(2)
pop edi
pop esi
_epilogue
; IN: stream, pointer to buffer of size M_SYM_NAME
; OUT: eax = token length or negative on error
; reads a token from stream
expect_token: ;expect_token(stream, buf)
_prologue
push ebx
mov ebx, 0
.loop:
cmp ebx, M_SYM_NAME
jge .bad
Peak(_ARG(0), .bad)
?printable eax, jz .finish
Get(stdin, .bad)
mov ecx, _ARG(1)
mov [ecx + ebx], al
inc ebx
jmp .loop
.finish:
mov eax, ebx
cmp ebx, 0
jg .ret
.bad:
mov eax, -1
.ret:
lea esp, _SLOT(1)
pop ebx
_epilogue
; IN: val, low, high
; OUT: bool
range: ;range(val, low, high)
_prologue
mov eax, 1
mov edx, _ARG(0)
cmp dl, _ARG(1)
jl .fail
cmp dl, _ARG(2)
jg .fail
jmp .ret
.fail:
mov eax, 0
.ret:
_epilogue
; IN: val
; OUT: bool
is_whitespace: ;is_whitespace(val)
_prologue
mov ecx, _ARG(0)
mov eax, 1
cmp ecx, ' '
je .ret
cmp ecx, 9 ;'\t'
je .ret
cmp ecx, 10 ;'\n'
je .ret
cmp ecx, 11 ;'\v'
je .ret
cmp ecx, 12 ;'\f'
je .ret
cmp ecx, 12 ;'\r'
je .ret
mov eax, 0
.ret:
_epilogue
; IN: stream
; OUT: eax = negative on error
consume_whitespace: ;consume_whitespace(stream)
_prologue
.loop:
Peak(_ARG(0), .ret)
@call1 is_whitespace, eax
test eax, eax
jz .good
Get(_ARG(0), .ret)
jmp .loop
.good:
mov eax, 0
.ret:
_epilogue
; IN: stream
; OUT: eax = negative on error
consume_non_whitespace: ;consume_non_whitespace(stream)
_prologue
.loop:
Peak(_ARG(0), .ret)
@call1 is_whitespace, eax
test eax, eax
jnz .good
Get(_ARG(0), .ret)
jmp .loop
.good:
mov eax, 0
.ret:
_epilogue
; IN: cstring
; OUT: eax = wrote or negative on error
; prints a null terminated string
print: ;print(cstring)
_prologue
push _ARG(0)
push stdout
call fprint
_epilogue
; IN: file*, cstring
; OUT: eax = wrote or negative on error
fprint: ;fprint(file*, cstring)
_prologue
mov eax, _ARG(1)
mov edx, 0
.loop:
mov cl, [eax + edx]
cmp cl, 0
je .write
add edx, 1
jmp .loop
.write:
push edx
push eax
push _ARG(0)
call file_write
_epilogue
; IN: buf_a, buf_b, length
; OUT: eax = -1 l, 0 eq, 1 g
; compare two strings of given length
strncmp: ;strncmp(buf_a, buf_b, length)
_prologue
push esi
push edi
push ebx
mov esi, _ARG(0)
mov edi, _ARG(1)
mov ebx, 0
.loop:
cmp ebx, _ARG(2)
jge .eq
mov cl, [esi]
mov dl, [edi]
cmp cl, dl
jl .lt
jg .gt
inc esi
inc edi
inc ebx
jmp .loop
.gt:
mov eax, 1
jmp .ret
.lt:
mov eax, -1
jmp .ret
.eq:
mov eax, 0
.ret:
lea esp, _SLOT(3)
pop ebx
pop edi
pop esi
_epilogue