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 #? Entry:  # run a single test, while debugging
  7 #?     e8/call test-read-line/disp32
  8 #?     # syscall(exit, Num-test-failures)
  9 #?     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 10 #?     b8/copy-to-EAX  1/imm32/exit
 11 #?     cd/syscall  0x80/imm8
 12 
 13 # read bytes from 'f' until (and including) a newline and store them into 's'
 14 # 's' fails to grow if and only if no data found
 15 # just abort if 's' is too small
 16 read-line:  # f : (address buffered-file), s : (address stream byte) -> <void>
 17     # pseudocode:
 18     #   while true
 19     #     if (s->write >= s->length) abort
 20     #     if (f->read >= f->write) populate stream from file
 21     #     if (f->write == 0) break
 22     #     AL = f->data[f->read]
 23     #     s->data[s->write] = AL
 24     #     ++f->read
 25     #     ++s->write
 26     #     if (AL == '\n') break
 27     # . prolog
 28     55/push-EBP
 29     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 30     # . save registers
 31     50/push-EAX
 32     51/push-ECX
 33     52/push-EDX
 34     56/push-ESI
 35     57/push-EDI
 36     # ESI = f
 37     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
 38     # ECX = f->read
 39     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(ESI+8) to ECX
 40     # EDI = s
 41     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   0xc/disp8       .                 # copy *(EBP+12) to EDI
 42     # EDX = s->write
 43     8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # copy *EDI to EDX
 44 $read-line:loop:
 45     # if (s->write >= s->length) abort
 46     3b/compare                      1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   8/disp8         .                 # compare EDX with *(EDI+8)
 47     7d/jump-if-greater-or-equal  $read-line:abort/disp8
 48     # if (f->read >= f->write) populate stream from file
 49     3b/compare                      1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   4/disp8         .                 # compare ECX with *(ESI+4)
 50     7c/jump-if-lesser  $read-line:from-stream/disp8
 51     # . clear-stream(stream = f+4)
 52     # . . push args
 53     8d/copy-address                 1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # copy ESI+4 to EAX
 54     50/push-EAX
 55     # . . call
 56     e8/call  clear-stream/disp32
 57     # . . discard args
 58     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 59     # . f->read must now be 0; update its cache at ECX
 60     31/xor                          3/mod/direct    1/rm32/ECX    .           .             .           1/r32/ECX   .               .                 # clear ECX
 61     # . EAX = read(f->fd, stream = f+4)
 62     # . . push args
 63     50/push-EAX
 64     ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
 65     # . . call
 66     e8/call  read/disp32
 67     # . . discard args
 68     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 69     # if (f->write == 0) break
 70     # since f->read was initially 0, EAX is the same as f->write
 71     # . if (EAX == 0) return true
 72     3d/compare-EAX-and  0/imm32
 73     74/jump-if-equal  $read-line:end/disp8
 74 $read-line:from-stream:
 75     # AL = f->data[f->read]
 76     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 77     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
 78     # s->data[s->write] = AL
 79     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)
 80     # ++f->read
 81     41/increment-ECX
 82     # ++s->write
 83     42/increment-EDX
 84     # if (AL == '\n') return
 85     3d/compare-EAX-and  0xa/imm32
 86     75/jump-if-not-equal  $read-line:loop/disp8
 87 $read-line:end:
 88     # save f->read
 89     89/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   8/disp8         .                 # copy ECX to *(ESI+8)
 90     # save s->write
 91     89/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # copy EDX to *EDI
 92     # . restore registers
 93     5f/pop-to-EDI
 94     5e/pop-to-ESI
 95     5a/pop-to-EDX
 96     59/pop-to-ECX
 97     58/pop-to-EAX
 98     # . epilog
 99     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
100     5d/pop-to-EBP
101     c3/return
102 
103 $read-line:abort:
104     # . _write(2/stderr, error)
105     # . . push args
106     68/push  "read-line: line too long\n"/imm32
107     68/push  2/imm32/stderr
108     # . . call
109     e8/call  _write/disp32
110     # . . discard args
111     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
112     # . syscall(exit, 1)
113     bb/copy-to-EBX  1/imm32
114     b8/copy-to-EAX  1/imm32/exit
115     cd/syscall  0x80/imm8
116     # never gets here
117 
118 test-read-line:
119     # - check that read-line stops at a newline
120     # setup
121     # . clear-stream(_test-stream)
122     # . . push args
123     68/push  _test-stream/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-buffered-file+4)
129     # . . push args
130     b8/copy-to-EAX  _test-buffered-file/imm32
131     05/add-to-EAX  4/imm32
132     50/push-EAX
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     # . clear-stream(_test-tmp-stream)
138     # . . push args
139     68/push  _test-tmp-stream/imm32
140     # . . call
141     e8/call  clear-stream/disp32
142     # . . discard args
143     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
144     # write(_test-stream, "ab\ncd")
145     # . . push args
146     68/push  "ab\ncd"/imm32
147     68/push  _test-stream/imm32
148     # . . call
149     e8/call  write/disp32
150     # . . discard args
151     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
152     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
153     # . EAX = read-line(_test-buffered-file, _test-tmp-stream)
154     # . . push args
155     68/push  _test-tmp-stream/imm32
156     68/push  _test-buffered-file/imm32
157     # . . call
158     e8/call  read-line/disp32
159     # . . discard args
160     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
161     # check-next-stream-line-equal(_test-tmp-stream, "ab", msg)
162     # . . push args
163     68/push  "F - test-read-line"/imm32
164     68/push  "ab"/imm32
165     68/push  _test-tmp-stream/imm32
166     # . . call
167     e8/call  check-next-stream-line-equal/disp32
168     # . . discard args
169     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
170     # end
171     c3/return
172 
173 test-read-line-reads-final-line-until-Eof:
174     # setup
175     # . clear-stream(_test-stream)
176     # . . push args
177     68/push  _test-stream/imm32
178     # . . call
179     e8/call  clear-stream/disp32
180     # . . discard args
181     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
182     # . clear-stream(_test-buffered-file+4)
183     # . . push args
184     b8/copy-to-EAX  _test-buffered-file/imm32
185     05/add-to-EAX  4/imm32
186     50/push-EAX
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     # . clear-stream(_test-tmp-stream)
192     # . . push args
193     68/push  _test-tmp-stream/imm32
194     # . . call
195     e8/call  clear-stream/disp32
196     # . . discard args
197     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
198     # write(_test-stream, "cd")
199     # . . push args
200     68/push  "cd"/imm32
201     68/push  _test-stream/imm32
202     # . . call
203     e8/call  write/disp32
204     # . . discard args
205     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
206     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
207     # . EAX = read-line(_test-buffered-file, _test-tmp-stream)
208     # . . push args
209     68/push  _test-tmp-stream/imm32
210     68/push  _test-buffered-file/imm32
211     # . . call
212     e8/call  read-line/disp32
213     # . . discard args
214     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
215     # check-stream-equal(_test-tmp-stream, "cd", msg)
216     # . . push args
217     68/push  "F - test-read-line-reads-final-line-until-Eof"/imm32
218     68/push  "cd"/imm32
219     68/push  _test-tmp-stream/imm32
220     # . . call
221     e8/call  check-stream-equal/disp32
222     # . . discard args
223     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
224     # end
225     c3/return
226 
227 # . . vim:nowrap:textwidth=0