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