https://github.com/akkartik/mu/blob/master/apps/crenshaw2-1b.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 numbers of multiple digits.
  4 #
  5 # To run:
  6 #   $ ./subx translate 0*.subx apps/crenshaw2-1b.subx -o apps/crenshaw2-1b
  7 #   $ echo '1a'  |./subx run apps/crenshaw2-1b
  8 # Expected output:
  9 #   # syscall(exit, 1a)
 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 '1a'  |./subx run apps/crenshaw2-1b > z1.subx
 16 #   $ ./subx translate z1.subx -o z1
 17 #   $ ./subx run z1
 18 #   $ echo $?
 19 #   26  # 0x1a in decimal
 20 #
 21 # Stdin must contain just a single hex digit. Other input will print an error:
 22 #   $ echo 'xyz'  |./subx run apps/crenshaw2-1b
 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     # . prolog
 35     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 36 
 37     # initialize heap
 38     # . Heap = new-segment(64KB)
 39     # . . push args
 40     68/push  Heap/imm32
 41     68/push  0x10000/imm32/64KB
 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) -> <void>
 94     # . prolog
 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     # . epilog
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 sequence of digits into 'out'. Abort if there are none, or if there is
191 # no 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 : (address buffered-file), out : (address stream), err : fd or (address stream), ed : (address exit-descriptor) -> <void>
195     # pseudocode:
196     #   if (!is-digit?(Look)) expected(ed, err, "integer")
197     #   do
198     #     if out->write >= out->length
199     #       write(err, "Error: too many digits in number\n")
200     #       stop(ed, 1)
201     #     out->data[out->write] = LSB(Look)
202     #     ++out->write
203     #     Look = get-char(in)
204     #   while is-digit?(Look)
205     # This is complicated because I don't want to hard-code the error strategy in
206     # a general helper like write-byte-buffered. Maybe I should just create a
207     # local helper.
208     #
209     # within the loop we'll try to keep things in registers:
210     #   in: esi
211     #   out: edi
212     #   out->write: ecx (cached copy; need to keep in sync)
213     #   out->length: edx
214     #   temporaries: eax, ebx
215     # We can't allocate Look to a register because it gets written implicitly in
216     # get-char in each iteration of the loop. (Thereby demonstrating that it's
217     # not the right interface for us. But we'll keep it just to follow Crenshaw.)
218     #
219     # . prolog
220     55/push-ebp
221     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
222     # - if (is-digit?(Look)) expected(ed, err, "integer")
223     # . eax = is-digit?(Look)
224     # . . push args
225     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
226     # . . call
227     e8/call  is-digit?/disp32
228     # . . discard args
229     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
230     # . if (eax == 0)
231     3d/compare-eax-and  0/imm32
232     75/jump-if-not-equal  $get-num:main/disp8
233     # . expected(ed, err, "integer")
234     # . . push args
235     68/push  "integer"/imm32
236     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
237     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
238     # . . call
239     e8/call  expected/disp32  # never returns
240     # . . discard args
241     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
242 $get-num:main:
243     # - otherwise read a digit
244     # . save registers
245     50/push-eax
246     51/push-ecx
247     52/push-edx
248     53/push-ebx
249     56/push-esi
250     57/push-edi
251     # read necessary variables to registers
252     # esi = in
253     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
254     # edi = out
255     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   0xc/disp8       .                 # copy *(ebp+12) to edi
256     # ecx = out->write
257     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
258     # edx = out->length
259     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           2/r32/edx   8/disp8         .                 # copy *(edi+8) to edx
260 $get-num:loop:
261     # if (out->write >= out->length) error
262     39/compare                      3/mod/direct    2/rm32/edx    .           .             .           1/r32/ecx   .               .                 # compare edx with ecx
263     7d/jump-if-lesser  $get-num:loop-stage2/disp8
264     # . error(ed, err, msg)  # TODO: show full number
265     # . . push args
266     68/push  "get-num: too many digits in number"/imm32
267     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
268     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
269     # . . call
270     e8/call  error/disp32  # never returns
271     # . . discard args
272     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
273 $get-num:loop-stage2:
274     # out->data[out->write] = LSB(Look)
275     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  1/index/ecx   .           3/r32/ebx   0xc/disp8       .                 # copy edi+ecx+12 to ebx
276     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Look/disp32     .                 # copy *Look to eax
277     88/copy-byte                    0/mod/indirect  3/rm32/ebx    .           .             .           0/r32/AL    .               .                 # copy byte at AL to *ebx
278     # ++out->write
279     41/increment-ecx
280     # Look = get-char(in)
281     # . . push args
282     56/push-esi
283     # . . call
284     e8/call  get-char/disp32
285     # . . discard args
286     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
287     # if (is-digit?(Look)) loop
288     # . eax = is-digit?(Look)
289     # . . push args
290     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
291     # . . call
292     e8/call  is-digit?/disp32
293     # . . discard args
294     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
295     # . if (eax != 0) loop
296     3d/compare-eax-and  0/imm32
297     0f 85/jump-if-not-equal  $get-num:loop/disp32
298 $get-num:loop-end:
299     # persist necessary variables from registers
300     89/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy ecx to *edi
301 $get-num:end:
302     # . restore registers
303     5f/pop-to-edi
304     5e/pop-to-esi
305     5b/pop-to-ebx
306     5a/pop-to-edx
307     59/pop-to-ecx
308     58/pop-to-eax
309     # . epilog
310     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
311     5d/pop-to-ebp
312     c3/return
313 
314 test-get-num-reads-single-digit:
315     # - check that get-num returns first character if it's a digit
316     # This test uses exit-descriptors. Use ebp for setting up local variables.
317     55/push-ebp
318     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
319     # clear all streams
320     # . clear-stream(_test-stream)
321     # . . push args
322     68/push  _test-stream/imm32
323     # . . call
324     e8/call  clear-stream/disp32
325     # . . discard args
326     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
327     # . clear-stream(_test-buffered-file+4)
328     # . . push args
329     b8/copy-to-eax  _test-buffered-file/imm32
330     05/add-to-eax  4/imm32
331     50/push-eax
332     # . . call
333     e8/call  clear-stream/disp32
334     # . . discard args
335     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
336     # . clear-stream(_test-output-stream)
337     # . . push args
338     68/push  _test-output-stream/imm32
339     # . . call
340     e8/call  clear-stream/disp32
341     # . . discard args
342     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
343     # . clear-stream(_test-error-stream)
344     # . . push args
345     68/push  _test-error-stream/imm32
346     # . . call
347     e8/call  clear-stream/disp32
348     # . . discard args
349     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
350     # initialize 'in'
351     # . write(_test-stream, "3")
352     # . . push args
353     68/push  "3"/imm32
354     68/push  _test-stream/imm32
355     # . . call
356     e8/call  write/disp32
357     # . . discard args
358     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
359     # initialize exit-descriptor 'ed' for the call to 'get-num' below
360     # . var ed/eax : exit-descriptor
361     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
362     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
363     # . tailor-exit-descriptor(ed, 16)
364     # . . push args
365     68/push  0x10/imm32/nbytes-of-args-for-get-num
366     50/push-eax/ed
367     # . . call
368     e8/call  tailor-exit-descriptor/disp32
369     # . . discard args
370     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
371     # prime the pump
372     # . get-char(_test-buffered-file)
373     # . . push args
374     68/push  _test-buffered-file/imm32
375     # . . call
376     e8/call  get-char/disp32
377     # . . discard args
378     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
379     # get-num(in, out, err, ed)
380     # . . push args
381     50/push-eax/ed
382     68/push  _test-error-stream/imm32
383     68/push  _test-output-stream/imm32
384     68/push  _test-buffered-file/imm32
385     # . . call
386     e8/call  get-num/disp32
387     # registers except esp may be clobbered at this point
388     # . . discard args
389     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
390     # check-ints-equal(*_test-output-stream->data, '3', msg)
391     # . . push args
392     68/push  "F - test-get-num-reads-single-digit"/imm32
393     68/push  0x33/imm32
394     b8/copy-to-eax  _test-output-stream/imm32
395     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
396     # . . call
397     e8/call  check-ints-equal/disp32
398     # . . discard args
399     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
400     # . reclaim locals
401     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
402     5d/pop-to-ebp
403     c3/return
404 
405 test-get-num-aborts-on-non-digit-in-Look:
406     # - check that get-num returns first character if it's a digit
407     # This test uses exit-descriptors. Use ebp for setting up local variables.
408     55/push-ebp
409     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
410     # clear all streams
411     # . clear-stream(_test-stream)
412     # . . push args
413     68/push  _test-stream/imm32
414     # . . call
415     e8/call  clear-stream/disp32
416     # . . discard args
417     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
418     # . clear-stream(_test-buffered-file+4)
419     # . . push args
420     b8/copy-to-eax  _test-buffered-file/imm32
421     05/add-to-eax  4/imm32
422     50/push-eax
423     # . . call
424     e8/call  clear-stream/disp32
425     # . . discard args
426     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
427     # . clear-stream(_test-output-stream)
428     # . . push args
429     68/push  _test-output-stream/imm32
430     # . . call
431     e8/call  clear-stream/disp32
432     # . . discard args
433     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
434     # . clear-stream(_test-error-stream)
435     # . . push args
436     68/push  _test-error-stream/imm32
437     # . . call
438     e8/call  clear-stream/disp32
439     # . . discard args
440     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
441     # initialize 'in'
442     # . write(_test-stream, "3")
443     # . . push args
444     68/push  "3"/imm32
445     68/push  _test-stream/imm32
446     # . . call
447     e8/call  write/disp32
448     # . . discard args
449     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
450     # initialize exit-descriptor 'ed' for the call to 'get-num' below
451     # . var ed/eax : (address exit-descriptor)
452     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
453     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
454     # . tailor-exit-descriptor(ed, 16)
455     # . . push args
456     68/push  0x10/imm32/nbytes-of-args-for-get-num
457     50/push-eax/ed
458     # . . call
459     e8/call  tailor-exit-descriptor/disp32
460     # . . discard args
461     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
462     # *don't* prime the pump
463     # get-num(in, out, err, ed)
464     # . . push args
465     50/push-eax/ed
466     68/push  _test-error-stream/imm32
467     68/push  _test-output-stream/imm32
468     68/push  _test-buffered-file/imm32
469     # . . call
470     e8/call  get-num/disp32
471     # registers except esp may be clobbered at this point
472     # . . discard args
473     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
474     # check that get-num tried to call exit(1)
475     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
476     # . . push args
477     68/push  "F - test-get-num-aborts-on-non-digit-in-Look"/imm32
478     68/push  2/imm32
479     # . . push ed->value
480     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           4/disp8         .                 # push *(eax+4)
481     # . . call
482     e8/call  check-ints-equal/disp32
483     # . . discard args
484     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
485     # . reclaim locals
486     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
487     5d/pop-to-ebp
488     c3/return
489 
490 test-get-num-reads-multiple-digits:
491     # - check that get-num returns all initial digits until it encounters a non-digit
492     # This test uses exit-descriptors. Use ebp for setting up local variables.
493     55/push-ebp
494     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
495     # clear all streams
496     # . clear-stream(_test-stream)
497     # . . push args
498     68/push  _test-stream/imm32
499     # . . call
500     e8/call  clear-stream/disp32
501     # . . discard args
502     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
503     # . clear-stream(_test-buffered-file+4)
504     # . . push args
505     b8/copy-to-eax  _test-buffered-file/imm32
506     05/add-to-eax  4/imm32
507     50/push-eax
508     # . . call
509     e8/call  clear-stream/disp32
510     # . . discard args
511     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
512     # . clear-stream(_test-output-stream)
513     # . . push args
514     68/push  _test-output-stream/imm32
515     # . . call
516     e8/call  clear-stream/disp32
517     # . . discard args
518     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
519     # . clear-stream(_test-error-stream)
520     # . . push args
521     68/push  _test-error-stream/imm32
522     # . . call
523     e8/call  clear-stream/disp32
524     # . . discard args
525     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
526     # initialize 'in'
527     # . write(_test-stream, "3456 x")
528     # . . push args
529     68/push  "3456"/imm32
530     68/push  _test-stream/imm32
531     # . . call
532     e8/call  write/disp32
533     # . . discard args
534     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
535     # initialize exit-descriptor 'ed' for the call to 'get-num' below
536     # . var ed/eax : (address exit-descriptor)
537     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
538     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
539     # . tailor-exit-descriptor(ed, 16)
540     # . . push args
541     68/push  0x10/imm32/nbytes-of-args-for-get-num
542     50/push-eax/ed
543     # . . call
544     e8/call  tailor-exit-descriptor/disp32
545     # . . discard args
546     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
547     # prime the pump
548     # . get-char(_test-buffered-file)
549     # . . push args
550     68/push  _test-buffered-file/imm32
551     # . . call
552     e8/call  get-char/disp32
553     # . . discard args
554     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
555     # get-num(in, out, err, ed)
556     # . . push args
557     50/push-eax/ed
558     68/push  _test-error-stream/imm32
559     68/push  _test-output-stream/imm32
560     68/push  _test-buffered-file/imm32
561     # . . call
562     e8/call  get-num/disp32
563     # registers except esp may be clobbered at this point
564     # . . discard args
565     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
566     # check-ints-equal(*_test-output-stream->data, '3456', msg)
567     # . . push args
568     68/push  "F - test-get-num-reads-multiple-digits"/imm32
569     68/push  0x36353433/imm32
570     b8/copy-to-eax  _test-output-stream/imm32
571     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
572     # . . call
573     e8/call  check-ints-equal/disp32
574     # . . discard args
575     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
576     # . reclaim locals
577     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
578     5d/pop-to-ebp
579     c3/return
580 
581 test-get-num-reads-multiple-digits-followed-by-nondigit:
582     # - check that get-num returns all initial digits until it encounters a non-digit
583     # This test uses exit-descriptors. Use ebp for setting up local variables.
584     55/push-ebp
585     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
586     # clear all streams
587     # . clear-stream(_test-stream)
588     # . . push args
589     68/push  _test-stream/imm32
590     # . . call
591     e8/call  clear-stream/disp32
592     # . . discard args
593     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
594     # . clear-stream(_test-buffered-file+4)
595     # . . push args
596     b8/copy-to-eax  _test-buffered-file/imm32
597     05/add-to-eax  4/imm32
598     50/push-eax
599     # . . call
600     e8/call  clear-stream/disp32
601     # . . discard args
602     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
603     # . clear-stream(_test-output-stream)
604     # . . push args
605     68/push  _test-output-stream/imm32
606     # . . call
607     e8/call  clear-stream/disp32
608     # . . discard args
609     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
610     # . clear-stream(_test-error-stream)
611     # . . push args
612     68/push  _test-error-stream/imm32
613     # . . call
614     e8/call  clear-stream/disp32
615     # . . discard args
616     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
617     # initialize 'in'
618     # . write(_test-stream, "3456 x")
619     # . . push args
620     68/push  "3456 x"/imm32
621     68/push  _test-stream/imm32
622     # . . call
623     e8/call  write/disp32
624     # . . discard args
625     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
626     # initialize exit-descriptor 'ed' for the call to 'get-num' below
627     # . var ed/eax : (address exit-descriptor)
628     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # subtract from esp
629     89/copy                         3/mod/direct    0/rm32/eax    .           .             .           4/r32/esp   .               .                 # copy esp to eax
630     # . tailor-exit-descriptor(ed, 16)
631     # . . push args
632     68/push  0x10/imm32/nbytes-of-args-for-get-num
633     50/push-eax/ed
634     # . . call
635     e8/call  tailor-exit-descriptor/disp32
636     # . . discard args
637     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
638     # prime the pump
639     # . get-char(_test-buffered-file)
640     # . . push args
641     68/push  _test-buffered-file/imm32
642     # . . call
643     e8/call  get-char/disp32
644     # . . discard args
645     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
646     # get-num(in, out, err, ed)
647     # . . push args
648     50/push-eax/ed
649     68/push  _test-error-stream/imm32
650     68/push  _test-output-stream/imm32
651     68/push  _test-buffered-file/imm32
652     # . . call
653     e8/call  get-num/disp32
654     # registers except esp may be clobbered at this point
655     # . . discard args
656     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
657     # check-ints-equal(*_test-output-stream->data, '3456', msg)
658     # . . push args
659     68/push  "F - test-get-num-reads-multiple-digits-followed-by-nondigit"/imm32
660     68/push  0x36353433/imm32
661     b8/copy-to-eax  _test-output-stream/imm32
662     ff          6/subop/push        1/mod/*+disp8   0/rm32/eax    .           .             .           .           0xc/disp8       .                 # push *(eax+12)
663     # . . call
664     e8/call  check-ints-equal/disp32
665     # . . discard args
666     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
667     # . reclaim locals
668     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
669     5d/pop-to-ebp
670     c3/return
671 
672 ## helpers
673 
674 # write(f, "Error: "+s+" expected\n") then stop(ed, 1)
675 expected:  # ed : (address exit-descriptor), f : fd or (address stream), s : (address array byte) -> <void>
676     # . prolog
677     55/push-ebp
678     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
679     # write(f, "Error: ")
680     # . . push args
681     68/push  "Error: "/imm32
682     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
683     # . . call
684     e8/call  write/disp32
685     # . . discard args
686     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
687     # write(f, s)
688     # . . push args
689     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x10/disp8      .                 # push *(ebp+16)
690     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
691     # . . call
692     e8/call  write/disp32
693     # . . discard args
694     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
695     # write(f, " expected\n")
696     # . . push args
697     68/push  " expected\n"/imm32
698     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
699     # . . call
700     e8/call  write/disp32
701     # . . discard args
702     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
703     # stop(ed, 1)
704     # . . push args
705     68/push  1/imm32
706     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
707     # . . call
708     e8/call  stop/disp32
709     # should never get past this point
710 $expected:dead-end:
711     # . epilog
712     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
713     5d/pop-to-ebp
714     c3/return
715 
716 # read a byte from 'f', and save it in 'Look'
717 get-char:  # f : (address buffered-file) -> <void>
718     # . prolog
719     55/push-ebp
720     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
721     # . save registers
722     50/push-eax
723     # eax = read-byte-buffered(f)
724     # . . push args
725     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
726     # . . call
727     e8/call  read-byte-buffered/disp32
728     # . . discard args
729     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
730     # save eax to Look
731     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/eax   Look/disp32     .                 # copy eax to *Look
732 $get-char:end:
733     # . restore registers
734     58/pop-to-eax
735     # . epilog
736     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
737     5d/pop-to-ebp
738     c3/return
739 
740 is-digit?:  # c : int -> eax : boolean
741     # . prolog
742     55/push-ebp
743     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
744     # eax = false
745     b8/copy-to-eax  0/imm32
746     # if (c < '0') return false
747     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x30/imm32        # compare *(ebp+8)
748     7c/jump-if-lesser  $is-digit?:end/disp8
749     # if (c > '9') return false
750     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x39/imm32        # compare *(ebp+8)
751     7f/jump-if-greater  $is-digit?:end/disp8
752     # otherwise return true
753     b8/copy-to-eax  1/imm32
754 $is-digit?:end:
755     # . epilog
756     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
757     5d/pop-to-ebp
758     c3/return
759 
760 == data
761 
762 Look:  # (char with some extra padding)
763     0/imm32
764 
765 # . . vim:nowrap:textwidth=0