https://github.com/akkartik/mu/blob/master/subx/apps/crenshaw2-1.subx
  1 # Port of https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas
  2 # which corresponds to the section "single digits" in https://compilers.iecc.com/crenshaw/tutor2.txt
  3 # except that we support hex digits.
  4 #
  5 # To run (from the subx/ directory):
  6 #   $ ./subx translate *.subx apps/crenshaw2-1.subx -o apps/crenshaw2-1
  7 #   $ echo '3'  |./subx run apps/crenshaw2-1
  8 # Expected output:
  9 #   # syscall(exit, 3)
 10 #   bb/copy-to-EBX  3/imm32
 11 #   b8/copy-to-EAX  1/imm32/exit
 12 #   cd/syscall  0x80/imm8
 13 #
 14 # To run the generated output:
 15 #   $ echo '3'  |./subx run apps/crenshaw2-1 > z1.subx
 16 #   $ ./subx translate z1.subx -o z1
 17 #   $ ./subx run z1
 18 #   $ echo $?
 19 #   3
 20 #
 21 # Stdin must contain just a single hex digit. Other input will print an error:
 22 #   $ echo 'xyz'  |./subx run apps/crenshaw2-1
 23 #   Error: integer expected
 24 #
 25 # Names in this file sometimes follow Crenshaw's original rather than my usual
 26 # naming conventions.
 27 
 28 == code
 29 #   instruction                     effective address                                                   register    displacement    immediate
 30 # . op          subop               mod             rm32          base        index         scale       r32
 31 # . 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
 32 
 33 #?     # for debugging: run a single test; don't bother setting status code
 34 #?     e8/call test-get-num-reads-single-digit/disp32
 35 #?     eb/jump  $main:end/disp8
 36 
 37 # main: run tests if necessary, call 'compile' if not
 38     # . prolog
 39     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 40     # - if argc > 1 and argv[1] == "test", then return run_tests()
 41     # . argc > 1
 42     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0/disp8         1/imm32           # compare *EBP
 43     7e/jump-if-lesser-or-equal  $run-main/disp8
 44     # . argv[1] == "test"
 45     # . . push args
 46     68/push  "test"/imm32
 47     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 48     # . . call
 49     e8/call  kernel-string-equal?/disp32
 50     # . . discard args
 51     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 52     # . check result
 53     3d/compare-EAX  1/imm32
 54     75/jump-if-not-equal  $run-main/disp8
 55     # . run-tests()
 56     e8/call  run-tests/disp32
 57     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 58     eb/jump  $main:end/disp8
 59 $run-main:
 60     # - otherwise read a program from stdin and emit its translation to stdout
 61     # var ed/EAX : exit-descriptor
 62     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
 63     89/copy                         3/mod/direct    0/rm32/EAX    .           .             .           4/r32/ESP   .               .                 # copy ESP to EAX
 64     # configure ed to really exit()
 65     # . ed->target = 0
 66     c7          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
 67     # return compile(Stdin, 1/stdout, 2/stderr, ed)
 68     # . . push args
 69     50/push-EAX/ed
 70     68/push  2/imm32/stderr
 71     68/push  1/imm32/stdout
 72     68/push  Stdin/imm32
 73     # . . call
 74     e8/call  compile/disp32
 75     # . . discard args
 76     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
 77     # . syscall(exit, 0)
 78     bb/copy-to-EBX  0/imm32
 79 $main:end:
 80     b8/copy-to-EAX  1/imm32/exit
 81     cd/syscall  0x80/imm8
 82 
 83 # the main entry point
 84 compile:  # in : (address buffered-file), out : fd or (address stream), err : fd or (address stream), ed : (address exit-descriptor) -> <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     # prime the pump
 92     # . Look = get-char(in)
 93     # . . push args
 94     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8      .                    # push *(EBP+8)
 95     # . . call
 96     e8/call  get-char/disp32
 97     # . . discard args
 98     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 99     # var num/ECX : (address stream) on the stack
100     # Numbers can be 32 bits or 8 hex bytes long. One of them will be in 'Look', so we need space for 7 bytes.
101     # Sizing the stream just right buys us overflow-handling for free inside 'get-num'.
102     # Add 12 bytes for 'read', 'write' and 'length' fields, for a total of 19 bytes, or 0x13 in hex.
103     # The stack pointer is no longer aligned, so dump_stack() can be misleading past this point.
104     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x13/imm32        # subtract from ESP
105     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
106     # initialize the stream
107     # . num->length = 7
108     c7          0/subop/copy        1/mod/*+disp8   1/rm32/ECX    .           .             .           .           8/disp8         7/imm32           # copy to *(ECX+8)
109     # . clear-stream(num)
110     # . . push args
111     51/push-ECX
112     # . . call
113     e8/call  clear-stream/disp32
114     # . . discard args
115     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
116     # read a digit from 'in' into 'num'
117     # . get-num(in, num, err, ed)
118     # . . push args
119     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x14/disp8      .                 # push *(EBP+20)
120     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x10/disp8      .                 # push *(EBP+16)
121     51/push-ECX/num
122     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8      .                    # push *(EBP+8)
123     # . . call
124     e8/call  get-num/disp32
125     # . . discard args
126     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
127     # render 'num' into the following template on 'out':
128     #   bb/copy-to-EBX  _num_
129     #   b8/copy-to-EAX  1/imm32/exit
130     #   cd/syscall  0x80/imm8
131     #
132     # . write(out, "bb/copy-to-EBX  ")
133     # . . push args
134     68/push  "bb/copy-to-EBX  "/imm32
135     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
136     # . . call
137     e8/call  write/disp32
138     # . . discard args
139     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
140     # . write-stream(out, num)
141     # . . push args
142     51/push-ECX/num
143     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
144     # . . call
145     e8/call  write-stream/disp32
146     # . . discard args
147     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
148     # . write(out, Newline)
149     # . . push args
150     68/push  Newline/imm32
151     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
152     # . . call
153     e8/call  write/disp32
154     # . . discard args
155     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
156     # . write(out, "b8/copy-to-EAX  1/imm32/exit")
157     # . . push args
158     68/push  "b8/copy-to-EAX  1/imm32/exit"/imm32
159     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
160     # . . call
161     e8/call  write/disp32
162     # . . discard args
163     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
164     # . write(out, Newline)
165     # . . push args
166     68/push  Newline/imm32
167     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
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     # . write(out, "cd/syscall  0x80/imm8")
173     # . . push args
174     68/push  "cd/syscall  0x80/imm8"/imm32
175     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
176     # . . call
177     e8/call  write/disp32
178     # . . discard args
179     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
180     # . write(out, Newline)
181     # . . push args
182     68/push  Newline/imm32
183     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
184     # . . call
185     e8/call  write/disp32
186     # . . discard args
187     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
188 $compile:end:
189     # . restore registers
190     59/pop-to-ECX
191     58/pop-to-EAX
192     # . epilog
193     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
194     5d/pop-to-EBP
195     c3/return
196 
197 # Read a single digit into 'out'. Abort if there are none, or if there is no space in 'out'.
198 # Input comes from the global variable 'Look', and we leave the next byte from
199 # 'in' into it on exit.
200 get-num:  # in : (address buffered-file), out : (address stream), err : fd or (address stream), ed : (address exit-descriptor) -> <void>
201     # pseudocode:
202     #   if (!is-digit?(Look)) expected(ed, err, "integer")
203     #   if (out->write >= out->length)
204     #     write(err, "Error: too many digits in number\n")
205     #     stop(ed, 1)
206     #   out->data[out->write] = LSB(Look)
207     #   ++out->write
208     #   Look = get-char(in)
209     #
210     # registers:
211     #   in: ESI
212     #   out: EDI
213     #   out->write: ECX (cached copy; need to keep in sync)
214     #   out->length: EDX
215     #   temporaries: EAX, EBX
216     # We can't allocate Look to a register because it gets written implicitly in
217     # get-char in each iteration of the loop. (Thereby demonstrating that it's
218     # not the right interface for us. But we'll keep it just to follow Crenshaw.)
219     #
220     # . prolog
221     55/push-EBP
222     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
223     # - if (is-digit?(Look)) expected(ed, err, "integer")
224     # . EAX = is-digit?(Look)
225     # . . push args
226     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
227     # . . call
228     e8/call  is-digit?/disp32
229     # . . discard args
230     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
231     # . if (EAX == 0)
232     3d/compare-EAX  0/imm32
233     75/jump-if-not-equal  $get-num:main/disp8
234     # . expected(ed, err, "integer")
235     # . . push args
236     68/push  "integer"/imm32
237     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x10/disp8      .                 # push *(EBP+16)
238     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x14/disp8      .                 # push *(EBP+20)
239     # . . call
240     e8/call  expected/disp32  # never returns
241     # . . discard args
242     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
243 $get-num:main:
244     # - otherwise read a digit
245     # . save registers
246     50/push-EAX
247     51/push-ECX
248     52/push-EDX
249     53/push-EBX
250     56/push-ESI
251     57/push-EDI
252     # read necessary variables to registers
253     # ESI = in
254     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
255     # EDI = out
256     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   0xc/disp8       .                 # copy *(EBP+12) to EDI
257     # ECX = out->write
258     8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
259     # EDX = out->length
260     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   8/disp8         .                 # copy *(EDI+8) to EDX
261     # if (out->write >= out->length) error
262     39/compare                      3/mod/direct    2/rm32/EDX    .           .             .           1/r32/ECX   .               .                 # compare EDX with ECX
263     7d/jump-if-lesser  $get-num:stage2/disp8
264     # . error(ed, err, msg)  # TODO: show full number
265     # . . push args
266     68/push  "get-num: too many digits in number"/imm32
267     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x10/disp8      .                 # push *(EBP+16)
268     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x14/disp8      .                 # push *(EBP+20)
269     # . . call
270     e8/call  error/disp32  # never returns
271     # . . discard args
272     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
273 $get-num:stage2:
274     # out->data[out->write] = LSB(Look)
275     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX   .           3/r32/EBX   0xc/disp8       .                 # copy EDI+ECX+12 to EBX
276     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Look/disp32     .                 # copy *Look to EAX
277     88/copy-byte                    0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/AL    .               .                 # copy byte at AL to *EBX
278     # ++out->write
279     41/increment-ECX
280     # Look = get-char(in)
281     # . . push args
282     56/push-ESI
283     # . . call
284     e8/call  get-char/disp32
285     # . . discard args
286     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
287 $get-num:loop-end:
288     # persist necessary variables from registers
289     89/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy ECX to *EDI
290 $get-num:end:
291     # . restore registers
292     5f/pop-to-EDI
293     5e/pop-to-ESI
294     5b/pop-to-EBX
295     5a/pop-to-EDX
296     59/pop-to-ECX
297     58/pop-to-EAX
298     # . epilog
299     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
300     5d/pop-to-EBP
301     c3/return
302 
303 test-get-num-reads-single-digit:
304     # - check that get-num returns first character if it's a digit
305     # This test uses exit-descriptors. Use EBP for setting up local variables.
306     55/push-EBP
307     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
308     # clear all streams
309     # . clear-stream(_test-stream)
310     # . . push args
311     68/push  _test-stream/imm32
312     # . . call
313     e8/call  clear-stream/disp32
314     # . . discard args
315     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
316     # . clear-stream(_test-buffered-file+4)
317     # . . push args
318     b8/copy-to-EAX  _test-buffered-file/imm32
319     05/add-to-EAX  4/imm32
320     50/push-EAX
321     # . . call
322     e8/call  clear-stream/disp32
323     # . . discard args
324     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
325     # . clear-stream(_test-output-stream)
326     # . . push args
327     68/push  _test-output-stream/imm32
328     # . . call
329     e8/call  clear-stream/disp32
330     # . . discard args
331     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
332     # . clear-stream(_test-error-stream)
333     # . . push args
334     68/push  _test-error-stream/imm32
335     # . . call
336     e8/call  clear-stream/disp32
337     # . . discard args
338     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
339     # initialize 'in'
340     # . write(_test-stream, "3")
341     # . . push args
342     68/push  "3"/imm32
343     68/push  _test-stream/imm32
344     # . . call
345     e8/call  write/disp32
346     # . . discard args
347     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
348     # initialize exit-descriptor 'ed' for the call to 'get-num' below
349     # . var ed/EAX : exit-descriptor
350     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
351     89/copy                         3/mod/direct    0/rm32/EAX    .           .             .           4/r32/ESP   .               .                 # copy ESP to EAX
352     # . tailor-exit-descriptor(ed, 16)
353     # . . push args
354     68/push  0x10/imm32/nbytes-of-args-for-get-num
355     50/push-EAX/ed
356     # . . call
357     e8/call  tailor-exit-descriptor/disp32
358     # . . discard args
359     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
360     # prime the pump
361     # . get-char(_test-buffered-file)
362     # . . push args
363     68/push  _test-buffered-file/imm32
364     # . . call
365     e8/call  get-char/disp32
366     # . . discard args
367     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
368     # get-num(in, out, err, ed)
369     # . . push args
370     50/push-EAX/ed
371     68/push  _test-error-stream/imm32
372     68/push  _test-output-stream/imm32
373     68/push  _test-buffered-file/imm32
374     # . . call
375     e8/call  get-num/disp32
376     # registers except ESP may be clobbered at this point
377     # . . discard args
378     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
379     # check-ints-equal(*_test-output-stream->data, '3', msg)
380     # . . push args
381     68/push  "F - test-get-num-reads-single-digit"/imm32
382     68/push  0x33/imm32
383     b8/copy-to-EAX  _test-output-stream/imm32
384     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
385     # . . call
386     e8/call  check-ints-equal/disp32
387     # . . discard args
388     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
389     # . reclaim locals
390     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
391     5d/pop-to-EBP
392     c3/return
393 
394 test-get-num-aborts-on-non-digit-in-Look:
395     # - check that get-num returns first character if it's a digit
396     # This test uses exit-descriptors. Use EBP for setting up local variables.
397     55/push-EBP
398     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
399     # clear all streams
400     # . clear-stream(_test-stream)
401     # . . push args
402     68/push  _test-stream/imm32
403     # . . call
404     e8/call  clear-stream/disp32
405     # . . discard args
406     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
407     # . clear-stream(_test-buffered-file+4)
408     # . . push args
409     b8/copy-to-EAX  _test-buffered-file/imm32
410     05/add-to-EAX  4/imm32
411     50/push-EAX
412     # . . call
413     e8/call  clear-stream/disp32
414     # . . discard args
415     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
416     # . clear-stream(_test-output-stream)
417     # . . push args
418     68/push  _test-output-stream/imm32
419     # . . call
420     e8/call  clear-stream/disp32
421     # . . discard args
422     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
423     # . clear-stream(_test-error-stream)
424     # . . push args
425     68/push  _test-error-stream/imm32
426     # . . call
427     e8/call  clear-stream/disp32
428     # . . discard args
429     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
430     # initialize 'in'
431     # . write(_test-stream, "3")
432     # . . push args
433     68/push  "3"/imm32
434     68/push  _test-stream/imm32
435     # . . call
436     e8/call  write/disp32
437     # . . discard args
438     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
439     # initialize exit-descriptor 'ed' for the call to 'get-num' below
440     # . var ed/EAX : (address exit-descriptor)
441     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
442     89/copy                         3/mod/direct    0/rm32/EAX    .           .             .           4/r32/ESP   .               .                 # copy ESP to EAX
443     # . tailor-exit-descriptor(ed, 16)
444     # . . push args
445     68/push  0x10/imm32/nbytes-of-args-for-get-num
446     50/push-EAX/ed
447     # . . call
448     e8/call  tailor-exit-descriptor/disp32
449     # . . discard args
450     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
451     # *don't* prime the pump
452     # get-num(in, out, err, ed)
453     # . . push args
454     50/push-EAX/ed
455     68/push  _test-error-stream/imm32
456     68/push  _test-output-stream/imm32
457     68/push  _test-buffered-file/imm32
458     # . . call
459     e8/call  get-num/disp32
460     # registers except ESP may be clobbered at this point
461     # . . discard args
462     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
463     # check that get-num tried to call exit(1)
464     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
465     # . . push args
466     68/push  "F - test-get-num-aborts-on-non-digit-in-Look"/imm32
467     68/push  2/imm32
468     # . . push ed->value
469     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
470     # . . call
471     e8/call  check-ints-equal/disp32
472     # . . discard args
473     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
474     # . reclaim locals
475     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
476     5d/pop-to-EBP
477     c3/return
478 
479 ## helpers
480 
481 # write(f, "Error: "+s+" expected\n") then stop(ed, 1)
482 expected:  # ed : (address exit-descriptor), f : fd or (address stream), s : (address array byte) -> <void>
483     # . prolog
484     55/push-EBP
485     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
486     # write(f, "Error: ")
487     # . . push args
488     68/push  "Error: "/imm32
489     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
490     # . . call
491     e8/call  write/disp32
492     # . . discard args
493     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
494     # write(f, s)
495     # . . push args
496     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0x10/disp8      .                 # push *(EBP+16)
497     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
498     # . . call
499     e8/call  write/disp32
500     # . . discard args
501     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
502     # write(f, " expected")
503     # . . push args
504     68/push  " expected"/imm32
505     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
506     # . . call
507     e8/call  write/disp32
508     # . . discard args
509     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
510     # write(f, Newline)
511     # . . push args
512     68/push  Newline/imm32
513     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
514     # . . call
515     e8/call  write/disp32
516     # . . discard args
517     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
518     # stop(ed, 1)
519     # . . push args
520     68/push  1/imm32
521     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
522     # . . call
523     e8/call  stop/disp32
524     # should never get past this point
525 $expected:dead-end:
526     # . epilog
527     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
528     5d/pop-to-EBP
529     c3/return
530 
531 # read a byte from 'f', and save it in 'Look'
532 get-char:  # f : (address buffered-file) -> <void>
533     # . prolog
534     55/push-EBP
535     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
536     # . save registers
537     50/push-EAX
538     # read-byte(f)
539     # . . push args
540     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
541     # . . call
542     e8/call  read-byte/disp32
543     # . . discard args
544     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
545     # save EAX to Look
546     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Look/disp32     .                 # copy EAX to *Look
547 $get-char:end:
548     # . restore registers
549     58/pop-to-EAX
550     # . epilog
551     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
552     5d/pop-to-EBP
553     c3/return
554 
555 is-digit?:  # c : int -> EAX : boolean
556     # . prolog
557     55/push-EBP
558     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
559     # EAX = false
560     b8/copy-to-EAX  0/imm32
561     # if (c < '0') return false
562     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         0x30/imm32        # compare *(EBP+8)
563     7c/jump-if-lesser  $is-digit?:end/disp8
564     # if (c > '9') return false
565     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         0x39/imm32        # compare *(EBP+8)
566     7f/jump-if-greater  $is-digit?:end/disp8
567     # otherwise return true
568     b8/copy-to-EAX  1/imm32
569 $is-digit?:end:
570     # . epilog
571     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
572     5d/pop-to-EBP
573     c3/return
574 
575 == data
576 
577 Look: # (char)
578     00 00 00 00  # = 0
579 
580 _test-output-stream:
581     # current write index
582     00 00 00 00
583     # current read index
584     00 00 00 00
585     # length (= 8)
586     08 00 00 00
587     # data
588     00 00 00 00 00 00 00 00  # 8 bytes
589 
590 _test-error-stream:
591     # current write index
592     00 00 00 00
593     # current read index
594     00 00 00 00
595     # length (= 8)
596     08 00 00 00
597     # data
598     00 00 00 00 00 00 00 00  # 8 bytes
599 
600 # . . vim:nowrap:textwidth=0