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     68/push  Heap-size/imm32
 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+4)
310     # . . push args
311     b8/copy-to-eax  _test-buffered-file/imm32
312     05/add-to-eax  4/imm32
313     50/push-eax
314     # . . call
315     e8/call  clear-stream/disp32
316     # . . discard args
317     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
318     # . clear-stream(_test-output-stream)
319     # . . push args
320     68/push  _test-output-stream/imm32
321     # . . call
322     e8/call  clear-stream/disp32
323     # . . discard args
324     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
325     # . clear-stream(_test-error-stream)
326     # . . push args
327     68/push  _test-error-stream/imm32
328     # . . call
329     e8/call  clear-stream/disp32
330     # . . discard args
331     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
332     # initialize 'in'
333     # . write(_test-stream, "3")
334     # . . push args
335     68/push  "3"/imm32
336     68/push  _test-stream/imm32
337     # . . call
338     e8/call  write/disp32
339     # . . discard args
340     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
341     # initialize exit-descriptor 'ed' for the call to 'get-num' below
342     # . var ed/eax : exit-descriptor
343     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
344     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
345     # . tailor-exit-descriptor(ed, 16)
346     # . . push args
347     68/push  0x10/imm32/nbytes-of-args-for-get-num
348     50/push-eax/ed
349     # . . call
350     e8/call  tailor-exit-descriptor/disp32
351     # . . discard args
352     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
353     # prime the pump
354     # . get-char(_test-buffered-file)
355     # . . push args
356     68/push  _test-buffered-file/imm32
357     # . . call
358     e8/call  get-char/disp32
359     # . . discard args
360     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
361     # get-num(in, out, err, ed)
362     # . . push args
363     50/push-eax/ed
364     68/push  _test-error-stream/imm32
365     68/push  _test-output-stream/imm32
366     68/push  _test-buffered-file/imm32
367     # . . call
368     e8/call  get-num/disp32
369     # registers except esp may be clobbered at this point
370     # . . discard args
371     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
372     # check-ints-equal(*_test-output-stream->data, '3', msg)
373     # . . push args
374     68/push  "F - test-get-num-reads-single-digit"/imm32
375     68/push  0x33/imm32
376     b8/copy-to-eax  _test-output-stream/imm32
377     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
378     # . . call
379     e8/call  check-ints-equal/disp32
380     # . . discard args
381     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
382     # . reclaim locals
383     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
384     5d/pop-to-ebp
385     c3/return
386 
387 test-get-num-aborts-on-non-digit-in-Look:
388     # - check that get-num returns first character if it's a digit
389     # This test uses exit-descriptors. Use ebp for setting up local variables.
390     55/push-ebp
391     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
392     # clear all streams
393     # . clear-stream(_test-stream)
394     # . . push args
395     68/push  _test-stream/imm32
396     # . . call
397     e8/call  clear-stream/disp32
398     # . . discard args
399     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
400     # . clear-stream(_test-buffered-file+4)
401     # . . push args
402     b8/copy-to-eax  _test-buffered-file/imm32
403     05/add-to-eax  4/imm32
404     50/push-eax
405     # . . call
406     e8/call  clear-stream/disp32
407     # . . discard args
408     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
409     # . clear-stream(_test-output-stream)
410     # . . push args
411     68/push  _test-output-stream/imm32
412     # . . call
413     e8/call  clear-stream/disp32
414     # . . discard args
415     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
416     # . clear-stream(_test-error-stream)
417     # . . push args
418     68/push  _test-error-stream/imm32
419     # . . call
420     e8/call  clear-stream/disp32
421     # . . discard args
422     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
423     # initialize 'in'
424     # . write(_test-stream, "3")
425     # . . push args
426     68/push  "3"/imm32
427     68/push  _test-stream/imm32
428     # . . call
429     e8/call  write/disp32
430     # . . discard args
431     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
432     # initialize exit-descriptor 'ed' for the call to 'get-num' below
433     # . var ed/eax : (address exit-descriptor)
434     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
435     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
436     # . tailor-exit-descriptor(ed, 16)
437     # . . push args
438     68/push  0x10/imm32/nbytes-of-args-for-get-num
439     50/push-eax/ed
440     # . . call
441     e8/call  tailor-exit-descriptor/disp32
442     # . . discard args
443     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
444     # *don't* prime the pump
445     # get-num(in, out, err, ed)
446     # . . push args
447     50/push-eax/ed
448     68/push  _test-error-stream/imm32
449     68/push  _test-output-stream/imm32
450     68/push  _test-buffered-file/imm32
451     # . . call
452     e8/call  get-num/disp32
453     # registers except esp may be clobbered at this point
454     # . . discard args
455     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
456     # check that get-num tried to call exit(1)
457     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
458     # . . push args
459     68/push  "F - test-get-num-aborts-on-non-digit-in-Look"/imm32
460     68/push  2/imm32
461     # . . push ed->value
462     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           4/disp8         .                 # push *(eax+4)
463     # . . call
464     e8/call  check-ints-equal/disp32
465     # . . discard args
466     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
467     # . reclaim locals
468     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
469     5d/pop-to-ebp
470     c3/return
471 
472 ## helpers
473 
474 # write(f, "Error: "+s+" expected\n") then stop(ed, 1)
475 expected:  # ed : (address exit-descriptor), f : fd or (address stream), s : (address array byte)
476     # . prologue
477     55/push-ebp
478     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
479     # write(f, "Error: ")
480     # . . push args
481     68/push  "Error: "/imm32
482     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
483     # . . call
484     e8/call  write/disp32
485     # . . discard args
486     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
487     # write(f, s)
488     # . . push args
489     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
490     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
491     # . . call
492     e8/call  write/disp32
493     # . . discard args
494     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
495     # write(f, " expected")
496     # . . push args
497     68/push  " expected\n"/imm32
498     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
499     # . . call
500     e8/call  write/disp32
501     # . . discard args
502     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
503     # stop(ed, 1)
504     # . . push args
505     68/push  1/imm32
506     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
507     # . . call
508     e8/call  stop/disp32
509     # should never get past this point
510 $expected:dead-end:
511     # . epilogue
512     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
513     5d/pop-to-ebp
514     c3/return
515 
516 # read a byte from 'f', and save it in 'Look'
517 get-char:  # f : (address buffered-file)
518     # . prologue
519     55/push-ebp
520     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
521     # . save registers
522     50/push-eax
523     # eax = read-byte-buffered(f)
524     # . . push args
525     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
526     # . . call
527     e8/call  read-byte-buffered/disp32
528     # . . discard args
529     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
530     # save eax to Look
531     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Look/disp32     .                 # copy eax to *Look
532 $get-char:end:
533     # . restore registers
534     58/pop-to-eax
535     # . epilogue
536     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
537     5d/pop-to-ebp
538     c3/return
539 
540 is-digit?:  # c : int -> eax : boolean
541     # . prologue
542     55/push-ebp
543     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
544     # eax = false
545     b8/copy-to-eax  0/imm32
546     # if (c < '0') return false
547     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x30/imm32        # compare *(ebp+8)
548     7c/jump-if-lesser  $is-digit?:end/disp8
549     # if (c > '9') return false
550     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x39/imm32        # compare *(ebp+8)
551     7f/jump-if-greater  $is-digit?:end/disp8
552     # otherwise return true
553     b8/copy-to-eax  1/imm32
554 $is-digit?:end:
555     # . epilogue
556     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
557     5d/pop-to-ebp
558     c3/return
559 
560 == data
561 
562 Look:  # (char with some extra padding)
563     0/imm32
564 
565 # . . vim:nowrap:textwidth=0