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