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. But
 28 # I don't want to type 1024 bytes here.
 29 
 30 == code
 31 #   instruction                     effective address                                                   register    displacement    immediate
 32 # . op          subop               mod             rm32          base        index         scale       r32
 33 # . 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
 34 
 35 # main:
 36     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 37 #?     e8/call  test-read-byte-multiple/disp32
 38     # syscall(exit, Num-test-failures)
 39     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 40     b8/copy-to-EAX  1/imm32
 41     cd/syscall  0x80/imm8
 42 
 43 # return next byte value in EAX, with top 3 bytes cleared.
 44 # On EOF, return 0xffffffff.
 45 read-byte:  # f : (address buffered-file) -> byte-or-eof/EAX
 46     # . prolog
 47     55/push-EBP
 48     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 49     # . save registers
 50     51/push-ECX
 51     56/push-ESI
 52     # ESI = f
 53     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
 54     # ECX = f->read
 55     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(ESI+8) to ECX
 56     # if (f->read >= f->write) populate stream from file
 57     3b/compare                      1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   4/disp8         .                 # compare ECX with *(ESI+4)
 58     7c/jump-if-lesser  $read-byte:from-stream/disp8
 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     # read byte 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 # - tests
 97 
 98 test-read-byte-single:
 99     # - check that read-byte returns first byte of 'file'
100     # setup
101     # . clear-stream(_test-stream)
102     # . . push args
103     68/push  _test-stream/imm32
104     # . . call
105     e8/call  clear-stream/disp32
106     # . . discard args
107     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
108     # . clear-stream(_test-buffered-file+4)
109     # . . push args
110     b8/copy-to-EAX  _test-buffered-file/imm32
111     05/add-to-EAX  4/imm32
112     50/push-EAX
113     # . . call
114     e8/call  clear-stream/disp32
115     # . . discard args
116     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
117     # . write(_test-stream, "Ab")
118     # . . push args
119     68/push  "Ab"/imm32
120     68/push  _test-stream/imm32
121     # . . call
122     e8/call  write/disp32
123     # . . discard args
124     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
125     # read-byte(_test-buffered-file)
126     # . . push args
127     68/push  _test-buffered-file/imm32
128     # . . call
129     e8/call  read-byte/disp32
130     # . . discard args
131     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
132     # check-ints-equal(EAX, 'A', msg)
133     # . . push args
134     68/push  "F - test-read-byte-single"/imm32
135     68/push  0x41/imm32
136     50/push-EAX
137     # . . call
138     e8/call  check-ints-equal/disp32
139     # . . discard args
140     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
141     # . end
142     c3/return
143 
144 test-read-byte-multiple:
145     # - call read-byte twice, check that second call returns second byte
146     # setup
147     # . clear-stream(_test-stream)
148     # . . push args
149     68/push  _test-stream/imm32
150     # . . call
151     e8/call  clear-stream/disp32
152     # . . discard args
153     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
154     # . clear-stream(_test-buffered-file+4)
155     # . . push args
156     b8/copy-to-EAX  _test-buffered-file/imm32
157     05/add-to-EAX  4/imm32
158     50/push-EAX
159     # . . call
160     e8/call  clear-stream/disp32
161     # . . discard args
162     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
163     # . write(_test-stream, "Ab")
164     # . . push args
165     68/push  "Ab"/imm32
166     68/push  _test-stream/imm32
167     # . . call
168     e8/call  write/disp32
169     # . . discard args
170     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
171     # read-byte(_test-buffered-file)
172     # . . push args
173     68/push  _test-buffered-file/imm32
174     # . . call
175     e8/call  read-byte/disp32
176     # . . discard args
177     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
178     # read-byte(_test-buffered-file)
179     # . . push args
180     68/push  _test-buffered-file/imm32
181     # . . call
182     e8/call  read-byte/disp32
183     # . . discard args
184     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
185     # check-ints-equal(EAX, 'b', msg)
186     # . . push args
187     68/push  "F - test-read-byte-multiple"/imm32
188     68/push  0x62/imm32
189     50/push-EAX
190     # . . call
191     e8/call  check-ints-equal/disp32
192     # . . discard args
193     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
194     # . end
195     c3/return
196 
197 test-read-byte-end-of-file:
198     # - call read-byte on an empty 'file', check that it returns -1
199     # setup
200     # . clear-stream(_test-stream)
201     # . . push args
202     68/push  _test-stream/imm32
203     # . . call
204     e8/call  clear-stream/disp32
205     # . . discard args
206     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
207     # . clear-stream(_test-buffered-file+4)
208     # . . push args
209     b8/copy-to-EAX  _test-buffered-file/imm32
210     05/add-to-EAX  4/imm32
211     50/push-EAX
212     # . . call
213     e8/call  clear-stream/disp32
214     # . . discard args
215     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
216     # read-byte(_test-buffered-file)
217     # . . push args
218     68/push  _test-buffered-file/imm32
219     # . . call
220     e8/call  read-byte/disp32
221     # . . discard args
222     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
223     # check-ints-equal(EAX, -1, msg)
224     # . . push args
225     68/push  "F - test-read-byte-end-of-file"/imm32
226     68/push  -1/imm32
227     50/push-EAX
228     # . . call
229     e8/call  check-ints-equal/disp32
230     # . . discard args
231     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
232     # . end
233     c3/return
234 
235 == data
236 
237 _test-buffered-file:
238     # file descriptor or (address stream)
239     _test-stream/imm32
240     # current write index
241     00 00 00 00
242     # current read index
243     00 00 00 00
244     # length (8)
245     08 00 00 00
246     # data
247     00 00 00 00 00 00 00 00  # 8 bytes
248 
249 # . . vim:nowrap:textwidth=0