https://github.com/akkartik/mu/blob/master/subx/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 # . . vim:nowrap:textwidth=0