https://github.com/akkartik/mu/blob/master/subx/068read-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-stream-buffer/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-stream-buffer/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 0x000a6261/imm32
191
192 b8/copy-to-EAX _test-stream-buffer/imm32
193 ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 0xc/disp8 .
194
195 e8/call check-ints-equal/disp32
196
197 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
198
199 c3/return
200
201 test-read-line-returns-true-on-eof:
202
203
204
205 68/push _test-stream/imm32
206
207 e8/call clear-stream/disp32
208
209 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
210
211
212 b8/copy-to-EAX _test-buffered-file/imm32
213 05/add-to-EAX 4/imm32
214 50/push-EAX
215
216 e8/call clear-stream/disp32
217
218 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
219
220
221 68/push _test-stream-buffer/imm32
222
223 e8/call clear-stream/disp32
224
225 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
226
227
228
229 68/push _test-stream-buffer/imm32
230 68/push _test-buffered-file/imm32
231
232 e8/call read-line/disp32
233
234 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
235
236
237 68/push "F - test-read-line-returns-true-on-eof"/imm32
238 68/push 0xffffffff/imm32/not-at-eof
239 50/push-EAX
240
241 e8/call check-ints-equal/disp32
242
243 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
244
245 c3/return
246
247 test-read-line-reads-final-line-until-eof:
248
249
250
251 68/push _test-stream/imm32
252
253 e8/call clear-stream/disp32
254
255 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
256
257
258 b8/copy-to-EAX _test-buffered-file/imm32
259 05/add-to-EAX 4/imm32
260 50/push-EAX
261
262 e8/call clear-stream/disp32
263
264 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
265
266
267 68/push _test-stream-buffer/imm32
268
269 e8/call clear-stream/disp32
270
271 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
272
273
274 68/push "cd"/imm32
275 68/push _test-stream/imm32
276
277 e8/call write/disp32
278
279 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
280
281
282
283 68/push _test-stream-buffer/imm32
284 68/push _test-buffered-file/imm32
285
286 e8/call read-line/disp32
287
288 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
289
290
291 68/push "F - test-read-line-reads-final-line-until-eof: return value"/imm32
292 68/push 0xffffffff/imm32/not-at-eof
293 50/push-EAX
294
295 e8/call check-ints-equal/disp32
296
297 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
298
299
300 68/push "F - test-read-line-reads-final-line-until-eof"/imm32
301 68/push 0x00006463/imm32
302
303 b8/copy-to-EAX _test-stream-buffer/imm32
304 ff 6/subop/push 1/mod/*+disp8 0/rm32/EAX . . . . 0xc/disp8 .
305
306 e8/call check-ints-equal/disp32
307
308 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
309
310 c3/return
311
312