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 : (addr buffered-file), s : (addr stream byte)
 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     # . prologue
 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     # . epilogue
 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->buffer)
122     # . . push args
123     68/push  $_test-buffered-file->buffer/imm32
124     # . . call
125     e8/call  clear-stream/disp32
126     # . . discard args
127     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
128     # . clear-stream(_test-tmp-stream)
129     # . . push args
130     68/push  _test-tmp-stream/imm32
131     # . . call
132     e8/call  clear-stream/disp32
133     # . . discard args
134     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
135     # write(_test-stream, "ab\ncd")
136     # . . push args
137     68/push  "ab\ncd"/imm32
138     68/push  _test-stream/imm32
139     # . . call
140     e8/call  write/disp32
141     # . . discard args
142     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
143     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
144     # . eax = read-line-buffered(_test-buffered-file, _test-tmp-stream)
145     # . . push args
146     68/push  _test-tmp-stream/imm32
147     68/push  _test-buffered-file/imm32
148     # . . call
149     e8/call  read-line-buffered/disp32
150     # . . discard args
151     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
152     # check-next-stream-line-equal(_test-tmp-stream, "ab", msg)
153     # . . push args
154     68/push  "F - test-read-line-buffered"/imm32
155     68/push  "ab"/imm32
156     68/push  _test-tmp-stream/imm32
157     # . . call
158     e8/call  check-next-stream-line-equal/disp32
159     # . . discard args
160     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
161     # end
162     c3/return
163 
164 test-read-line-buffered-reads-final-line-until-Eof:
165     # setup
166     # . clear-stream(_test-stream)
167     # . . push args
168     68/push  _test-stream/imm32
169     # . . call
170     e8/call  clear-stream/disp32
171     # . . discard args
172     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
173     # . clear-stream($_test-buffered-file->buffer)
174     # . . push args
175     68/push  $_test-buffered-file->buffer/imm32
176     # . . call
177     e8/call  clear-stream/disp32
178     # . . discard args
179     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
180     # . clear-stream(_test-tmp-stream)
181     # . . push args
182     68/push  _test-tmp-stream/imm32
183     # . . call
184     e8/call  clear-stream/disp32
185     # . . discard args
186     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
187     # write(_test-stream, "cd")
188     # . . push args
189     68/push  "cd"/imm32
190     68/push  _test-stream/imm32
191     # . . call
192     e8/call  write/disp32
193     # . . discard args
194     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
195     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
196     # . eax = read-line-buffered(_test-buffered-file, _test-tmp-stream)
197     # . . push args
198     68/push  _test-tmp-stream/imm32
199     68/push  _test-buffered-file/imm32
200     # . . call
201     e8/call  read-line-buffered/disp32
202     # . . discard args
203     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
204     # check-stream-equal(_test-tmp-stream, "cd", msg)
205     # . . push args
206     68/push  "F - test-read-line-buffered-reads-final-line-until-Eof"/imm32
207     68/push  "cd"/imm32
208     68/push  _test-tmp-stream/imm32
209     # . . call
210     e8/call  check-stream-equal/disp32
211     # . . discard args
212     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
213     # end
214     c3/return
215 
216 # read bytes from 'f' until (and including) a newline and store them into 's'
217 # 's' fails to grow if and only if no data found
218 # just abort if 's' is too small
219 read-line:  # f : (addr stream byte), s : (addr stream byte)
220     # pseudocode:
221     #   while true
222     #     if (s->write >= s->length) abort
223     #     if (f->read >= f->write) break
224     #     AL = f->data[f->read]
225     #     s->data[s->write] = AL
226     #     ++f->read
227     #     ++s->write
228     #     if (AL == '\n') break
229     # . prologue
230     55/push-ebp
231     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
232     # . save registers
233     50/push-eax
234     51/push-ecx
235     52/push-edx
236     56/push-esi
237     57/push-edi
238     # esi = f
239     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
240     # ecx = f->read
241     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # copy *(esi+4) to ecx
242     # edi = s
243     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
244     # edx = s->write
245     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy *edi to edx
246 $read-line:loop:
247     # if (s->write >= s->length) abort
248     3b/compare                      1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # compare edx with *(edi+8)
249     0f 8d/jump-if-greater-or-equal  $read-line:abort/disp32
250     # if (f->read >= f->write) break
251     3b/compare                      0/mod/indirect  6/rm32/esi    .           .             .           1/r32/ecx   .               .                 # compare ecx with *esi
252     7d/jump-if-greater-or-equal  $read-line:end/disp8
253     # AL = f->data[f->read]
254     31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
255     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
256     # s->data[s->write] = AL
257     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)
258     # ++f->read
259     41/increment-ecx
260     # ++s->write
261     42/increment-edx
262     # if (AL == '\n') return
263     3d/compare-eax-and  0xa/imm32
264     0f 85/jump-if-not-equal  $read-line:loop/disp32
265 $read-line:end:
266     # save f->read
267     89/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # copy ecx to *(esi+4)
268     # save s->write
269     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           2/r32/edx   .               .                 # copy edx to *edi
270     # . restore registers
271     5f/pop-to-edi
272     5e/pop-to-esi
273     5a/pop-to-edx
274     59/pop-to-ecx
275     58/pop-to-eax
276     # . epilogue
277     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
278     5d/pop-to-ebp
279     c3/return
280 
281 $read-line:abort:
282     # . _write(2/stderr, error)
283     # . . push args
284     68/push  "read-line: line too long\n"/imm32
285     68/push  2/imm32/stderr
286     # . . call
287     e8/call  _write/disp32
288     # . . discard args
289     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
290     # . syscall(exit, 1)
291     bb/copy-to-ebx  1/imm32
292     b8/copy-to-eax  1/imm32/exit
293     cd/syscall  0x80/imm8
294     # never gets here
295 
296 test-read-line:
297     # - check that read-line stops at a newline
298     # setup
299     # . clear-stream(_test-stream)
300     # . . push args
301     68/push  _test-stream/imm32
302     # . . call
303     e8/call  clear-stream/disp32
304     # . . discard args
305     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
306     # . clear-stream(_test-tmp-stream)
307     # . . push args
308     68/push  _test-tmp-stream/imm32
309     # . . call
310     e8/call  clear-stream/disp32
311     # . . discard args
312     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
313     # write(_test-stream, "ab\ncd")
314     # . . push args
315     68/push  "ab\ncd"/imm32
316     68/push  _test-stream/imm32
317     # . . call
318     e8/call  write/disp32
319     # . . discard args
320     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
321     # read a line from _test-stream into _test-tmp-stream
322     # . eax = read-line(_test-stream, _test-tmp-stream)
323     # . . push args
324     68/push  _test-tmp-stream/imm32
325     68/push  _test-stream/imm32
326     # . . call
327     e8/call  read-line/disp32
328     # . . discard args
329     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
330     # check-next-stream-line-equal(_test-tmp-stream, "ab", msg)
331     # . . push args
332     68/push  "F - test-read-line"/imm32
333     68/push  "ab"/imm32
334     68/push  _test-tmp-stream/imm32
335     # . . call
336     e8/call  check-next-stream-line-equal/disp32
337     # . . discard args
338     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
339     # end
340     c3/return
341 
342 test-read-line-reads-final-line-until-Eof:
343     # setup
344     # . clear-stream(_test-stream)
345     # . . push args
346     68/push  _test-stream/imm32
347     # . . call
348     e8/call  clear-stream/disp32
349     # . . discard args
350     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
351     # . clear-stream(_test-tmp-stream)
352     # . . push args
353     68/push  _test-tmp-stream/imm32
354     # . . call
355     e8/call  clear-stream/disp32
356     # . . discard args
357     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
358     # write(_test-stream, "cd")
359     # . . push args
360     68/push  "cd"/imm32
361     68/push  _test-stream/imm32
362     # . . call
363     e8/call  write/disp32
364     # . . discard args
365     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
366     # read a line from _test-stream into _test-tmp-stream
367     # . eax = read-line(_test-stream, _test-tmp-stream)
368     # . . push args
369     68/push  _test-tmp-stream/imm32
370     68/push  _test-stream/imm32
371     # . . call
372     e8/call  read-line/disp32
373     # . . discard args
374     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
375     # check-stream-equal(_test-tmp-stream, "cd", msg)
376     # . . push args
377     68/push  "F - test-read-line-reads-final-line-until-Eof"/imm32
378     68/push  "cd"/imm32
379     68/push  _test-tmp-stream/imm32
380     # . . call
381     e8/call  check-stream-equal/disp32
382     # . . discard args
383     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
384     # end
385     c3/return
386 
387 # . . vim:nowrap:textwidth=0