1 # write-stream: like write, but write streams rather than strings
  2 
  3 == code
  4 # instruction                     effective address                                                   operand     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
 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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
 31   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   # ESI = f
 44   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
 45   # EDI = s
 46   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              7/r32/EDI   0xc/disp8       .                 # copy *(EBP+12) to EDI
 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  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # copy *EDI to EAX
 50   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
 51   50/push-EAX
 52     # push &s->data[s->read]
 53   8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           0/r32/EAX   4/disp8         .                 # copy *(EDI+4) 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->length]
 57   8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(ESI+8) to EAX
 58   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
 59   50/push-EAX
 60     # push &f->data[f->write]
 61   8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy *ESI to EAX
 62   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
 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  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # add EAX to *ESI
 70   # s->read += EAX
 71   01/add                          1/mod/*+disp8   7/rm32/EDI    .           .             .           0/r32/EAX   4/disp8         .                 # add EAX to *(EDI+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   4/rm32/sib    5/base/EBP  4/index/none  .           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   4/rm32/sib    5/base/EBP  4/index/none  .           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   # clear-stream(_test-stream)
123     # push args
124   68/push  _test-stream/imm32
125     # call
126   e8/call  clear-stream/disp32
127     # discard args
128   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
129   # clear-stream(_test-stream2)
130     # push args
131   68/push  _test-stream2/imm32
132     # call
133   e8/call  clear-stream/disp32
134     # discard args
135   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
136   # write(_test-stream2, "Ab")
137     # push args
138   68/push  "Ab"/imm32
139   68/push  _test-stream2/imm32
140     # call
141   e8/call  write/disp32
142     # discard args
143   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
144   # write-stream(_test-stream, _test-stream2)
145     # push args
146   68/push  _test-stream2/imm32
147   68/push  _test-stream/imm32
148     # call
149   e8/call  write-stream/disp32
150     # discard args
151   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
152   # check-ints-equal(*_test-stream.data, 41/A 62/b 00 00, msg)
153     # push args
154   68/push  "F - test-write-stream-single"/imm32
155   68/push  0x006241/imm32/Ab
156     # push *_test-stream.data
157   b8/copy-to-EAX  _test-stream/imm32
158   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
159     # call
160   e8/call  check-ints-equal/disp32
161     # discard args
162   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
163   # end
164   c3/return
165 
166 test-write-stream-appends:
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   # write-stream(_test-stream, _test-stream2)
190     # push args
191   68/push  _test-stream2/imm32
192   68/push  _test-stream/imm32
193     # call
194   e8/call  write-stream/disp32
195     # discard args
196   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
197   # write(_test-stream2, "D")
198     # push args
199   68/push  "D"/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   # write-stream(_test-stream, _test-stream2)
206     # push args
207   68/push  _test-stream2/imm32
208   68/push  _test-stream/imm32
209     # call
210   e8/call  write-stream/disp32
211     # discard args
212   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
213   # check-ints-equal(*_test-stream.data, 43/C 44/D 00 00, msg)
214     # push args
215   68/push  "F - test-write-stream-appends"/imm32
216   68/push  0x00004443/imm32/C-D
217     # push *_test-stream.data
218   b8/copy-to-EAX  _test-stream/imm32
219   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
220     # call
221   e8/call  check-ints-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