https://github.com/akkartik/mu/blob/master/subx/062write-byte.subx
  1 # write-byte: write a single byte to a buffered-file. The write may be buffered.
  2 # flush: write out any buffered writes to disk.
  3 #
  4 # TODO: Come up with a way to signal failure to write to disk. This is hard
  5 # since the failure may impact previous calls that were buffered.
  6 
  7 == data
  8 
  9 # The buffered file for standard output.
 10 
 11 Stdout:
 12     # file descriptor or (address stream)
 13     01 00 00 00  # 1 = standard output
 14     # current write index
 15     00 00 00 00
 16     # current read index
 17     00 00 00 00
 18     # length (8)
 19     08 00 00 00
 20     # data
 21     00 00 00 00 00 00 00 00  # 8 bytes
 22 
 23 # TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But
 24 # I don't want to type 1024 bytes here.
 25 
 26 == code
 27 #   instruction                     effective address                                                   register    displacement    immediate
 28 # . op          subop               mod             rm32          base        index         scale       r32
 29 # . 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
 30 
 31 # main:
 32     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 33     # syscall(exit, Num-test-failures)
 34     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 35     b8/copy-to-EAX  1/imm32
 36     cd/syscall  0x80/imm8
 37 
 38 # Write lower byte of 'n' to 'f'.
 39 write-byte:  # f : (address buffered-file), n : num -> <void>
 40     # . prolog
 41     55/push-EBP
 42     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 43     # . save registers
 44     51/push-ECX
 45     56/push-ESI
 46     # ESI = f
 47     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
 48     # ECX = f->write
 49     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   4/disp8         .                 # copy *(ESI+4) to ECX
 50     # if (f->write >= f->length) flush and clear stream
 51     3b/compare                      1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   0xc/disp8       .                 # compare ECX with *(ESI+12)
 52     7c/jump-if-lesser  $write-byte:to-stream/disp8
 53     # . flush-buffered-file(f)
 54     # . . push args
 55     56/push-ESI
 56     # . . call
 57     e8/call  flush/disp32
 58     # . . discard args
 59     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 60     # . clear-stream(stream = f+4)
 61     # . . push args
 62     8d/copy-address                 1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # copy ESI+4 to EAX
 63     50/push-EAX
 64     # . . call
 65     e8/call  clear-stream/disp32
 66     # . . discard args
 67     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 68 $write-byte:to-stream:
 69     # write to stream
 70     # f->data[f->read] = LSB(n)
 71     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 72     8a/copy-byte                    1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           0/r32/AL    0xc/disp8       .                 # copy byte at *(EBP+12) to AL
 73     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    6/base/ESI  1/index/ECX   .           0/r32/AL    0x10/disp8      .                 # copy AL to *(ESI+ECX+16)
 74     # ++f->read
 75     ff          0/subop/increment   1/mod/*+disp8   6/rm32/ESI    .           .             .           .           4/disp8         .                 # increment *(ESI+4)
 76     # . restore registers
 77     5e/pop-to-ESI
 78     59/pop-to-ECX
 79     # . epilog
 80     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 81     5d/pop-to-EBP
 82     c3/return
 83 
 84 flush:  # f : (address buffered-file) -> <void>
 85     # . prolog
 86     55/push-EBP
 87     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 88     # . save registers
 89     50/push-EAX
 90     51/push-ECX
 91     # EAX = f
 92     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
 93     # write-stream(f->fd, data = f+4)
 94       # . . push args
 95     8d/copy-address                 1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   4/disp8         .                 # copy EAX+4 to ECX
 96     51/push-ECX
 97     ff          6/subop/push        0/mod/indirect  0/rm32/EAX    .           .             .           .           .               .                 # push *EAX
 98       # . . call
 99     e8/call  write-stream/disp32
100       # . . discard args
101     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
102     # . restore registers
103     59/pop-to-ECX
104     58/pop-to-EAX
105     # . epilog
106     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
107     5d/pop-to-EBP
108     c3/return
109 
110 # - tests
111 
112 test-write-byte-single:
113     # - check that read-byte returns first byte of 'file'
114     # setup
115     # . clear-stream(_test-stream)
116     # . . push args
117     68/push  _test-stream/imm32
118     # . . call
119     e8/call  clear-stream/disp32
120     # . . discard args
121     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
122     # . clear-stream(_test-buffered-file+4)
123     # . . push args
124     b8/copy-to-EAX  _test-buffered-file/imm32
125     05/add-to-EAX  4/imm32
126     50/push-EAX
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-byte(_test-buffered-file, 'A')
132     # . . push args
133     68/push  0x41/imm32
134     68/push  _test-buffered-file/imm32
135     # . . call
136     e8/call  write-byte/disp32
137     # . . discard args
138     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
139     # flush(_test-buffered-file)
140     # . . push args
141     68/push  _test-buffered-file/imm32
142     # . . call
143     e8/call  flush/disp32
144     # . . discard args
145     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
146     # check-ints-equal(*_test-stream->data, 'A', msg)
147     # . . push args
148     68/push  "F - test-write-byte-single"/imm32
149     68/push  0x41/imm32
150     # . . push *_test-stream->data
151     b8/copy-to-EAX  _test-stream/imm32
152     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
153     # . . call
154     e8/call  check-ints-equal/disp32
155     # . . discard args
156     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
157     # . end
158     c3/return
159 
160 # . . vim:nowrap:textwidth=0