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 Stdout:
 11     # file descriptor or (address stream)
 12     01 00 00 00  # 1 = standard output
 13     # current write index
 14     00 00 00 00
 15     # current read index
 16     00 00 00 00
 17     # length (8)
 18     08 00 00 00
 19     # data
 20     00 00 00 00 00 00 00 00  # 8 bytes
 21 
 22 # TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But
 23 # I don't want to type in 1024 bytes here.
 24 
 25 == code
 26 #   instruction                     effective address                                                   register    displacement    immediate
 27 # . op          subop               mod             rm32          base        index         scale       r32
 28 # . 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
 29 
 30 # main:
 31     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 32     # syscall(exit, Num-test-failures)
 33     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 34     b8/copy-to-EAX  1/imm32/exit
 35     cd/syscall  0x80/imm8
 36 
 37 # Write lower byte of 'n' to 'f'.
 38 write-byte:  # f : (address buffered-file), n : int -> <void>
 39     # . prolog
 40     55/push-EBP
 41     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 42     # . save registers
 43     51/push-ECX
 44     57/push-EDI
 45     # EDI = f
 46     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
 47     # ECX = f->write
 48     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           1/r32/ECX   4/disp8         .                 # copy *(EDI+4) to ECX
 49     # if (f->write >= f->length) flush and clear f's stream
 50     3b/compare                      1/mod/*+disp8   7/rm32/EDI    .           .             .           1/r32/ECX   0xc/disp8       .                 # compare ECX with *(EDI+12)
 51     7c/jump-if-lesser  $write-byte:to-stream/disp8
 52     # . flush(f)
 53     # . . push args
 54     57/push-EDI
 55     # . . call
 56     e8/call  flush/disp32
 57     # . . discard args
 58     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 59     # . clear-stream(stream = f+4)
 60     # . . push args
 61     8d/copy-address                 1/mod/*+disp8   7/rm32/EDI    .           .             .           0/r32/EAX   4/disp8         .                 # copy EDI+4 to EAX
 62     50/push-EAX
 63     # . . call
 64     e8/call  clear-stream/disp32
 65     # . . discard args
 66     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 67 $write-byte:to-stream:
 68     # write to stream
 69     # f->data[f->read] = LSB(n)
 70     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 71     8a/copy-byte                    1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/AL    0xc/disp8       .                 # copy byte at *(EBP+12) to AL
 72     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX   .           0/r32/AL    0x10/disp8      .                 # copy AL to *(EDI+ECX+16)
 73     # ++f->read
 74     ff          0/subop/increment   1/mod/*+disp8   7/rm32/EDI    .           .             .           .           4/disp8         .                 # increment *(EDI+4)
 75 $write-byte:end:
 76     # . restore registers
 77     5f/pop-to-EDI
 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   5/rm32/EBP    .           .             .           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 $flush:end:
103     # . restore registers
104     59/pop-to-ECX
105     58/pop-to-EAX
106     # . epilog
107     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
108     5d/pop-to-EBP
109     c3/return
110 
111 # - tests
112 
113 test-write-byte-single:
114     # - check that read-byte returns first byte of 'file'
115     # setup
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     # . clear-stream(_test-buffered-file+4)
124     # . . push args
125     b8/copy-to-EAX  _test-buffered-file/imm32
126     05/add-to-EAX  4/imm32
127     50/push-EAX
128     # . . call
129     e8/call  clear-stream/disp32
130     # . . discard args
131     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
132     # write-byte(_test-buffered-file, 'A')
133     # . . push args
134     68/push  0x41/imm32
135     68/push  _test-buffered-file/imm32
136     # . . call
137     e8/call  write-byte/disp32
138     # . . discard args
139     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
140     # flush(_test-buffered-file)
141     # . . push args
142     68/push  _test-buffered-file/imm32
143     # . . call
144     e8/call  flush/disp32
145     # . . discard args
146     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
147     # check-ints-equal(*_test-stream->data, 'A', msg)
148     # . . push args
149     68/push  "F - test-write-byte-single"/imm32
150     68/push  0x41/imm32
151     # . . push *_test-stream->data
152     b8/copy-to-EAX  _test-stream/imm32
153     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
154     # . . call
155     e8/call  check-ints-equal/disp32
156     # . . discard args
157     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
158     # . end
159     c3/return
160 
161 # . . vim:nowrap:textwidth=0