https://github.com/akkartik/mu/blob/master/subx/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 # 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-single/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-stream-equal(_test-stream, "Ab", msg)
154     # . . push args
155     68/push  "F - test-write-stream-single"/imm32
156     68/push  "Ab"/imm32
157     68/push  _test-stream/imm32
158     # . . call
159     e8/call  check-stream-equal/disp32
160     # . . discard args
161     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
162     # . end
163     c3/return
164 
165 test-write-stream-appends:
166     # setup
167     # . clear-stream(_test-stream)
168     # . . push args
169     68/push  _test-stream/imm32
170     # . . call
171     e8/call  clear-stream/disp32
172     # . . discard args
173     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
174     # . clear-stream(_test-stream2)
175     # . . push args
176     68/push  _test-stream2/imm32
177     # . . call
178     e8/call  clear-stream/disp32
179     # . . discard args
180     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
181     # . write(_test-stream2, "C")
182     # . . push args
183     68/push  "C"/imm32
184     68/push  _test-stream2/imm32
185     # . . call
186     e8/call  write/disp32
187     # . . discard args
188     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
189     # first write
190     # . write-stream(_test-stream, _test-stream2)
191     # . . push args
192     68/push  _test-stream2/imm32
193     68/push  _test-stream/imm32
194     # . . call
195     e8/call  write-stream/disp32
196     # . . discard args
197     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
198     # second write
199     # . write(_test-stream2, "D")
200     # . . push args
201     68/push  "D"/imm32
202     68/push  _test-stream2/imm32
203     # . . call
204     e8/call  write/disp32
205     # . . discard args
206     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
207     # . write-stream(_test-stream, _test-stream2)
208     # . . push args
209     68/push  _test-stream2/imm32
210     68/push  _test-stream/imm32
211     # . . call
212     e8/call  write-stream/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-stream, "CD", msg)
216     # . . push args
217     68/push  "F - test-write-stream-appends"/imm32
218     68/push  "CD"/imm32
219     68/push  _test-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 == data
228 
229 _test-stream2:
230     # current write index
231     04 00 00 00
232     # current read index
233     01 00 00 00
234     # length (= 8)
235     08 00 00 00
236     # data
237     41 42 43 44 00 00 00 00  # 8 bytes
238 
239 # . . vim:nowrap:textwidth=0