https://github.com/akkartik/mu/blob/master/subx/057write.subx
  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                                                   register    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/exit
 28     cd/syscall  0x80/imm8
 29 
 30 # TODO: come up with a way to signal when a write to disk fails
 31 write:  # f : fd or (address stream), s : (address array byte) -> <void>
 32     # . prolog
 33     55/push-EBP
 34     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 35     # if (f < 0x08000000) _write(f, s) and return  # f can't be a user-mode address, so treat it as a kernel file descriptor
 36     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         0x08000000/imm32  # compare *(EBP+8)
 37     7d/jump-if-greater-or-equal  $write:fake/disp8
 38     # . . push args
 39     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
 40     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 41     # . . call
 42     e8/call  _write/disp32
 43     # . . discard args
 44     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 45     eb/jump  $write:end/disp8
 46 $write:fake:
 47     # otherwise, treat 'f' as a stream to append to
 48     # . save registers
 49     50/push-EAX
 50     51/push-ECX
 51     52/push-EDX
 52     53/push-EBX
 53     # ECX = f
 54     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 55     # EDX = f->write
 56     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy *ECX to EDX
 57     # EBX = f->length
 58     8b/copy                         1/mod/*+disp8   1/rm32/ECX    .           .             .           3/r32/EBX   8/disp8         .                 # copy *(ECX+8) to EBX
 59     # EAX = _append-3(&f->data[f->write], &f->data[f->length], s)
 60     # . . push s
 61     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
 62     # . . push &f->data[f->length]
 63     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
 64     53/push-EBX
 65     # . . push &f->data[f->write]
 66     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
 67     53/push-EBX
 68     # . . call
 69     e8/call  _append-3/disp32
 70     # . . discard args
 71     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 72     # f->write += EAX
 73     01/add                          0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # add EAX to *ECX
 74     # . restore registers
 75     5b/pop-to-EBX
 76     5a/pop-to-EDX
 77     59/pop-to-ECX
 78     58/pop-to-EAX
 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 test-write-single:
 86     # clear-stream(_test-stream)
 87     # . . push args
 88     68/push  _test-stream/imm32
 89     # . . call
 90     e8/call  clear-stream/disp32
 91     # . . discard args
 92     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 93     # write(_test-stream, "Ab")
 94     # . . push args
 95     68/push  "Ab"/imm32
 96     68/push  _test-stream/imm32
 97     # . . call
 98     e8/call  write/disp32
 99     # . . discard args
100     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
101     # check-ints-equal(*_test-stream->data, 41/A 62/b 00 00, msg)
102     # . . push args
103     68/push  "F - test-write-single"/imm32
104     68/push  0x006241/imm32/Ab
105     # . . push *_test-stream->data
106     b8/copy-to-EAX  _test-stream/imm32
107     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
108     # . . call
109     e8/call  check-ints-equal/disp32
110     # . . discard args
111     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
112     # end
113     c3/return
114 
115 test-write-appends:
116     # clear-stream(_test-stream)
117     # . . push args
118     68/push  _test-stream/imm32
119     # . . call
120     e8/call  clear-stream/disp32
121     # . . discard args
122     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
123     # write(_test-stream, "C")
124     # . . push args
125     68/push  "C"/imm32
126     68/push  _test-stream/imm32
127     # . . call
128     e8/call  write/disp32
129     # . . discard args
130     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
131     # write(_test-stream, "D")
132     # . . push args
133     68/push  "D"/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(*_test-stream->data, 43/C 44/D 00 00, msg)
140     # . . push args
141     68/push  "F - test-write-appends"/imm32
142     68/push  0x00004443/imm32/C-D
143     # . . push *_test-stream->data
144     b8/copy-to-EAX  _test-stream/imm32
145     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
146     # . . call
147     e8/call  check-ints-equal/disp32
148     # . . discard args
149     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
150     # end
151     c3/return
152 
153 == data
154 
155 _test-stream:
156     # current write index
157     00 00 00 00
158     # current read index
159     00 00 00 00
160     # length (= 8)
161     08 00 00 00
162     # data
163     00 00 00 00 00 00 00 00  # 8 bytes
164 
165 # . . vim:nowrap:textwidth=0