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) read byte from stream
 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     # otherwise first populate stream from file
 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     # . EAX = read(f->fd, stream = f+4)
 69     # . . push args
 70     50/push-EAX
 71     ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
 72     # . . call
 73     e8/call  read/disp32
 74     # . . discard args
 75     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 76     # if EAX = 0 return 0xffffffff
 77     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # compare EAX
 78     75/jump-if-not-equal  $read-byte:from-stream/disp8
 79     b8/copy-to-EAX  0xffffffff/imm32
 80     eb/jump  $read-byte:end/disp8
 81 $read-byte:from-stream:
 82     # reading from stream
 83     # AL = f->data[f->read]
 84     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 85     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
 86     # ++f->read
 87     ff          0/subop/increment   1/mod/*+disp8   6/rm32/ESI    .           .             .           .           8/disp8         .                 # increment *(ESI+8)
 88 $read-byte:end:
 89     # . restore registers
 90     5e/pop-to-ESI
 91     59/pop-to-ECX
 92     # . epilog
 93     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 94     5d/pop-to-EBP
 95     c3/return
 96 
 97 # - tests
 98 
 99 test-read-byte-single:
100     # - check that read-byte returns first byte of 'file'
101     # setup
102     # . clear-stream(_test-stream)
103     # . . push args
104     68/push  _test-stream/imm32
105     # . . call
106     e8/call  clear-stream/disp32
107     # . . discard args
108     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
109     # . clear-stream(_test-buffered-file+4)
110     # . . push args
111     b8/copy-to-EAX  _test-buffered-file/imm32
112     05/add-to-EAX  4/imm32
113     50/push-EAX
114     # . . call
115     e8/call  clear-stream/disp32
116     # . . discard args
117     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
118     # . write(_test-stream, "Ab")
119     # . . push args
120     68/push  "Ab"/imm32
121     68/push  _test-stream/imm32
122     # . . call
123     e8/call  write/disp32
124     # . . discard args
125     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
126     # read-byte(_test-buffered-file)
127     # . . push args
128     68/push  _test-buffered-file/imm32
129     # . . call
130     e8/call  read-byte/disp32
131     # . . discard args
132     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
133     # check-ints-equal(EAX, 'A', msg)
134     # . . push args
135     68/push  "F - test-read-byte-single"/imm32
136     68/push  0x41/imm32
137     50/push-EAX
138     # . . call
139     e8/call  check-ints-equal/disp32
140     # . . discard args
141     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
142     # . end
143     c3/return
144 
145 test-read-byte-multiple:
146     # - call read-byte twice, check that second call returns second byte
147     # setup
148     # . clear-stream(_test-stream)
149     # . . push args
150     68/push  _test-stream/imm32
151     # . . call
152     e8/call  clear-stream/disp32
153     # . . discard args
154     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
155     # . clear-stream(_test-buffered-file+4)
156     # . . push args
157     b8/copy-to-EAX  _test-buffered-file/imm32
158     05/add-to-EAX  4/imm32
159     50/push-EAX
160     # . . call
161     e8/call  clear-stream/disp32
162     # . . discard args
163     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
164     # . write(_test-stream, "Ab")
165     # . . push args
166     68/push  "Ab"/imm32
167     68/push  _test-stream/imm32
168     # . . call
169     e8/call  write/disp32
170     # . . discard args
171     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
172     # read-byte(_test-buffered-file)
173     # . . push args
174     68/push  _test-buffered-file/imm32
175     # . . call
176     e8/call  read-byte/disp32
177     # . . discard args
178     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
179     # read-byte(_test-buffered-file)
180     # . . push args
181     68/push  _test-buffered-file/imm32
182     # . . call
183     e8/call  read-byte/disp32
184     # . . discard args
185     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
186     # check-ints-equal(EAX, 'b', msg)
187     # . . push args
188     68/push  "F - test-read-byte-multiple"/imm32
189     68/push  0x62/imm32
190     50/push-EAX
191     # . . call
192     e8/call  check-ints-equal/disp32
193     # . . discard args
194     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
195     # . end
196     c3/return
197 
198 test-read-byte-end-of-file:
199     # - call read-byte on an empty 'file', check that it returns -1
200     # setup
201     # . clear-stream(_test-stream)
202     # . . push args
203     68/push  _test-stream/imm32
204     # . . call
205     e8/call  clear-stream/disp32
206     # . . discard args
207     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
208     # . clear-stream(_test-buffered-file+4)
209     # . . push args
210     b8/copy-to-EAX  _test-buffered-file/imm32
211     05/add-to-EAX  4/imm32
212     50/push-EAX
213     # . . call
214     e8/call  clear-stream/disp32
215     # . . discard args
216     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
217     # read-byte(_test-buffered-file)
218     # . . push args
219     68/push  _test-buffered-file/imm32
220     # . . call
221     e8/call  read-byte/disp32
222     # . . discard args
223     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
224     # check-ints-equal(EAX, -1, msg)
225     # . . push args
226     68/push  "F - test-read-byte-end-of-file"/imm32
227     68/push  -1/imm32
228     50/push-EAX
229     # . . call
230     e8/call  check-ints-equal/disp32
231     # . . discard args
232     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
233     # . end
234     c3/return
235 
236 == data
237 
238 _test-buffered-file:
239     # file descriptor or (address stream)
240     _test-stream/imm32
241     # current write index
242     00 00 00 00
243     # current read index
244     00 00 00 00
245     # length (8)
246     08 00 00 00
247     # data
248     00 00 00 00 00 00 00 00  # 8 bytes
249 
250 # . . vim:nowrap:textwidth=0