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