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