https://github.com/akkartik/mu/blob/master/subx/071read-line.subx
1 == code
2
3
4
5
6
7 e8/call run-tests/disp32
8
9 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32
10 b8/copy-to-EAX 1/imm32/exit
11 cd/syscall 0x80/imm8
12
13
14
15
16 read-line:
17
18
19
20
21
22
23
24
25
26
27
28 55/push-EBP
29 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . .
30
31 51/push-ECX
32 52/push-EDX
33 56/push-ESI
34 57/push-EDI
35
36 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 .
37
38 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 .
39
40 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 0xc/disp8 .
41
42 8b/copy 0/mod/indirect 7/rm32/EDI . . . 2/r32/EDX . .
43 $read-line:loop:
44
45 3b/compare 1/mod/*+disp8 7/rm32/EDI . . . 2/r32/EDX 8/disp8 .
46 7d/jump-if-greater-or-equal $read-line:abort/disp8
47
48 3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 .
49 7c/jump-if-lesser $read-line:from-stream/disp8
50
51
52 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 .
53 50/push-EAX
54
55 e8/call clear-stream/disp32
56
57 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
58
59 31/xor 3/mod/direct 1/rm32/ECX . . . 1/r32/ECX . .
60
61
62 50/push-EAX
63 ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . .
64
65 e8/call read/disp32
66
67 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
68
69
70 81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0/imm32
71 75/jump-if-not-equal $read-line:from-stream/disp8
72 b8/copy-to-EAX 0xffffffff/imm32
73 eb/jump $read-line:end/disp8
74 $read-line:from-stream:
75
76 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . .
77 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0x10/disp8 .
78
79 88/copy-byte 1/mod/*+disp8 4/rm32/sib 7/base/EDI 2/index/EDX . 0/r32/AL 0xc/disp8 .
80
81 41/increment-ECX
82
83 42/increment-EDX
84
85 81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0xa/imm32
86 75/jump-if-not-equal $read-line:loop/disp8
87 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . .
88 $read-line:end:
89
90 89/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 .
91
92 89/copy 0/mod/indirect 7/rm32/EDI . . . 2/r32/EDX . .
93
94 5f/pop-to-EDI
95 5e/pop-to-ESI
96 5a/pop-to-EDX
97 59/pop-to-ECX
98
99 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . .
100 5d/pop-to-EBP
101 c3/return
102
103 $read-line:abort:
104
105
106 68/push "read-line: line too long"/imm32
107 68/push 2/imm32/stderr
108
109 e8/call _write/disp32
110
111 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
112
113 bb/copy-to-EBX 1/imm32
114 b8/copy-to-EAX 1/imm32/exit
115 cd/syscall 0x80/imm8
116
117
118 test-read-line:
119
120
121
122
123 68/push _test-stream/imm32
124
125 e8/call clear-stream/disp32
126
127 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
128
129
130 b8/copy-to-EAX _test-buffered-file/imm32
131 05/add-to-EAX 4/imm32
132 50/push-EAX
133
134 e8/call clear-stream/disp32
135
136 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
137
138
139 68/push _test-tmp-stream/imm32
140
141 e8/call clear-stream/disp32
142
143 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
144
145
146
147 68/push "ab"/imm32
148 68/push _test-stream/imm32
149
150 e8/call write/disp32
151
152 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
153
154
155 68/push Newline/imm32
156 68/push _test-stream/imm32
157
158 e8/call write/disp32
159
160 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
161
162
163 68/push "cd"/imm32
164 68/push _test-stream/imm32
165
166 e8/call write/disp32
167
168 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
169
170
171
172 68/push _test-tmp-stream/imm32
173 68/push _test-buffered-file/imm32
174
175 e8/call read-line/disp32
176
177 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
178
179
180 68/push "F - test-read-line: return value"/imm32
181 68/push 0/imm32/not-at-eof
182 50/push-EAX
183
184 e8/call check-ints-equal/disp32
185
186 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
187
188
189 68/push "F - test-read-line"/imm32
190 68/push "ab"/imm32
191 68/push _test-tmp-stream/imm32
192
193 e8/call check-next-stream-line-equal/disp32
194
195 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
196
197 c3/return
198
199 test-read-line-returns-true-on-eof:
200
201
202
203 68/push _test-stream/imm32
204
205 e8/call clear-stream/disp32
206
207 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
208
209
210 b8/copy-to-EAX _test-buffered-file/imm32
211 05/add-to-EAX 4/imm32
212 50/push-EAX
213
214 e8/call clear-stream/disp32
215
216 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
217
218
219 68/push _test-tmp-stream/imm32
220
221 e8/call clear-stream/disp32
222
223 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
224
225
226
227 68/push _test-tmp-stream/imm32
228 68/push _test-buffered-file/imm32
229
230 e8/call read-line/disp32
231
232 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
233
234
235 68/push "F - test-read-line-returns-true-on-eof"/imm32
236 68/push 0xffffffff/imm32/not-at-eof
237 50/push-EAX
238
239 e8/call check-ints-equal/disp32
240
241 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
242
243 c3/return
244
245 test-read-line-reads-final-line-until-eof:
246
247
248
249 68/push _test-stream/imm32
250
251 e8/call clear-stream/disp32
252
253 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
254
255
256 b8/copy-to-EAX _test-buffered-file/imm32
257 05/add-to-EAX 4/imm32
258 50/push-EAX
259
260 e8/call clear-stream/disp32
261
262 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
263
264
265 68/push _test-tmp-stream/imm32
266
267 e8/call clear-stream/disp32
268
269 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
270
271
272 68/push "cd"/imm32
273 68/push _test-stream/imm32
274
275 e8/call write/disp32
276
277 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
278
279
280
281 68/push _test-tmp-stream/imm32
282 68/push _test-buffered-file/imm32
283
284 e8/call read-line/disp32
285
286 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
287
288
289 68/push "F - test-read-line-reads-final-line-until-eof: return value"/imm32
290 68/push 0xffffffff/imm32/not-at-eof
291 50/push-EAX
292
293 e8/call check-ints-equal/disp32
294
295 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
296
297
298 68/push "F - test-read-line-reads-final-line-until-eof"/imm32
299 68/push "cd"/imm32
300 68/push _test-tmp-stream/imm32
301
302 e8/call check-stream-equal/disp32
303
304 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
305
306 c3/return
307
308