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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/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     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
 64     # configure ed to really exit()
 65     # . ed->target = 0
 66     c7/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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   .               .                 # copy ESP to ECX
106     # initialize the stream
107     # . num->length = 7
108     c7/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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x14/disp8      .                 # push *(EBP+20)
120     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
121     51/push-ECX/num
122     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           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     # . restore registers
189     59/pop-to-ECX
190     58/pop-to-EAX
191     # . epilog
192     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
193     5d/pop-to-EBP
194     c3/return
195 
196 # Read a single digit into 'out'. Abort if there are none, or if there is no space in 'out'.
197 # Input comes from the global variable 'Look', and we leave the next byte from
198 # 'in' into it on exit.
199 get-num:  # in : (address buffered-file), out : (address stream), err : fd or (address stream), ed : (address exit-descriptor) -> <void>
200     # pseudocode:
201     #   if !is-digit?(Look) expected(ed, err, "integer")
202     #   if out->write >= out->length
203     #     write(err, "Error: too many digits in number\n")
204     #     stop(ed, 1)
205     #   out->data[out->write] = LSB(Look)
206     #   ++out->write
207     #   Look = get-char(in)
208     #
209     # registers:
210     #   ESI : in
211     #   EDI : out
212     #   EAX : temp
213     #   ECX : out->write
214     #   EDX : out->length
215     #   EBX : temp2
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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
238     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           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   4/rm32/sib    5/base/EBP  4/index/none  .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
255     # EDI = out
256     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           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     3b/compare                      3/mod/direct    1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # 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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
268     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           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     # . restore registers
291     5f/pop-to-EDI
292     5e/pop-to-ESI
293     5b/pop-to-EBX
294     5a/pop-to-EDX
295     59/pop-to-ECX
296     58/pop-to-EAX
297     # . epilog
298     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
299     5d/pop-to-EBP
300     c3/return
301 
302 test-get-num-reads-single-digit:
303     # - check that get-num returns first character if it's a digit
304     # This test uses exit-descriptors. Use EBP for setting up local variables.
305     55/push-EBP
306     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
307     # clear all streams
308     # . clear-stream(_test-stream)
309     # . . push args
310     68/push  _test-stream/imm32
311     # . . call
312     e8/call  clear-stream/disp32
313     # . . discard args
314     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
315     # . clear-stream(_test-buffered-file+4)
316     # . . push args
317     b8/copy-to-EAX  _test-buffered-file/imm32
318     05/add-to-EAX  4/imm32
319     50/push-EAX
320     # . . call
321     e8/call  clear-stream/disp32
322     # . . discard args
323     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
324     # . clear-stream(_test-output-stream)
325     # . . push args
326     68/push  _test-output-stream/imm32
327     # . . call
328     e8/call  clear-stream/disp32
329     # . . discard args
330     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
331     # . clear-stream(_test-error-stream)
332     # . . push args
333     68/push  _test-error-stream/imm32
334     # . . call
335     e8/call  clear-stream/disp32
336     # . . discard args
337     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
338     # initialize 'in'
339     # . write(_test-stream, "3")
340     # . . push args
341     68/push  "3"/imm32
342     68/push  _test-stream/imm32
343     # . . call
344     e8/call  write/disp32
345     # . . discard args
346     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
347     # initialize exit-descriptor 'ed' for the call to 'get-num' below
348     # . var ed/EAX : exit-descriptor
349     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
350     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
351     # . tailor-exit-descriptor(ed, 16)
352     # . . push args
353     68/push  0x10/imm32/nbytes-of-args-for-get-num
354     50/push-EAX/ed
355     # . . call
356     e8/call  tailor-exit-descriptor/disp32
357     # . . discard args
358     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
359     # prime the pump
360     # . get-char(_test-buffered-file)
361     # . . push args
362     68/push  _test-buffered-file/imm32
363     # . . call
364     e8/call  get-char/disp32
365     # . . discard args
366     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
367     # get-num(in, out, err, ed)
368     # . . push args
369     50/push-EAX/ed
370     68/push  _test-error-stream/imm32
371     68/push  _test-output-stream/imm32
372     68/push  _test-buffered-file/imm32
373     # . . call
374     e8/call  get-num/disp32
375     # registers except ESP may be clobbered at this point
376     # . . discard args
377     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
378     # check-ints-equal(*_test-output-stream->data, '3', msg)
379     # . . push args
380     68/push  "F - test-get-num-reads-single-digit"/imm32
381     68/push  0x33/imm32
382     b8/copy-to-EAX  _test-output-stream/imm32
383     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
384     # . . call
385     e8/call  check-ints-equal/disp32
386     # . . discard args
387     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
388     # . reclaim locals
389     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
390     5d/pop-to-EBP
391     c3/return
392 
393 test-get-num-aborts-on-non-digit-in-Look:
394     # - check that get-num returns first character if it's a digit
395     # This test uses exit-descriptors. Use EBP for setting up local variables.
396     55/push-EBP
397     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
398     # clear all streams
399     # . clear-stream(_test-stream)
400     # . . push args
401     68/push  _test-stream/imm32
402     # . . call
403     e8/call  clear-stream/disp32
404     # . . discard args
405     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
406     # . clear-stream(_test-buffered-file+4)
407     # . . push args
408     b8/copy-to-EAX  _test-buffered-file/imm32
409     05/add-to-EAX  4/imm32
410     50/push-EAX
411     # . . call
412     e8/call  clear-stream/disp32
413     # . . discard args
414     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
415     # . clear-stream(_test-output-stream)
416     # . . push args
417     68/push  _test-output-stream/imm32
418     # . . call
419     e8/call  clear-stream/disp32
420     # . . discard args
421     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
422     # . clear-stream(_test-error-stream)
423     # . . push args
424     68/push  _test-error-stream/imm32
425     # . . call
426     e8/call  clear-stream/disp32
427     # . . discard args
428     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
429     # initialize 'in'
430     # . write(_test-stream, "3")
431     # . . push args
432     68/push  "3"/imm32
433     68/push  _test-stream/imm32
434     # . . call
435     e8/call  write/disp32
436     # . . discard args
437     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
438     # initialize exit-descriptor 'ed' for the call to 'get-num' below
439     # . var ed/EAX : (address exit-descriptor)
440     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
441     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
442     # . tailor-exit-descriptor(ed, 16)
443     # . . push args
444     68/push  0x10/imm32/nbytes-of-args-for-get-num
445     50/push-EAX/ed
446     # . . call
447     e8/call  tailor-exit-descriptor/disp32
448     # . . discard args
449     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
450     # *don't* prime the pump
451     # get-num(in, out, err, ed)
452     # . . push args
453     50/push-EAX/ed
454     68/push  _test-error-stream/imm32
455     68/push  _test-output-stream/imm32
456     68/push  _test-buffered-file/imm32
457     # . . call
458     e8/call  get-num/disp32
459     # registers except ESP may be clobbered at this point
460     # . . discard args
461     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
462     # check that get-num tried to call exit(1)
463     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
464     # . . push args
465     68/push  "F - test-get-num-aborts-on-non-digit-in-Look"/imm32
466     68/push  2/imm32
467     # . . push ed->value
468     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
469     # . . call
470     e8/call  check-ints-equal/disp32
471     # . . discard args
472     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
473     # . reclaim locals
474     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
475     5d/pop-to-EBP
476     c3/return
477 
478 ## helpers
479 
480 # write(f, "Error: "+s+" expected\n") then stop(ed, 1)
481 expected:  # ed : (address exit-descriptor), f : fd or (address stream), s : (address array byte) -> <void>
482     # . prolog
483     55/push-EBP
484     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
485     # write(f, "Error: ")
486     # . . push args
487     68/push  "Error: "/imm32
488     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
489     # . . call
490     e8/call  write/disp32
491     # . . discard args
492     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
493     # write(f, s)
494     # . . push args
495     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
496     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
497     # . . call
498     e8/call  write/disp32
499     # . . discard args
500     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
501     # write(f, " expected")
502     # . . push args
503     68/push  " expected"/imm32
504     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
505     # . . call
506     e8/call  write/disp32
507     # . . discard args
508     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
509     # write(f, Newline)
510     # . . push args
511     68/push  Newline/imm32
512     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
513     # . . call
514     e8/call  write/disp32
515     # . . discard args
516     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
517     # stop(ed, 1)
518     # . . push args
519     68/push  1/imm32
520     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         .                 # push *(EBP+8)
521     # . . call
522     e8/call  stop/disp32
523     # should never get past this point
524     # . epilog
525     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
526     5d/pop-to-EBP
527     c3/return
528 
529 # read a byte from 'f', and save it in 'Look'
530 get-char:  # f : (address buffered-file) -> <void>
531     # . prolog
532     55/push-EBP
533     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
534     # . save registers
535     50/push-EAX
536     # read-byte(f)
537     # . . push args
538     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       .                 # push *(EBP+8)
539     # . . call
540     e8/call  read-byte/disp32
541     # . . discard args
542     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
543     # save EAX to Look
544     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Look/disp32     .                 # copy EAX to *Look
545     # . restore registers
546     58/pop-to-EAX
547     # . epilog
548     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
549     5d/pop-to-EBP
550     c3/return
551 
552 is-digit?:  # c : int -> bool/EAX
553     # . prolog
554     55/push-EBP
555     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
556     # EAX = false
557     b8/copy-to-EAX  0/imm32
558     # if c < '0' return false
559     81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       0x30/imm32        # compare *(EBP+8)
560     7c/jump-if-lesser  $is-digit?:end/disp8
561     # if c > '9' return false
562     81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       0x39/imm32        # compare *(EBP+8)
563     7f/jump-if-greater  $is-digit?:end/disp8
564     # otherwise return true
565     b8/copy-to-EAX  1/imm32
566 $is-digit?:end:
567     # . epilog
568     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
569     5d/pop-to-EBP
570     c3/return
571 
572 == data
573 
574 Look: # (char)
575     00 00 00 00  # = 0
576 
577 _test-output-stream:
578     # current write index
579     00 00 00 00
580     # current read index
581     00 00 00 00
582     # length (= 8)
583     08 00 00 00
584     # data
585     00 00 00 00 00 00 00 00  # 8 bytes
586 
587 _test-error-stream:
588     # current write index
589     00 00 00 00
590     # current read index
591     00 00 00 00
592     # length (= 8)
593     08 00 00 00
594     # data
595     00 00 00 00 00 00 00 00  # 8 bytes
596 
597 # . . vim:nowrap:textwidth=0