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 (addr stream byte), s: (addr stream byte)
 19     # . prologue
 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-addr>=  $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     # . epilogue
 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: (addr stream byte)
 79     # . prologue
 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-<  $_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     # . epilogue
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:  # (stream byte)
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