1 # write: like _write, but also support in-memory streams in addition to file
  2 # descriptors.
  3 #
  4 # Our first dependency-injected and testable primitive. We can pass it either
  5 # a file descriptor or an address to a stream. If a file descriptor is passed
  6 # in, we _write to it using the right syscall. If a 'fake file descriptor' or
  7 # stream is passed in, we append to the stream. This lets us redirect output
  8 # in tests and check it later.
  9 #
 10 # We assume our data segment will never begin at an address shorter than
 11 # 0x08000000, so any smaller arguments are assumed to be real file descriptors.
 12 #
 13 # A stream looks like this:
 14 #   read: int  # index at which to read next
 15 #   write: int  # index at which writes go
 16 #   data: (array byte)  # prefixed by length as usual
 17 
 18 == code
 19 # instruction                     effective address                                                   operand     displacement    immediate
 20 # op          subop               mod             rm32          base        index         scale       r32
 21 # 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
 22 
 23 # main:
 24   e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 25   # syscall(exit, Num-test-failures)
 26   8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 27   b8/copy-to-EAX  1/imm32
 28   cd/syscall  0x80/imm8
 29 
 30 write:  # f : fd or (address stream), s : (address array byte) -> bytes_written/EAX
 31     # (If we ever leave the Linux kernel behind, it may be better to return
 32     # the number of bytes *not* written. Success would then be signaled by
 33     # returning 0.)
 34   # prolog
 35   55/push-EBP
 36   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 37   # if (f < 0x08000000) _write(f, s), return  # f can't be a user-mode address, so treat it as a kernel file descriptor
 38   81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         0x08000000/imm32  # compare *(EBP+8)
 39   7d/jump-if-greater-or-equal  $write:fake/disp8
 40     # push args
 41   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
 42   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         .                 # push *(EBP+8)
 43     # call
 44   e8/call  _write/disp32
 45     # discard args
 46   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 47   eb/jump  $write:end/disp8
 48 $write:fake:
 49   # otherwise, treat 'f' as a stream to append to
 50   # save registers
 51   51/push-ECX
 52   52/push-EDX
 53   53/push-EBX
 54   # ECX = f
 55   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 56   # EDX = f->write
 57   8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy *ECX to EDX
 58   # EBX = f->length
 59   8b/copy                         1/mod/*+disp8   1/rm32/ECX    .           .             .           3/r32/EBX   8/disp8         .                 # copy *(ECX+8) to EBX
 60   # EAX = _append-3(&f->data[f->write], &f->data[f->length], s)
 61     # push s
 62   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
 63     # push &f->data[f->length]
 64   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    1/base/ECX  3/index/EBX   .           3/r32/EBX   0xc/disp8       .                 # copy ECX+EBX+12 to EBX
 65   53/push-EBX
 66     # push &f->data[f->write]
 67   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    1/base/ECX  2/index/EDX   .           3/r32/EBX   0xc/disp8       .                 # copy ECX+EDX+12 to EBX
 68   53/push-EBX
 69     # call
 70   e8/call  _append-3/disp32
 71     # discard args
 72   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 73   # f->write += EAX
 74   01/add                          0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # add EAX to *ECX
 75   # restore registers
 76   5b/pop-to-EBX
 77   5a/pop-to-EDX
 78   59/pop-to-ECX
 79 $write:end:
 80   # epilog
 81   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 82   5d/pop-to-EBP
 83   c3/return
 84 
 85 clear-stream:  # f : (address stream) -> <void>
 86   # prolog
 87   55/push-EBP
 88   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 89   # save registers
 90   50/push-EAX
 91   51/push-ECX
 92   # EAX = f
 93   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
 94   # ECX = f->length
 95   8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
 96   # ECX = &f->data[f->length]
 97   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  1/index/ECX   .           1/r32/ECX   0xc/disp8       .                 # copy EAX+ECX+12 to ECX
 98   # f->write = 0
 99   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
100   # f->read = 0
101   c7/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
102   # EAX = f->data
103   81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xc/imm32         # add to EAX
104   # while (true)
105 $clear-stream:loop:
106   # if EAX >= ECX break
107   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
108   7d/jump-if-greater-or-equal  $clear-stream:end/disp8
109   # *EAX = 0
110   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
111   # EAX += 4
112   81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm32           # add to EAX
113   eb/jump  $clear-stream:loop/disp8
114 $clear-stream:end:
115   # restore registers
116   59/pop-to-ECX
117   58/pop-to-EAX
118   # epilog
119   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
120   5d/pop-to-EBP
121   c3/return
122 
123 test-write-single:
124   # clear-stream(_test-stream)
125     # push args
126   68/push  _test-stream/imm32
127     # call
128   e8/call  clear-stream/disp32
129     # discard args
130   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
131   # write(_test-stream, "Ab")
132     # push args
133   68/push  "Ab"/imm32
134   68/push  _test-stream/imm32
135     # call
136   e8/call  write/disp32
137     # discard args
138   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
139   # check-ints-equal(EAX, 2)
140     # push args
141   68/push  "F - test-read-single: return EAX"/imm32
142   68/push  2/imm32
143   50/push-EAX
144     # call
145   e8/call  check-ints-equal/disp32
146     # discard args
147   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
148   # check-ints-equal(*_test-stream->data, 41/A 62/b 00 00, msg)
149     # push args
150   68/push  "F - test-write-single"/imm32
151   68/push  0x006241/imm32/Ab
152     # push *_test-stream->data
153   b8/copy-to-EAX  _test-stream/imm32
154   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
155     # call
156   e8/call  check-ints-equal/disp32
157     # discard args
158   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
159   # end
160   c3/return
161 
162 test-write-appends:
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   # write(_test-stream, "C")
171     # push args
172   68/push  "C"/imm32
173   68/push  _test-stream/imm32
174     # call
175   e8/call  write/disp32
176     # discard args
177   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
178   # write(_test-stream, "D")
179     # push args
180   68/push  "D"/imm32
181   68/push  _test-stream/imm32
182     # call
183   e8/call  write/disp32
184     # discard args
185   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
186   # check-ints-equal(*_test-stream->data, 43/C 44/D 00 00, msg)
187     # push args
188   68/push  "F - test-write-appends"/imm32
189   68/push  0x00004443/imm32/C-D
190     # push *_test-stream->data
191   b8/copy-to-EAX  _test-stream/imm32
192   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
193     # call
194   e8/call  check-ints-equal/disp32
195     # discard args
196   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
197   # end
198   c3/return
199 
200 == data
201 
202 _test-stream:
203   # current write index
204   00 00 00 00
205   # current read index
206   00 00 00 00
207   # length (= 8)
208   08 00 00 00
209   # data
210   00 00 00 00 00 00 00 00  # 8 bytes
211 
212 # vim:nowrap:textwidth=0