https://github.com/akkartik/mu/blob/master/subx/059read-byte.subx
  1 # read-byte: one higher-level abstraction atop 'read'.
  2 #
  3 # There are many situations where 'read' is a lot to manage, and we need
  4 # to abstract some details away. One of them is when we want to read a file
  5 # character by character. In this situation we follow C's FILE data structure,
  6 # which manages the underlying file descriptor together with the buffer it
  7 # reads into. We call our version 'buffered-file'. Should be useful with other
  8 # primitives as well, in later layers.
  9 
 10 == data
 11 
 12 # The buffered file for standard input. Also illustrates the layout for
 13 # buffered-file.
 14 
 15 Stdin:
 16     # file descriptor or (address stream)
 17     00 00 00 00  # 0 = standard input
 18     # current write index
 19     00 00 00 00
 20     # current read index
 21     00 00 00 00
 22     # length (8)
 23     08 00 00 00
 24     # data
 25     00 00 00 00 00 00 00 00  # 8 bytes
 26 
 27 # TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency.
 28 
 29 == code
 30 #   instruction                     effective address                                                   register    displacement    immediate
 31 # . op          subop               mod             rm32          base        index         scale       r32
 32 # . 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
 33 
 34 # main:
 35     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 36 #?     e8/call  test-read-byte-multiple/disp32
 37     # syscall(exit, Num-test-failures)
 38     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 39     b8/copy-to-EAX  1/imm32
 40     cd/syscall  0x80/imm8
 41 
 42 # return next byte value in EAX, with top 3 bytes cleared.
 43 # On EOF, return 0xffffffff.
 44 read-byte:  # f : (address buffered-file) -> byte-or-eof/EAX
 45     # . prolog
 46     55/push-EBP
 47     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 48     # . save registers
 49     51/push-ECX
 50     56/push-ESI
 51     # ESI = f
 52     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
 53     # ECX = f->read
 54     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(ESI+8) to ECX
 55     # if (f->read < f->write) read byte from stream
 56     3b/compare                      1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   4/disp8         .                 # compare ECX with *(ESI+4)
 57     7c/jump-if-lesser  $read-byte:from-stream/disp8
 58     # otherwise first populate stream from file
 59     # . clear-stream(stream = f+4)
 60     # . . push args
 61     8d/copy-address                 1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # copy ESI+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     # . EAX = read(f->fd, stream = f+4)
 68     # . . push args
 69     50/push-EAX
 70     ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
 71     # . . call
 72     e8/call  read/disp32
 73     # . . discard args
 74     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 75     # if EAX = 0 return 0xffffffff
 76     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # compare EAX
 77     75/jump-if-not-equal  $read-byte:from-stream/disp8
 78     b8/copy-to-EAX  0xffffffff/imm32
 79     eb/jump  $read-byte:end/disp8
 80 $read-byte:from-stream:
 81     # reading from stream
 82     # AL = f->data[f->read]
 83     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 84     8a/copy-byte                    1/mod/*+disp8   4/rm32/sib    6/base/ESI  1/index/ECX   .           0/r32/AL    0x10/disp8      .                 # copy *(ESI+ECX+16) to AL
 85     # ++f->read
 86     ff          0/subop/increment   1/mod/*+disp8   6/rm32/ESI    .           .             .           .           8/disp8         .                 # increment *(ESI+8)
 87 $read-byte:end:
 88     # . restore registers
 89     5e/pop-to-ESI
 90     59/pop-to-ECX
 91     # . epilog
 92     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 93     5d/pop-to-EBP
 94     c3/return
 95 
 96 # todo: how should write-byte look? What should it do when the output has no
 97 # space remaining? Maybe return an error code.
 98 
 99 # - tests
100 
101 test-read-byte-single:
102     # - check that read-byte returns first byte of 'file'
103     # setup
104     # . clear-stream(_test-stream)
105     # . . push args
106     68/push  _test-stream/imm32
107     # . . call
108     e8/call  clear-stream/disp32
109     # . . discard args
110     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
111     # . clear-stream(_test-buffered-file+4)
112     # . . push args
113     b8/copy-to-EAX  _test-buffered-file/imm32
114     05/add-to-EAX  4/imm32
115     50/push-EAX
116     # . . call
117     e8/call  clear-stream/disp32
118     # . . discard args
119     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
120     # . write(_test-stream, "Ab")
121     # . . push args
122     68/push  "Ab"/imm32
123     68/push  _test-stream/imm32
124     # . . call
125     e8/call  write/disp32
126     # . . discard args
127     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
128     # read-byte(_test-buffered-file)
129     # . . push args
130     68/push  _test-buffered-file/imm32
131     # . . call
132     e8/call  read-byte/disp32
133     # . . discard args
134     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
135     # check-ints-equal(EAX, 'A')
136     # . . push args
137     68/push  "F - test-read-byte-single"/imm32
138     68/push  0x41/imm32
139     50/push-EAX
140     # . . call
141     e8/call  check-ints-equal/disp32
142     # . . discard args
143     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
144     # . end
145     c3/return
146 
147 test-read-byte-multiple:
148     # - call read-byte twice, check that second call returns second byte
149     # setup
150     # . clear-stream(_test-stream)
151     # . . push args
152     68/push  _test-stream/imm32
153     # . . call
154     e8/call  clear-stream/disp32
155     # . . discard args
156     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
157     # . clear-stream(_test-buffered-file+4)
158     # . . push args
159     b8/copy-to-EAX  _test-buffered-file/imm32
160     05/add-to-EAX  4/imm32
161     50/push-EAX
162     # . . call
163     e8/call  clear-stream/disp32
164     # . . discard args
165     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
166     # . write(_test-stream, "Ab")
167     # . . push args
168     68/push  "Ab"/imm32
169     68/push  _test-stream/imm32
170     # . . call
171     e8/call  write/disp32
172     # . . discard args
173     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
174     # read-byte(_test-buffered-file)
175     # . . push args
176     68/push  _test-buffered-file/imm32
177     # . . call
178     e8/call  read-byte/disp32
179     # . . discard args
180     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
181     # read-byte(_test-buffered-file)
182     # . . push args
183     68/push  _test-buffered-file/imm32
184     # . . call
185     e8/call  read-byte/disp32
186     # . . discard args
187     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
188     # check-ints-equal(EAX, 'b')
189     # . . push args
190     68/push  "F - test-read-byte-multiple"/imm32
191     68/push  0x62/imm32
192     50/push-EAX
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 test-read-byte-end-of-file:
201     # - call read-byte on an empty 'file', check that it returns -1
202     # setup
203     # . clear-stream(_test-stream)
204     # . . push args
205     68/push  _test-stream/imm32
206     # . . call
207     e8/call  clear-stream/disp32
208     # . . discard args
209     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
210     # . clear-stream(_test-buffered-file+4)
211     # . . push args
212     b8/copy-to-EAX  _test-buffered-file/imm32
213     05/add-to-EAX  4/imm32
214     50/push-EAX
215     # . . call
216     e8/call  clear-stream/disp32
217     # . . discard args
218     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
219     # read-byte(_test-buffered-file)
220     # . . push args
221     68/push  _test-buffered-file/imm32
222     # . . call
223     e8/call  read-byte/disp32
224     # . . discard args
225     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
226     # check-ints-equal(EAX, -1)
227     # . . push args
228     68/push  "F - test-read-byte-end-of-file"/imm32
229     68/push  -1/imm32
230     50/push-EAX
231     # . . call
232     e8/call  check-ints-equal/disp32
233     # . . discard args
234     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
235     # . end
236     c3/return
237 
238 == data
239 
240 _test-buffered-file:
241     # file descriptor or (address stream)
242     _test-stream/imm32
243     # current write index
244     00 00 00 00
245     # current read index
246     00 00 00 00
247     # length (8)
248     08 00 00 00
249     # data
250     00 00 00 00 00 00 00 00  # 8 bytes
251 
252 # . . vim:nowrap:textwidth=0