https://github.com/akkartik/mu/blob/master/subx/067write-buffered.subx
  1 # write-buffered: like 'write', but for a buffered-file
  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 #?     e8/call test-write-buffered/disp32
 10 #?     e8/call test-write-buffered-with-intermediate-flush/disp32
 11     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 12     # syscall(exit, Num-test-failures)
 13     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 14     b8/copy-to-EAX  1/imm32/exit
 15     cd/syscall  0x80/imm8
 16 
 17 write-buffered:  # f : (address buffered-file), msg : (address array byte) -> <void>
 18     # pseudocode:
 19     #   in = msg->data
 20     #   inend = &msg->data[msg->length]
 21     #   while (in < inend)
 22     #     if (f->write >= f->length)
 23     #       flush(f)
 24     #       clear-stream(f)
 25     #     c = *in
 26     #     f->data[f->write] = c
 27     #     ++f->write
 28     #     ++in
 29     #
 30     # registers:
 31     #   in: ESI
 32     #   inend: ECX
 33     #   f: EDI
 34     #   f->length: EDX
 35     #   f->write: EBX (cached; need to keep in sync)
 36     #   c: EAX
 37     #
 38     # . prolog
 39     55/push-EBP
 40     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 41     # . save registers
 42     50/push-EAX
 43     51/push-ECX
 44     52/push-EDX
 45     53/push-EBX
 46     56/push-ESI
 47     57/push-EDI
 48     # EAX = msg
 49     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
 50     # in/ESI = msg->data
 51     8d/copy-address                 1/mod/*+disp8   0/rm32/EAX    .           .             .           6/r32/ESI   4/disp8         .                 # copy EAX+4 to ESI
 52     # inend/ECX = &msg->data[msg->length]
 53     8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # copy *EAX to ECX
 54     8d/copy-address                 0/mod/indirect  4/rm32/sib    6/base/ESI  1/index/ECX   .           1/r32/ECX   .               .                 # copy ESI+ECX to ECX
 55     # EDI = f
 56     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
 57     # EDX = f->length
 58     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   0xc/disp8       .                 # copy *(EDI+12) to EDX
 59     # EBX = f->write
 60     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           3/r32/EBX   4/disp8         .                 # copy *(EDI+4) to EBX
 61 $write-buffered:loop:
 62     # if (in >= inend) break
 63     39/compare                      3/mod/direct    6/rm32/ESI    .           .             .           1/r32/ECX   .               .                 # compare ESI with ECX
 64     7d/jump-if-greater-or-equal  $write-buffered:loop-end/disp8
 65     # if (f->write >= f->length) flush and clear f's stream
 66     39/compare                      3/mod/direct    3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # compare EBX with EDX
 67     7c/jump-if-lesser  $write-buffered:to-stream/disp8
 68     # . persist f->write
 69     89/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           3/r32/EBX   4/disp8         .                 # copy EBX to *(EDI+4)
 70     # . flush(f)
 71     # . . push args
 72     57/push-EDI
 73     # . . call
 74     e8/call  flush/disp32
 75     # . . discard args
 76     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 77     # . clear-stream(stream = f+4)
 78     # . . push args
 79     8d/copy-address                 1/mod/*+disp8   7/rm32/EDI    .           .             .           0/r32/EAX   4/disp8         .                 # copy EDI+4 to EAX
 80     50/push-EAX
 81     # . . call
 82     e8/call  clear-stream/disp32
 83     # . . discard args
 84     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 85     # . f->write must now be 0; update its cache at EBX
 86     31/xor                          3/mod/direct    3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # clear EBX
 87 $write-buffered:to-stream:
 88     # write to stream
 89     # f->data[f->write] = *in
 90     # . AL = *in
 91     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 92     8a/copy-byte                    0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/AL    .               .                 # copy byte at *ESI to AL
 93     # . f->data[f->write] = AL
 94     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  3/index/EBX   .           0/r32/AL    0x10/disp8      .                 # copy AL to *(EDI+EBX+16)
 95     # ++f->write
 96     43/increment-EBX
 97     # ++in
 98     46/increment-ESI
 99     eb/jump  $write-buffered:loop/disp8
100 $write-buffered:loop-end:
101     # persist necessary variables from registers
102     89/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           3/r32/EBX   4/disp8         .                 # copy EBX to *(EDI+4)
103 $write-buffered:end:
104     # . restore registers
105     5f/pop-to-EDI
106     5e/pop-to-ESI
107     5b/pop-to-EBX
108     5a/pop-to-EDX
109     59/pop-to-ECX
110     58/pop-to-EAX
111     # . epilog
112     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
113     5d/pop-to-EBP
114     c3/return
115 
116 test-write-buffered:
117     # - check that write-buffered writes to the file
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-buffered-file+4)
127     # . . push args
128     b8/copy-to-EAX  _test-buffered-file/imm32
129     05/add-to-EAX  4/imm32
130     50/push-EAX
131     # . . call
132     e8/call  clear-stream/disp32
133     # . . discard args
134     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
135     # write-buffered(_test-buffered-file, "Abc")
136     # . . push args
137     68/push  "Abc"/imm32
138     68/push  _test-buffered-file/imm32
139     # . . call
140     e8/call  write-buffered/disp32
141     # . . discard args
142     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
143     # flush(_test-buffered-file)
144     # . . push args
145     68/push  _test-buffered-file/imm32
146     # . . call
147     e8/call  flush/disp32
148     # . . discard args
149     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
150     # check-stream-equal(_test-stream, "Abc", msg)
151     # . . push args
152     68/push  "F - test-write-buffered-single"/imm32
153     68/push  "Abc"/imm32
154     68/push  _test-stream/imm32
155     # . . call
156     e8/call  check-stream-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-buffered-with-intermediate-flush:
163     # - check that write-buffered flushes in the middle if its buffer fills up
164     # setup
165     # . clear-stream(_test-stream)
166     # . . push args
167     68/push  _test-stream/imm32
168     # . . call
169     e8/call  clear-stream/disp32
170     # . . discard args
171     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
172     # . clear-stream(_test-buffered-file+4)
173     # . . push args
174     b8/copy-to-EAX  _test-buffered-file/imm32
175     05/add-to-EAX  4/imm32
176     50/push-EAX
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     # _test-stream can hold 8 bytes, but _test-buffered-file can hold only 6.
182     # Try to write 7 bytes.
183     # . write-buffered(_test-buffered-file, "Abcdefg")
184     # . . push args
185     68/push  "Abcdefg"/imm32
186     68/push  _test-buffered-file/imm32
187     # . . call
188     e8/call  write-buffered/disp32
189     # . . discard args
190     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
191     # don't flush
192     # 6 bytes should still have gotten to _test-stream
193     # . check-ints-equal(*_test-stream->write, 6, msg)
194     # . . push args
195     68/push  "F - test-write-buffered-with-intermediate-flush: flushed data"/imm32
196     68/push  6/imm32
197     # . . push *_test-stream->write
198     b8/copy-to-EAX  _test-stream/imm32
199     ff          6/subop/push        0/mod/indirect  0/rm32/EAX    .           .             .           .           .               .                 # push *EAX
200     # . . call
201     e8/call  check-ints-equal/disp32
202     # . . discard args
203     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
204     # and 1 byte should still be in _test-buffered-file
205     # . check-ints-equal(*_test-buffered-file->write, 1, msg)
206     # . . push args
207     68/push  "F - test-write-buffered-with-intermediate-flush: unflushed bytes"/imm32
208     68/push  1/imm32
209     # . . push *_test-buffered-file->write
210     b8/copy-to-EAX  _test-buffered-file/imm32
211     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
212     # . . call
213     e8/call  check-ints-equal/disp32
214     # . . discard args
215     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
216     # . end
217     c3/return
218 
219 # . . vim:nowrap:textwidth=0