https://github.com/akkartik/mu/blob/master/071read-line.subx
  1 == code
  2 #   instruction                     effective address                                                   register    displacement    immediate
  3 # . op          subop               mod             rm32          base        index         scale       r32
  4 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
  5 
  6 # read bytes from 'f' until (and including) a newline and store them into 's'
  7 # 's' fails to grow if and only if no data found
  8 # just abort if 's' is too small
  9 read-line-buffered:  # f : (address buffered-file), s : (address stream byte) -> <void>
 10     # pseudocode:
 11     #   while true
 12     #     if (s->write >= s->length) abort
 13     #     if (f->read >= f->write) populate stream from file
 14     #     if (f->write == 0) break
 15     #     AL = f->data[f->read]
 16     #     s->data[s->write] = AL
 17     #     ++f->read
 18     #     ++s->write
 19     #     if (AL == '\n') break
 20     # . prolog
 21     55/push-ebp
 22     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 23     # . save registers
 24     50/push-eax
 25     51/push-ecx
 26     52/push-edx
 27     56/push-esi
 28     57/push-edi
 29     # esi = f
 30     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
 31     # ecx = f->read
 32     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(esi+8) to ecx
 33     # edi = s
 34     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
 35     # edx = s->write
 36     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy *edi to edx
 37 $read-line-buffered:loop:
 38     # if (s->write >= s->length) abort
 39     3b/compare                      1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # compare edx with *(edi+8)
 40     7d/jump-if-greater-or-equal  $read-line-buffered:abort/disp8
 41     # if (f->read >= f->write) populate stream from file
 42     3b/compare                      1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # compare ecx with *(esi+4)
 43     7c/jump-if-lesser  $read-line-buffered:from-stream/disp8
 44     # . clear-stream(stream = f+4)
 45     # . . push args
 46     8d/copy-address                 1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy esi+4 to eax
 47     50/push-eax
 48     # . . call
 49     e8/call  clear-stream/disp32
 50     # . . discard args
 51     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 52     # . f->read must now be 0; update its cache at ecx
 53     31/xor                          3/mod/direct    1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # clear ecx
 54     # . eax = read(f->fd, stream = f+4)
 55     # . . push args
 56     50/push-eax
 57     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
 58     # . . call
 59     e8/call  read/disp32
 60     # . . discard args
 61     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 62     # if (f->write == 0) break
 63     # since f->read was initially 0, eax is the same as f->write
 64     # . if (eax == 0) return true
 65     3d/compare-eax-and  0/imm32
 66     74/jump-if-equal  $read-line-buffered:end/disp8
 67 $read-line-buffered:from-stream:
 68     # AL = f->data[f->read]
 69     31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
 70     8a/copy-byte                    1/mod/*+disp8   4/rm32/sib    6/base/esi  1/index/ecx   .           0/r32/AL    0x10/disp8      .                 # copy byte at *(esi+ecx+16) to AL
 71     # s->data[s->write] = AL
 72     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/edi  2/index/edx   .           0/r32/AL    0xc/disp8       .                 # copy AL to *(edi+edx+12)
 73     # ++f->read
 74     41/increment-ecx
 75     # ++s->write
 76     42/increment-edx
 77     # if (AL == '\n') return
 78     3d/compare-eax-and  0xa/imm32
 79     75/jump-if-not-equal  $read-line-buffered:loop/disp8
 80 $read-line-buffered:end:
 81     # save f->read
 82     89/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   8/disp8         .                 # copy ecx to *(esi+8)
 83     # save s->write
 84     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy edx to *edi
 85     # . restore registers
 86     5f/pop-to-edi
 87     5e/pop-to-esi
 88     5a/pop-to-edx
 89     59/pop-to-ecx
 90     58/pop-to-eax
 91     # . epilog
 92     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 93     5d/pop-to-ebp
 94     c3/return
 95 
 96 $read-line-buffered:abort:
 97     # . _write(2/stderr, error)
 98     # . . push args
 99     68/push  "read-line-buffered: line too long\n"/imm32
100     68/push  2/imm32/stderr
101     # . . call
102     e8/call  _write/disp32
103     # . . discard args
104     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
105     # . syscall(exit, 1)
106     bb/copy-to-ebx  1/imm32
107     b8/copy-to-eax  1/imm32/exit
108     cd/syscall  0x80/imm8
109     # never gets here
110 
111 test-read-line-buffered:
112     # - check that read-line-buffered stops at a newline
113     # setup
114     # . clear-stream(_test-stream)
115     # . . push args
116     68/push  _test-stream/imm32
117     # . . call
118     e8/call  clear-stream/disp32
119     # . . discard args
120     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
121     # . clear-stream(_test-buffered-file+4)
122     # . . push args
123     b8/copy-to-eax  _test-buffered-file/imm32
124     05/add-to-eax  4/imm32
125     50/push-eax
126     # . . call
127     e8/call  clear-stream/disp32
128     # . . discard args
129     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
130     # . clear-stream(_test-tmp-stream)
131     # . . push args
132     68/push  _test-tmp-stream/imm32
133     # . . call
134     e8/call  clear-stream/disp32
135     # . . discard args
136     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
137     # write(_test-stream, "ab\ncd")
138     # . . push args
139     68/push  "ab\ncd"/imm32
140     68/push  _test-stream/imm32
141     # . . call
142     e8/call  write/disp32
143     # . . discard args
144     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
145     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
146     # . eax = read-line-buffered(_test-buffered-file, _test-tmp-stream)
147     # . . push args
148     68/push  _test-tmp-stream/imm32
149     68/push  _test-buffered-file/imm32
150     # . . call
151     e8/call  read-line-buffered/disp32
152     # . . discard args
153     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
154     # check-next-stream-line-equal(_test-tmp-stream, "ab", msg)
155     # . . push args
156     68/push  "F - test-read-line-buffered"/imm32
157     68/push  "ab"/imm32
158     68/push  _test-tmp-stream/imm32
159     # . . call
160     e8/call  check-next-stream-line-equal/disp32
161     # . . discard args
162     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
163     # end
164     c3/return
165 
166 test-read-line-buffered-reads-final-line-until-Eof:
167     # setup
168     # . clear-stream(_test-stream)
169     # . . push args
170     68/push  _test-stream/imm32
171     # . . call
172     e8/call  clear-stream/disp32
173     # . . discard args
174     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
175     # . clear-stream(_test-buffered-file+4)
176     # . . push args
177     b8/copy-to-eax  _test-buffered-file/imm32
178     05/add-to-eax  4/imm32
179     50/push-eax
180     # . . call
181     e8/call  clear-stream/disp32
182     # . . discard args
183     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
184     # . clear-stream(_test-tmp-stream)
185     # . . push args
186     68/push  _test-tmp-stream/imm32
187     # . . call
188     e8/call  clear-stream/disp32
189     # . . discard args
190     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
191     # write(_test-stream, "cd")
192     # . . push args
193     68/push  "cd"/imm32
194     68/push  _test-stream/imm32
195     # . . call
196     e8/call  write/disp32
197     # . . discard args
198     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
199     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
200     # . eax = read-line-buffered(_test-buffered-file, _test-tmp-stream)
201     # . . push args
202     68/push  _test-tmp-stream/imm32
203     68/push  _test-buffered-file/imm32
204     # . . call
205     e8/call  read-line-buffered/disp32
206     # . . discard args
207     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
208     # check-stream-equal(_test-tmp-stream, "cd", msg)
209     # . . push args
210     68/push  "F - test-read-line-buffered-reads-final-line-until-Eof"/imm32
211     68/push  "cd"/imm32
212     68/push  _test-tmp-stream/imm32
213     # . . call
214     e8/call  check-stream-equal/disp32
215     # . . discard args
216     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
217     # end
218     c3/return
219 
220 # read bytes from 'f' until (and including) a newline and store them into 's'
221 # 's' fails to grow if and only if no data found
222 # just abort if 's' is too small
223 read-line:  # f : (address stream), s : (address stream byte) -> <void>
224     # pseudocode:
225     #   while true
226     #     if (s->write >= s->length) abort
227     #     if (f->read >= f->write) break
228     #     AL = f->data[f->read]
229     #     s->data[s->write] = AL
230     #     ++f->read
231     #     ++s->write
232     #     if (AL == '\n') break
233     # . prolog
234     55/push-ebp
235     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
236     # . save registers
237     50/push-eax
238     51/push-ecx
239     52/push-edx
240     56/push-esi
241     57/push-edi
242     # esi = f
243     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
244     # ecx = f->read
245     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # copy *(esi+4) to ecx
246     # edi = s
247     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
248     # edx = s->write
249     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy *edi to edx
250 $read-line:loop:
251     # if (s->write >= s->length) abort
252     3b/compare                      1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # compare edx with *(edi+8)
253     0f 8d/jump-if-greater-or-equal  $read-line:abort/disp32
254     # if (f->read >= f->write) break
255     3b/compare                      0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare ecx with *esi
256     7d/jump-if-greater-or-equal  $read-line:end/disp8
257     # AL = f->data[f->read]
258     31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
259     8a/copy-byte                    1/mod/*+disp8   4/rm32/sib    6/base/esi  1/index/ecx   .           0/r32/AL    0xc/disp8       .                 # copy byte at *(esi+ecx+12) to AL
260     # s->data[s->write] = AL
261     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/edi  2/index/edx   .           0/r32/AL    0xc/disp8       .                 # copy AL to *(edi+edx+12)
262     # ++f->read
263     41/increment-ecx
264     # ++s->write
265     42/increment-edx
266     # if (AL == '\n') return
267     3d/compare-eax-and  0xa/imm32
268     0f 85/jump-if-not-equal  $read-line:loop/disp32
269 $read-line:end:
270     # save f->read
271     89/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # copy ecx to *(esi+4)
272     # save s->write
273     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy edx to *edi
274     # . restore registers
275     5f/pop-to-edi
276     5e/pop-to-esi
277     5a/pop-to-edx
278     59/pop-to-ecx
279     58/pop-to-eax
280     # . epilog
281     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
282     5d/pop-to-ebp
283     c3/return
284 
285 $read-line:abort:
286     # . _write(2/stderr, error)
287     # . . push args
288     68/push  "read-line: line too long\n"/imm32
289     68/push  2/imm32/stderr
290     # . . call
291     e8/call  _write/disp32
292     # . . discard args
293     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
294     # . syscall(exit, 1)
295     bb/copy-to-ebx  1/imm32
296     b8/copy-to-eax  1/imm32/exit
297     cd/syscall  0x80/imm8
298     # never gets here
299 
300 test-read-line:
301     # - check that read-line stops at a newline
302     # setup
303     # . clear-stream(_test-stream)
304     # . . push args
305     68/push  _test-stream/imm32
306     # . . call
307     e8/call  clear-stream/disp32
308     # . . discard args
309     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
310     # . clear-stream(_test-tmp-stream)
311     # . . push args
312     68/push  _test-tmp-stream/imm32
313     # . . call
314     e8/call  clear-stream/disp32
315     # . . discard args
316     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
317     # write(_test-stream, "ab\ncd")
318     # . . push args
319     68/push  "ab\ncd"/imm32
320     68/push  _test-stream/imm32
321     # . . call
322     e8/call  write/disp32
323     # . . discard args
324     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
325     # read a line from _test-stream into _test-tmp-stream
326     # . eax = read-line(_test-stream, _test-tmp-stream)
327     # . . push args
328     68/push  _test-tmp-stream/imm32
329     68/push  _test-stream/imm32
330     # . . call
331     e8/call  read-line/disp32
332     # . . discard args
333     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
334     # check-next-stream-line-equal(_test-tmp-stream, "ab", msg)
335     # . . push args
336     68/push  "F - test-read-line"/imm32
337     68/push  "ab"/imm32
338     68/push  _test-tmp-stream/imm32
339     # . . call
340     e8/call  check-next-stream-line-equal/disp32
341     # . . discard args
342     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
343     # end
344     c3/return
345 
346 test-read-line-reads-final-line-until-Eof:
347     # setup
348     # . clear-stream(_test-stream)
349     # . . push args
350     68/push  _test-stream/imm32
351     # . . call
352     e8/call  clear-stream/disp32
353     # . . discard args
354     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
355     # . clear-stream(_test-tmp-stream)
356     # . . push args
357     68/push  _test-tmp-stream/imm32
358     # . . call
359     e8/call  clear-stream/disp32
360     # . . discard args
361     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
362     # write(_test-stream, "cd")
363     # . . push args
364     68/push  "cd"/imm32
365     68/push  _test-stream/imm32
366     # . . call
367     e8/call  write/disp32
368     # . . discard args
369     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
370     # read a line from _test-stream into _test-tmp-stream
371     # . eax = read-line(_test-stream, _test-tmp-stream)
372     # . . push args
373     68/push  _test-tmp-stream/imm32
374     68/push  _test-stream/imm32
375     # . . call
376     e8/call  read-line/disp32
377     # . . discard args
378     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
379     # check-stream-equal(_test-tmp-stream, "cd", msg)
380     # . . push args
381     68/push  "F - test-read-line-reads-final-line-until-Eof"/imm32
382     68/push  "cd"/imm32
383     68/push  _test-tmp-stream/imm32
384     # . . call
385     e8/call  check-stream-equal/disp32
386     # . . discard args
387     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
388     # end
389     c3/return
390 
391 # . . vim:nowrap:textwidth=0