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 #? 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     7d/jump-if-greater-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     # . restore registers
106     5f/pop-to-EDI
107     5e/pop-to-ESI
108     5b/pop-to-EBX
109     5a/pop-to-EDX
110     59/pop-to-ECX
111     58/pop-to-EAX
112     # . epilog
113     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
114     5d/pop-to-EBP
115     c3/return
116 
117 test-write-stream-single:
118     # setup
119     # . clear-stream(_test-stream)
120     # . . push args
121     68/push  _test-stream/imm32
122     # . . call
123     e8/call  clear-stream/disp32
124     # . . discard args
125     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
126     # . clear-stream(_test-stream2)
127     # . . push args
128     68/push  _test-stream2/imm32
129     # . . call
130     e8/call  clear-stream/disp32
131     # . . discard args
132     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
133     # . write(_test-stream2, "Ab")
134     # . . push args
135     68/push  "Ab"/imm32
136     68/push  _test-stream2/imm32
137     # . . call
138     e8/call  write/disp32
139     # . . discard args
140     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
141     # write-stream(_test-stream, _test-stream2)
142     # . . push args
143     68/push  _test-stream2/imm32
144     68/push  _test-stream/imm32
145     # . . call
146     e8/call  write-stream/disp32
147     # . . discard args
148     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
149     # check-stream-equal(_test-stream, "Ab", msg)
150     # . . push args
151     68/push  "F - test-write-stream-single"/imm32
152     68/push  "Ab"/imm32
153     68/push  _test-stream/imm32
154     # . . call
155     e8/call  check-stream-equal/disp32
156     # . . discard args
157     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
158     # . end
159     c3/return
160 
161 test-write-stream-appends:
162     # setup
163     # . clear-stream(_test-stream)
164     # . . push args
165     68/push  _test-stream/imm32
166     # . . call
167     e8/call  clear-stream/disp32
168     # . . discard args
169     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
170     # . clear-stream(_test-stream2)
171     # . . push args
172     68/push  _test-stream2/imm32
173     # . . call
174     e8/call  clear-stream/disp32
175     # . . discard args
176     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
177     # . write(_test-stream2, "C")
178     # . . push args
179     68/push  "C"/imm32
180     68/push  _test-stream2/imm32
181     # . . call
182     e8/call  write/disp32
183     # . . discard args
184     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
185     # first write
186     # . write-stream(_test-stream, _test-stream2)
187     # . . push args
188     68/push  _test-stream2/imm32
189     68/push  _test-stream/imm32
190     # . . call
191     e8/call  write-stream/disp32
192     # . . discard args
193     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
194     # second write
195     # . write(_test-stream2, "D")
196     # . . push args
197     68/push  "D"/imm32
198     68/push  _test-stream2/imm32
199     # . . call
200     e8/call  write/disp32
201     # . . discard args
202     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
203     # . write-stream(_test-stream, _test-stream2)
204     # . . push args
205     68/push  _test-stream2/imm32
206     68/push  _test-stream/imm32
207     # . . call
208     e8/call  write-stream/disp32
209     # . . discard args
210     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
211     # check-stream-equal(_test-stream, "CD", msg)
212     # . . push args
213     68/push  "F - test-write-stream-appends"/imm32
214     68/push  "CD"/imm32
215     68/push  _test-stream/imm32
216     # . . call
217     e8/call  check-stream-equal/disp32
218     # . . discard args
219     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
220     # . end
221     c3/return
222 
223 == data
224 
225 _test-stream2:
226     # current write index
227     4/imm32
228     # current read index
229     1/imm32
230     # length
231     8/imm32
232     # data
233     41/A 42/B 43/C 44/D 00 00 00 00  # 8 bytes
234 
235 # . . vim:nowrap:textwidth=0