https://github.com/akkartik/mu/blob/master/062write-stream.subx
  1 # write-stream: like write, but write streams rather than strings
  2 
  3 == code
  4 #   instruction                     effective address                                                   register    displacement    immediate
  5 # . op          subop               mod             rm32          base        index         scale       r32
  6 # . 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
  7 
  8 #? Entry:  # manual test
  9 #?     # write-stream(stdout, _test-stream2)
 10 #?     68/push  _test-stream2/imm32
 11 #?     68/push  1/imm32/stdout
 12 #?     e8/call write-stream/disp32
 13 #?     # syscall(exit, Num-test-failures)
 14 #?     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 15 #?     b8/copy-to-EAX  1/imm32/exit
 16 #?     cd/syscall  0x80/imm8
 17 
 18 write-stream:  # f : fd or (address stream), s : (address stream) -> <void>
 19     # . prolog
 20     55/push-EBP
 21     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 22     # if (f < 0x08000000) _write-stream(f, s), return  # f can't be a user-mode address, so treat it as a kernel file descriptor
 23     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         0x08000000/imm32  # compare *(EBP+8)
 24     73/jump-if-greater-unsigned-or-equal  $write-stream:fake/disp8
 25     # . . push args
 26     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
 27     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 28     # . . call
 29     e8/call  _write-stream/disp32
 30     # . . discard args
 31     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 32     eb/jump  $write-stream:end/disp8
 33 $write-stream:fake:
 34     # otherwise, treat 'f' as a stream to append to
 35     # . save registers
 36     50/push-EAX
 37     56/push-ESI
 38     57/push-EDI
 39     # EDI = f
 40     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
 41     # ESI = s
 42     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
 43     # EAX = _append-4(&f->data[f->write], &f->data[f->length], &s->data[s->read], &s->data[s->write])
 44     # . . push &s->data[s->write]
 45     8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy *ESI to EAX
 46     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/ESI  0/index/EAX   .           0/r32/EAX   0xc/disp8       .                 # copy ESI+EAX+12 to EAX
 47     50/push-EAX
 48     # . . push &s->data[s->read]
 49     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # copy *(ESI+4) to EAX
 50     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/ESI  0/index/EAX   .           0/r32/EAX   0xc/disp8       .                 # copy ESI+EAX+12 to EAX
 51     50/push-EAX
 52     # . . push &f->data[f->length]
 53     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EDI+8) to EAX
 54     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  0/index/EAX   .           0/r32/EAX   0xc/disp8       .                 # copy EDI+EAX+12 to EAX
 55     50/push-EAX
 56     # . . push &f->data[f->write]
 57     8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # copy *EDI to EAX
 58     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  0/index/EAX   .           0/r32/EAX   0xc/disp8       .                 # copy EDI+EAX+12 to EAX
 59     50/push-EAX
 60     # . . call
 61     e8/call  _append-4/disp32
 62     # . . discard args
 63     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
 64     # f->write += EAX
 65     01/add                          0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # add EAX to *EDI
 66     # s->read += EAX
 67     01/add                          1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # add EAX to *(ESI+4)
 68     # . restore registers
 69     5f/pop-to-EDI
 70     5e/pop-to-ESI
 71     58/pop-to-EAX
 72 $write-stream:end:
 73     # . epilog
 74     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 75     5d/pop-to-EBP
 76     c3/return
 77 
 78 _write-stream:  # fd : int, s : (address stream) -> <void>
 79     # . prolog
 80     55/push-EBP
 81     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 82     # . save registers
 83     50/push-EAX
 84     51/push-ECX
 85     52/push-EDX
 86     53/push-EBX
 87     56/push-ESI
 88     57/push-EDI
 89     # ESI = s
 90     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
 91     # EDI = s->read
 92     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           7/r32/EDI   4/disp8         .                 # copy *(ESI+4) to EDI
 93     # EDX = s->write
 94     8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           2/r32/EDX   .               .                 # copy *ESI to EDX
 95     # syscall(write, fd, &s->data[s->read], s->write - s->read)
 96     # . . fd : EBX
 97     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   8/disp8         .                 # copy *(EBP+8) to EBX
 98     # . . data : ECX = &s->data[s->read]
 99     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/ESI  7/index/EDI   .           1/r32/ECX   0xc/disp8       .                 # copy ESI+EDI+12 to ECX
100     # . . size : EDX = s->write - s->read
101     29/subtract                     3/mod/direct    2/rm32/EDX    .           .             .           7/r32/EDI   .               .                 # subtract EDI from EDX
102     # . . syscall
103     b8/copy-to-EAX  4/imm32/write
104     cd/syscall  0x80/imm8
105     # if (EAX < 0) abort
106     3d/compare-EAX-with  0/imm32
107     0f 8c/jump-if-lesser  $_write-stream:abort/disp32
108     # s->read += EAX
109     01/add                          1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # add EAX to *(ESI+4)
110     # . restore registers
111     5f/pop-to-EDI
112     5e/pop-to-ESI
113     5b/pop-to-EBX
114     5a/pop-to-EDX
115     59/pop-to-ECX
116     58/pop-to-EAX
117     # . epilog
118     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
119     5d/pop-to-EBP
120     c3/return
121 
122 $_write-stream:abort:
123     # . _write(2/stderr, error)
124     # . . push args
125     68/push  "_write-stream: failed to write to file\n"/imm32
126     68/push  2/imm32/stderr
127     # . . call
128     e8/call  _write/disp32
129     # . . discard args
130     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
131     # . syscall(exit, 1)
132     bb/copy-to-EBX  1/imm32
133     b8/copy-to-EAX  1/imm32/exit
134     cd/syscall  0x80/imm8
135     # never gets here
136 
137 test-write-stream-single:
138     # setup
139     # . clear-stream(_test-stream)
140     # . . push args
141     68/push  _test-stream/imm32
142     # . . call
143     e8/call  clear-stream/disp32
144     # . . discard args
145     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
146     # . clear-stream(_test-stream2)
147     # . . push args
148     68/push  _test-stream2/imm32
149     # . . call
150     e8/call  clear-stream/disp32
151     # . . discard args
152     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
153     # . write(_test-stream2, "Ab")
154     # . . push args
155     68/push  "Ab"/imm32
156     68/push  _test-stream2/imm32
157     # . . call
158     e8/call  write/disp32
159     # . . discard args
160     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
161     # write-stream(_test-stream, _test-stream2)
162     # . . push args
163     68/push  _test-stream2/imm32
164     68/push  _test-stream/imm32
165     # . . call
166     e8/call  write-stream/disp32
167     # . . discard args
168     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
169     # check-stream-equal(_test-stream, "Ab", msg)
170     # . . push args
171     68/push  "F - test-write-stream-single"/imm32
172     68/push  "Ab"/imm32
173     68/push  _test-stream/imm32
174     # . . call
175     e8/call  check-stream-equal/disp32
176     # . . discard args
177     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
178     # . end
179     c3/return
180 
181 test-write-stream-appends:
182     # setup
183     # . clear-stream(_test-stream)
184     # . . push args
185     68/push  _test-stream/imm32
186     # . . call
187     e8/call  clear-stream/disp32
188     # . . discard args
189     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
190     # . clear-stream(_test-stream2)
191     # . . push args
192     68/push  _test-stream2/imm32
193     # . . call
194     e8/call  clear-stream/disp32
195     # . . discard args
196     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
197     # . write(_test-stream2, "C")
198     # . . push args
199     68/push  "C"/imm32
200     68/push  _test-stream2/imm32
201     # . . call
202     e8/call  write/disp32
203     # . . discard args
204     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
205     # first write
206     # . write-stream(_test-stream, _test-stream2)
207     # . . push args
208     68/push  _test-stream2/imm32
209     68/push  _test-stream/imm32
210     # . . call
211     e8/call  write-stream/disp32
212     # . . discard args
213     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
214     # second write
215     # . write(_test-stream2, "D")
216     # . . push args
217     68/push  "D"/imm32
218     68/push  _test-stream2/imm32
219     # . . call
220     e8/call  write/disp32
221     # . . discard args
222     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
223     # . write-stream(_test-stream, _test-stream2)
224     # . . push args
225     68/push  _test-stream2/imm32
226     68/push  _test-stream/imm32
227     # . . call
228     e8/call  write-stream/disp32
229     # . . discard args
230     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
231     # check-stream-equal(_test-stream, "CD", msg)
232     # . . push args
233     68/push  "F - test-write-stream-appends"/imm32
234     68/push  "CD"/imm32
235     68/push  _test-stream/imm32
236     # . . call
237     e8/call  check-stream-equal/disp32
238     # . . discard args
239     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
240     # . end
241     c3/return
242 
243 == data
244 
245 _test-stream2:
246     # current write index
247     4/imm32
248     # current read index
249     1/imm32
250     # length
251     8/imm32
252     # data
253     41/A 42/B 43/C 44/D 00 00 00 00  # 8 bytes
254 
255 # . . vim:nowrap:textwidth=0