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 == false) goto run-main
 61     3d/compare-eax-and  0/imm32/false
 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 : (ref 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 : (addr buffered-file), out : fd or (addr stream byte), err : fd or (addr stream byte), ed : (addr 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 : (ref stream byte 7)
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
191 # space in 'out'.
192 # Input comes from the global variable 'Look' (first byte) and the argument
193 # 'in' (rest). We leave the next byte from 'in' into 'Look' on exit.
194 get-num:  # in : (addr buffered-file), out : (addr stream byte), err : fd or (addr stream byte), ed : (addr exit-descriptor)
195     # pseudocode:
196     #   if (!is-digit?(Look)) expected(ed, err, "integer")
197     #   if out->write >= out->length
198     #     write(err, "Error: too many digits in number\n")
199     #     stop(ed, 1)
200     #   out->data[out->write] = LSB(Look)
201     #   ++out->write
202     #   Look = get-char(in)
203     #
204     # registers:
205     #   in: esi
206     #   out: edi
207     #   out->write: ecx (cached copy; need to keep in sync)
208     #   out->length: edx
209     #   temporaries: eax, ebx
210     # We can't allocate Look to a register because it gets written implicitly in
211     # get-char in each iteration of the loop. (Thereby demonstrating that it's
212     # not the right interface for us. But we'll keep it just to follow Crenshaw.)
213     #
214     # . prologue
215     55/push-ebp
216     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
217     # - if (is-digit?(Look)) expected(ed, err, "integer")
218     # . eax = is-digit?(Look)
219     # . . push args
220     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
221     # . . call
222     e8/call  is-digit?/disp32
223     # . . discard args
224     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
225     # . if (eax == false)
226     3d/compare-eax-and  0/imm32/false
227     75/jump-if-not-equal  $get-num:main/disp8
228     # . expected(ed, err, "integer")
229     # . . push args
230     68/push  "integer"/imm32
231     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
232     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
233     # . . call
234     e8/call  expected/disp32  # never returns
235     # . . discard args
236     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
237 $get-num:main:
238     # - otherwise read a digit
239     # . save registers
240     50/push-eax
241     51/push-ecx
242     52/push-edx
243     53/push-ebx
244     56/push-esi
245     57/push-edi
246     # read necessary variables to registers
247     # esi = in
248     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
249     # edi = out
250     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
251     # ecx = out->write
252     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
253     # edx = out->length
254     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(edi+8) to edx
255 $get-num:loop:
256     # if (out->write >= out->length) error
257     39/compare                      3/mod/direct    2/rm32/edx    .           .             .           1/r32/ecx   .               .                 # compare edx with ecx
258     7d/jump-if-lesser  $get-num:stage2/disp8
259     # . error(ed, err, msg)  # TODO: show full number
260     # . . push args
261     68/push  "get-num: too many digits in number"/imm32
262     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
263     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
264     # . . call
265     e8/call  error/disp32  # never returns
266     # . . discard args
267     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
268 $get-num:stage2:
269     # out->data[out->write] = LSB(Look)
270     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
271     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Look/disp32     .                 # copy *Look to eax
272     88/copy-byte                    0/mod/indirect  3/rm32/ebx    .           .             .           0/r32/AL    .               .                 # copy byte at AL to *ebx
273     # ++out->write
274     41/increment-ecx
275     # Look = get-char(in)
276     # . . push args
277     56/push-esi
278     # . . call
279     e8/call  get-char/disp32
280     # . . discard args
281     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
282 $get-num:loop-end:
283     # persist necessary variables from registers
284     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy ecx to *edi
285 $get-num:end:
286     # . restore registers
287     5f/pop-to-edi
288     5e/pop-to-esi
289     5b/pop-to-ebx
290     5a/pop-to-edx
291     59/pop-to-ecx
292     58/pop-to-eax
293     # . epilogue
294     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
295     5d/pop-to-ebp
296     c3/return
297 
298 test-get-num-reads-single-digit:
299     # - check that get-num returns first character if it's a digit
300     # This test uses exit-descriptors. Use ebp for setting up local variables.
301     55/push-ebp
302     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
303     # clear all streams
304     # . clear-stream(_test-stream)
305     # . . push args
306     68/push  _test-stream/imm32
307     # . . call
308     e8/call  clear-stream/disp32
309     # . . discard args
310     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
311     # . clear-stream($_test-buffered-file->buffer)
312     # . . push args
313     68/push  $_test-buffered-file->buffer/imm32
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 : (ref 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->buffer)
401     # . . push args
402     68/push  $_test-buffered-file->buffer/imm32
403     # . . call
404     e8/call  clear-stream/disp32
405     # . . discard args
406     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
407     # . clear-stream(_test-output-stream)
408     # . . push args
409     68/push  _test-output-stream/imm32
410     # . . call
411     e8/call  clear-stream/disp32
412     # . . discard args
413     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
414     # . clear-stream(_test-error-stream)
415     # . . push args
416     68/push  _test-error-stream/imm32
417     # . . call
418     e8/call  clear-stream/disp32
419     # . . discard args
420     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
421     # initialize 'in'
422     # . write(_test-stream, "3")
423     # . . push args
424     68/push  "3"/imm32
425     68/push  _test-stream/imm32
426     # . . call
427     e8/call  write/disp32
428     # . . discard args
429     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
430     # initialize exit-descriptor 'ed' for the call to 'get-num' below
431     # . var ed/eax : (ref exit-descriptor)
432     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
433     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
434     # . tailor-exit-descriptor(ed, 16)
435     # . . push args
436     68/push  0x10/imm32/nbytes-of-args-for-get-num
437     50/push-eax/ed
438     # . . call
439     e8/call  tailor-exit-descriptor/disp32
440     # . . discard args
441     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
442     # *don't* prime the pump
443     # get-num(in, out, err, ed)
444     # . . push args
445     50/push-eax/ed
446     68/push  _test-error-stream/imm32
447     68/push  _test-output-stream/imm32
448     68/push  _test-buffered-file/imm32
449     # . . call
450     e8/call  get-num/disp32
451     # registers except esp may be clobbered at this point
452     # . . discard args
453     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
454     # check that get-num tried to call exit(1)
455     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
456     # . . push args
457     68/push  "F - test-get-num-aborts-on-non-digit-in-Look"/imm32
458     68/push  2/imm32
459     # . . push ed->value
460     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           4/disp8         .                 # push *(eax+4)
461     # . . call
462     e8/call  check-ints-equal/disp32
463     # . . discard args
464     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
465     # . reclaim locals
466     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
467     5d/pop-to-ebp
468     c3/return
469 
470 ## helpers
471 
472 # write(f, "Error: "+s+" expected\n") then stop(ed, 1)
473 expected:  # ed : (addr exit-descriptor), f : fd or (addr stream byte), s : (addr array byte)
474     # . prologue
475     55/push-ebp
476     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
477     # write(f, "Error: ")
478     # . . push args
479     68/push  "Error: "/imm32
480     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
481     # . . call
482     e8/call  write/disp32
483     # . . discard args
484     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
485     # write(f, s)
486     # . . push args
487     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
488     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
489     # . . call
490     e8/call  write/disp32
491     # . . discard args
492     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
493     # write(f, " expected\n")
494     # . . push args
495     68/push  " expected\n"/imm32
496     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
497     # . . call
498     e8/call  write/disp32
499     # . . discard args
500     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
501     # stop(ed, 1)
502     # . . push args
503     68/push  1/imm32
504     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
505     # . . call
506     e8/call  stop/disp32
507     # should never get past this point
508 $expected:dead-end:
509     # . epilogue
510     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
511     5d/pop-to-ebp
512     c3/return
513 
514 # read a byte from 'f', and save it in 'Look'
515 get-char:  # f : (addr buffered-file)
516     # . prologue
517     55/push-ebp
518     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
519     # . save registers
520     50/push-eax
521     # eax = read-byte-buffered(f)
522     # . . push args
523     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
524     # . . call
525     e8/call  read-byte-buffered/disp32
526     # . . discard args
527     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
528     # save eax to Look
529     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Look/disp32     .                 # copy eax to *Look
530 $get-char:end:
531     # . restore registers
532     58/pop-to-eax
533     # . epilogue
534     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
535     5d/pop-to-ebp
536     c3/return
537 
538 is-digit?:  # c : int -> eax : boolean
539     # . prologue
540     55/push-ebp
541     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
542     # eax = false
543     b8/copy-to-eax  0/imm32
544     # if (c < '0') return false
545     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x30/imm32        # compare *(ebp+8)
546     7c/jump-if-lesser  $is-digit?:end/disp8
547     # if (c > '9') return false
548     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x39/imm32        # compare *(ebp+8)
549     7f/jump-if-greater  $is-digit?:end/disp8
550     # otherwise return true
551     b8/copy-to-eax  1/imm32
552 $is-digit?:end:
553     # . epilogue
554     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
555     5d/pop-to-ebp
556     c3/return
557 
558 == data
559 
560 Look:  # (char with some extra padding)
561     0/imm32
562 
563 # . . vim:nowrap:textwidth=0