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