https://github.com/akkartik/mu/blob/master/subx/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 (from the subx/ directory):
  6 #   $ ./subx translate *.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 #?     # for debugging: run a single test; don't bother setting status code
 34 #?     e8/call test-get-num-reads-single-digit/disp32
 35 #?     eb/jump  $main:end/disp8
 36 
 37 # main: run tests if necessary, call 'compile' if not
 38     # . prolog
 39     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 40     # - if argc > 1 and argv[1] == "test" then return run_tests()
 41     # . argc > 1
 42     81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0/disp8         1/imm32           # compare *EBP
 43     7e/jump-if-lesser-or-equal  $run-main/disp8
 44     # . argv[1] == "test"
 45     # . . push args
 46     68/push  "test"/imm32
 47     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       .                 # push *(EBP+8)
 48     # . . call
 49     e8/call  kernel-string-equal/disp32
 50     # . . discard args
 51     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 52     # . check result
 53     3d/compare-EAX  1/imm32
 54     75/jump-if-not-equal  $run-main/disp8
 55     # . run-tests()
 56     e8/call  run-tests/disp32
 57     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 58     eb/jump  $main:end/disp8
 59 $run-main:
 60     # - otherwise read a program from stdin and emit its translation to stdout
 61     # var ed/EAX : exit-descriptor
 62     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
 63     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
 64     # configure ed to really exit()
 65     # . ed->target = 0
 66     c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
 67     # return compile(Stdin, 1/stdout, 2/stderr, ed)
 68     # . . push args
 69     50/push-EAX/ed
 70     68/push  2/imm32/stderr
 71     68/push  1/imm32/stdout
 72     68/push  Stdin/imm32
 73     # . . call
 74     e8/call  compile/disp32
 75     # . . discard args
 76     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
 77     # . syscall(exit, 0)
 78     bb/copy-to-EBX  0/imm32
 79 $main:end:
 80     b8/copy-to-EAX  1/imm32/exit
 81     cd/syscall  0x80/imm8
 82 
 83 # the main entry point
 84 compile:  # in : (address buffered-file), out : fd or (address stream), err : fd or (address stream), ed : (address exit-descriptor) -> <void>
 85     # . prolog
 86     55/push-EBP
 87     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 88     # . save registers
 89     50/push-EAX
 90     51/push-ECX
 91     # prime the pump
 92     # . Look = get-char(in)
 93     # . . push args
 94     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8      .                    # push *(EBP+8)
 95     # . . call
 96     e8/call  get-char/disp32
 97     # . . discard args
 98     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 99     # var num/ECX : (address stream) on the stack
100     # Numbers can be 32 bits or 8 hex bytes long. One of them will be in 'Look', so we need space for 7 bytes.
101     # Sizing the stream just right buys us overflow-handling for free inside 'get-num'.
102     # Add 12 bytes for 'read', 'write' and 'length' fields, for a total of 19 bytes, or 0x13 in hex.
103     # The stack pointer is no longer aligned, so dump_stack() can be misleading past this point.
104     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x13/imm32        # subtract from ESP
105     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   .               .                 # copy ESP to ECX
106     # initialize the stream
107     # . num->length = 7
108     c7/copy                         1/mod/*+disp8   1/rm32/ECX    .           .             .           .           8/disp8         7/imm32           # copy to *(ECX+8)
109     # . clear-stream(num)
110     # . . push args
111     51/push-ECX
112     # . . call
113     e8/call  clear-stream/disp32
114     # . . discard args
115     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
116     # read a digit from 'in' into 'num'
117     # . get-num(in, num, err, ed)
118     # . . push args
119     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x14/disp8      .                 # push *(EBP+20)
120     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
121     51/push-ECX/num
122     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8      .                    # push *(EBP+8)
123     # . . call
124     e8/call  get-num/disp32
125     # . . discard args
126     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
127     # render 'num' into the following template on 'out':
128     #   bb/copy-to-EBX  _num_
129     #   b8/copy-to-EAX  1/imm32/exit
130     #   cd/syscall  0x80/imm8
131     #
132     # . EAX = write(out, "bb/copy-to-EBX  ")
133     # . . push args
134     68/push  "bb/copy-to-EBX  "/imm32
135     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
136     # . . call
137     e8/call  write/disp32
138     # . . discard args
139     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
140     # . write-stream(out, num)
141     # . . push args
142     51/push-ECX/num
143     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
144     # . . call
145     e8/call  write-stream/disp32
146     # . . discard args
147     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
148     # . write(out, Newline)
149     # . . push args
150     68/push  Newline/imm32
151     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
152     # . . call
153     e8/call  write/disp32
154     # . . discard args
155     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
156     # . write(out, "b8/copy-to-EAX  1/imm32/exit")
157     # . . push args
158     68/push  "b8/copy-to-EAX  1/imm32/exit"/imm32
159     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
160     # . . call
161     e8/call  write/disp32
162     # . . discard args
163     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
164     # . write(out, Newline)
165     # . . push args
166     68/push  Newline/imm32
167     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
168     # . . call
169     e8/call  write/disp32
170     # . . discard args
171     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
172     # . write(out, "cd/syscall  0x80/imm8")
173     # . . push args
174     68/push  "cd/syscall  0x80/imm8"/imm32
175     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
176     # . . call
177     e8/call  write/disp32
178     # . . discard args
179     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
180     # . write(out, Newline)
181     # . . push args
182     68/push  Newline/imm32
183     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
184     # . . call
185     e8/call  write/disp32
186     # . . discard args
187     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
188     # . restore registers
189     59/pop-to-ECX
190     58/pop-to-EAX
191     # . epilog
192     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
193     5d/pop-to-EBP
194     c3/return
195 
196 # Read a sequence of digits into 'out'. Abort if there are none, or if there is
197 # no space in 'out'.
198 # Input comes from the global variable 'Look' (first byte) and the argument
199 # 'in' (rest). We leave the next byte from 'in' into 'Look' on exit.
200 get-num:  # in : (address buffered-file), out : (address stream), err : fd or (address stream), ed : (address exit-descriptor) -> <void>
201     # pseudocode:
202     #   if !is-digit?(Look) expected(ed, err, "integer")
203     #   do
204     #     if out->write >= out->length
205     #       write(err, "Error: too many digits in number\n")
206     #       stop(ed, 1)
207     #     out->data[out->write] = LSB(Look)
208     #     ++out->write
209     #     Look = get-char(in)
210     #   while is-digit?(Look)
211     # This is complicated because I don't want to hard-code the error strategy in
212     # a general helper like write-byte. Maybe I should just create a local helper.
213     #
214     # within the loop we'll try to keep things in registers:
215     #   ESI : in
216     #   EDI : out
217     #   EAX : temp
218     #   ECX : out->write
219     #   EDX : out->length
220     #   EBX : temp2
221     # We can't allocate Look to a register because it gets written implicitly in
222     # get-char in each iteration of the loop. (Thereby demonstrating that it's
223     # not the right interface for us. But we'll keep it just to follow Crenshaw.)
224     #
225     # . prolog
226     55/push-EBP
227     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
228     # - if is-digit?(Look) expected(ed, err, "integer")
229     # . EAX = is-digit?(Look)
230     # . . push args
231     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
232     # . . call
233     e8/call  is-digit?/disp32
234     # . . discard args
235     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
236     # . if EAX == 0
237     3d/compare-EAX  0/imm32
238     75/jump-if-not-equal  $get-num:main/disp8
239     # . expected(ed, err, "integer")
240     # . . push args
241     68/push  "integer"/imm32
242     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
243     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x14/disp8      .                 # push *(EBP+20)
244     # . . call
245     e8/call  expected/disp32  # never returns
246     # . . discard args
247     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
248 $get-num:main:
249     # - otherwise read a digit
250     # . save registers
251     50/push-EAX
252     51/push-ECX
253     52/push-EDX
254     53/push-EBX
255     56/push-ESI
256     57/push-EDI
257     # read necessary variables to registers
258     # ESI = in
259     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
260     # EDI = out
261     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           7/r32/EDI   0xc/disp8       .                 # copy *(EBP+12) to EDI
262     # ECX = out->write
263     8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
264     # EDX = out->length
265     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   8/disp8         .                 # copy *(EDI+8) to EDX
266 $get-num:loop:
267     # if out->write >= out->length error
268     3b/compare                      3/mod/direct    1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # compare EDX with ECX
269     7d/jump-if-lesser  $get-num:loop-stage2/disp8
270     # . error(ed, err, msg)  # TODO: show full number
271     # . . push args
272     68/push  "get-num: too many digits in number"/imm32
273     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
274     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x14/disp8      .                 # push *(EBP+20)
275     # . . call
276     e8/call  error/disp32  # never returns
277     # . . discard args
278     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
279 $get-num:loop-stage2:
280     # out->data[out->write] = LSB(Look)
281     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
282     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Look/disp32     .                 # copy *Look to EAX
283     88/copy-byte                    0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/AL    .               .                 # copy byte at AL to *EBX
284     # ++out->write
285     41/increment-ECX
286     # Look = get-char(in)
287     # . . push args
288     56/push-ESI
289     # . . call
290     e8/call  get-char/disp32
291     # . . discard args
292     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
293     # if is-digit?(Look) loop
294     # . EAX = is-digit?(Look)
295     # . . push args
296     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Look/disp32     .                 # push *Look
297     # . . call
298     e8/call  is-digit?/disp32
299     # . . discard args
300     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
301     # . if EAX loop
302     3d/compare-EAX  0/imm32
303     0f 85/jump-if-not-equal  $get-num:loop/disp32
304 $get-num:loop-end:
305     # persist necessary variables from registers
306     89/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy ECX to *EDI
307     # . restore registers
308     5f/pop-to-EDI
309     5e/pop-to-ESI
310     5b/pop-to-EBX
311     5a/pop-to-EDX
312     59/pop-to-ECX
313     58/pop-to-EAX
314     # . epilog
315     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
316     5d/pop-to-EBP
317     c3/return
318 
319 test-get-num-reads-single-digit:
320     # - check that get-num returns first character if it's a digit
321     # This test uses exit-descriptors. Use EBP for setting up local variables.
322     55/push-EBP
323     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
324     # clear all streams
325     # . clear-stream(_test-stream)
326     # . . push args
327     68/push  _test-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     # . clear-stream(_test-buffered-file+4)
333     # . . push args
334     b8/copy-to-EAX  _test-buffered-file/imm32
335     05/add-to-EAX  4/imm32
336     50/push-EAX
337     # . . call
338     e8/call  clear-stream/disp32
339     # . . discard args
340     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
341     # . clear-stream(_test-output-stream)
342     # . . push args
343     68/push  _test-output-stream/imm32
344     # . . call
345     e8/call  clear-stream/disp32
346     # . . discard args
347     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
348     # . clear-stream(_test-error-stream)
349     # . . push args
350     68/push  _test-error-stream/imm32
351     # . . call
352     e8/call  clear-stream/disp32
353     # . . discard args
354     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
355     # initialize 'in'
356     # . write(_test-stream, "3")
357     # . . push args
358     68/push  "3"/imm32
359     68/push  _test-stream/imm32
360     # . . call
361     e8/call  write/disp32
362     # . . discard args
363     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
364     # initialize exit-descriptor 'ed' for the call to 'get-num' below
365     # . var ed/EAX : exit-descriptor
366     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
367     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
368     # . tailor-exit-descriptor(ed, 16)
369     # . . push args
370     68/push  0x10/imm32/nbytes-of-args-for-get-num
371     50/push-EAX/ed
372     # . . call
373     e8/call  tailor-exit-descriptor/disp32
374     # . . discard args
375     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
376     # prime the pump
377     # . get-char(_test-buffered-file)
378     # . . push args
379     68/push  _test-buffered-file/imm32
380     # . . call
381     e8/call  get-char/disp32
382     # . . discard args
383     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
384     # get-num(in, out, err, ed)
385     # . . push args
386     50/push-EAX/ed
387     68/push  _test-error-stream/imm32
388     68/push  _test-output-stream/imm32
389     68/push  _test-buffered-file/imm32
390     # . . call
391     e8/call  get-num/disp32
392     # registers except ESP may be clobbered at this point
393     # . . discard args
394     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
395     # check-ints-equal(*_test-output-stream->data, '3', msg)
396     # . . push args
397     68/push  "F - test-get-num-reads-single-digit"/imm32
398     68/push  0x33/imm32
399     b8/copy-to-EAX  _test-output-stream/imm32
400     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
401     # . . call
402     e8/call  check-ints-equal/disp32
403     # . . discard args
404     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
405     # . reclaim locals
406     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
407     5d/pop-to-EBP
408     c3/return
409 
410 test-get-num-aborts-on-non-digit-in-Look:
411     # - check that get-num returns first character if it's a digit
412     # This test uses exit-descriptors. Use EBP for setting up local variables.
413     55/push-EBP
414     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
415     # clear all streams
416     # . clear-stream(_test-stream)
417     # . . push args
418     68/push  _test-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     # . clear-stream(_test-buffered-file+4)
424     # . . push args
425     b8/copy-to-EAX  _test-buffered-file/imm32
426     05/add-to-EAX  4/imm32
427     50/push-EAX
428     # . . call
429     e8/call  clear-stream/disp32
430     # . . discard args
431     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
432     # . clear-stream(_test-output-stream)
433     # . . push args
434     68/push  _test-output-stream/imm32
435     # . . call
436     e8/call  clear-stream/disp32
437     # . . discard args
438     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
439     # . clear-stream(_test-error-stream)
440     # . . push args
441     68/push  _test-error-stream/imm32
442     # . . call
443     e8/call  clear-stream/disp32
444     # . . discard args
445     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
446     # initialize 'in'
447     # . write(_test-stream, "3")
448     # . . push args
449     68/push  "3"/imm32
450     68/push  _test-stream/imm32
451     # . . call
452     e8/call  write/disp32
453     # . . discard args
454     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
455     # initialize exit-descriptor 'ed' for the call to 'get-num' below
456     # . var ed/EAX : (address exit-descriptor)
457     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
458     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
459     # . tailor-exit-descriptor(ed, 16)
460     # . . push args
461     68/push  0x10/imm32/nbytes-of-args-for-get-num
462     50/push-EAX/ed
463     # . . call
464     e8/call  tailor-exit-descriptor/disp32
465     # . . discard args
466     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
467     # *don't* prime the pump
468     # get-num(in, out, err, ed)
469     # . . push args
470     50/push-EAX/ed
471     68/push  _test-error-stream/imm32
472     68/push  _test-output-stream/imm32
473     68/push  _test-buffered-file/imm32
474     # . . call
475     e8/call  get-num/disp32
476     # registers except ESP may be clobbered at this point
477     # . . discard args
478     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
479     # check that get-num tried to call exit(1)
480     # . check-ints-equal(ed->value, 2, msg)  # i.e. stop was called with value 1
481     # . . push args
482     68/push  "F - test-get-num-aborts-on-non-digit-in-Look"/imm32
483     68/push  2/imm32
484     # . . push ed->value
485     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         .                 # push *(EAX+4)
486     # . . call
487     e8/call  check-ints-equal/disp32
488     # . . discard args
489     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
490     # . reclaim locals
491     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
492     5d/pop-to-EBP
493     c3/return
494 
495 test-get-num-reads-multiple-digits:
496     # - check that get-num returns all initial digits until it encounters a non-digit
497     # This test uses exit-descriptors. Use EBP for setting up local variables.
498     55/push-EBP
499     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
500     # clear all streams
501     # . clear-stream(_test-stream)
502     # . . push args
503     68/push  _test-stream/imm32
504     # . . call
505     e8/call  clear-stream/disp32
506     # . . discard args
507     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
508     # . clear-stream(_test-buffered-file+4)
509     # . . push args
510     b8/copy-to-EAX  _test-buffered-file/imm32
511     05/add-to-EAX  4/imm32
512     50/push-EAX
513     # . . call
514     e8/call  clear-stream/disp32
515     # . . discard args
516     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
517     # . clear-stream(_test-output-stream)
518     # . . push args
519     68/push  _test-output-stream/imm32
520     # . . call
521     e8/call  clear-stream/disp32
522     # . . discard args
523     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
524     # . clear-stream(_test-error-stream)
525     # . . push args
526     68/push  _test-error-stream/imm32
527     # . . call
528     e8/call  clear-stream/disp32
529     # . . discard args
530     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
531     # initialize 'in'
532     # . write(_test-stream, "3456 x")
533     # . . push args
534     68/push  "3456"/imm32
535     68/push  _test-stream/imm32
536     # . . call
537     e8/call  write/disp32
538     # . . discard args
539     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
540     # initialize exit-descriptor 'ed' for the call to 'get-num' below
541     # . var ed/EAX : (address exit-descriptor)
542     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
543     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
544     # . tailor-exit-descriptor(ed, 16)
545     # . . push args
546     68/push  0x10/imm32/nbytes-of-args-for-get-num
547     50/push-EAX/ed
548     # . . call
549     e8/call  tailor-exit-descriptor/disp32
550     # . . discard args
551     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
552     # prime the pump
553     # . get-char(_test-buffered-file)
554     # . . push args
555     68/push  _test-buffered-file/imm32
556     # . . call
557     e8/call  get-char/disp32
558     # . . discard args
559     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
560     # get-num(in, out, err, ed)
561     # . . push args
562     50/push-EAX/ed
563     68/push  _test-error-stream/imm32
564     68/push  _test-output-stream/imm32
565     68/push  _test-buffered-file/imm32
566     # . . call
567     e8/call  get-num/disp32
568     # registers except ESP may be clobbered at this point
569     # . . discard args
570     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
571     # check-ints-equal(*_test-output-stream->data, '3456', msg)
572     # . . push args
573     68/push  "F - test-get-num-reads-multiple-digits"/imm32
574     68/push  0x36353433/imm32
575     b8/copy-to-EAX  _test-output-stream/imm32
576     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
577     # . . call
578     e8/call  check-ints-equal/disp32
579     # . . discard args
580     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
581     # . reclaim locals
582     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
583     5d/pop-to-EBP
584     c3/return
585 
586 test-get-num-reads-multiple-digits-followed-by-nondigit:
587     # - check that get-num returns all initial digits until it encounters a non-digit
588     # This test uses exit-descriptors. Use EBP for setting up local variables.
589     55/push-EBP
590     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
591     # clear all streams
592     # . clear-stream(_test-stream)
593     # . . push args
594     68/push  _test-stream/imm32
595     # . . call
596     e8/call  clear-stream/disp32
597     # . . discard args
598     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
599     # . clear-stream(_test-buffered-file+4)
600     # . . push args
601     b8/copy-to-EAX  _test-buffered-file/imm32
602     05/add-to-EAX  4/imm32
603     50/push-EAX
604     # . . call
605     e8/call  clear-stream/disp32
606     # . . discard args
607     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
608     # . clear-stream(_test-output-stream)
609     # . . push args
610     68/push  _test-output-stream/imm32
611     # . . call
612     e8/call  clear-stream/disp32
613     # . . discard args
614     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
615     # . clear-stream(_test-error-stream)
616     # . . push args
617     68/push  _test-error-stream/imm32
618     # . . call
619     e8/call  clear-stream/disp32
620     # . . discard args
621     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
622     # initialize 'in'
623     # . write(_test-stream, "3456 x")
624     # . . push args
625     68/push  "3456 x"/imm32
626     68/push  _test-stream/imm32
627     # . . call
628     e8/call  write/disp32
629     # . . discard args
630     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
631     # initialize exit-descriptor 'ed' for the call to 'get-num' below
632     # . var ed/EAX : (address exit-descriptor)
633     81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
634     8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   .               .                 # copy ESP to EAX
635     # . tailor-exit-descriptor(ed, 16)
636     # . . push args
637     68/push  0x10/imm32/nbytes-of-args-for-get-num
638     50/push-EAX/ed
639     # . . call
640     e8/call  tailor-exit-descriptor/disp32
641     # . . discard args
642     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
643     # prime the pump
644     # . get-char(_test-buffered-file)
645     # . . push args
646     68/push  _test-buffered-file/imm32
647     # . . call
648     e8/call  get-char/disp32
649     # . . discard args
650     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
651     # get-num(in, out, err, ed)
652     # . . push args
653     50/push-EAX/ed
654     68/push  _test-error-stream/imm32
655     68/push  _test-output-stream/imm32
656     68/push  _test-buffered-file/imm32
657     # . . call
658     e8/call  get-num/disp32
659     # registers except ESP may be clobbered at this point
660     # . . discard args
661     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
662     # check-ints-equal(*_test-output-stream->data, '3456', msg)
663     # . . push args
664     68/push  "F - test-get-num-reads-multiple-digits-followed-by-nondigit"/imm32
665     68/push  0x36353433/imm32
666     b8/copy-to-EAX  _test-output-stream/imm32
667     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
668     # . . call
669     e8/call  check-ints-equal/disp32
670     # . . discard args
671     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
672     # . reclaim locals
673     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
674     5d/pop-to-EBP
675     c3/return
676 
677 ## helpers
678 
679 # write(f, "Error: "+s+" expected\n") then stop(ed, 1)
680 expected:  # ed : (address exit-descriptor), f : fd or (address stream), s : (address array byte) -> <void>
681     # . prolog
682     55/push-EBP
683     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
684     # write(f, "Error: ")
685     # . . push args
686     68/push  "Error: "/imm32
687     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
688     # . . call
689     e8/call  write/disp32
690     # . . discard args
691     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
692     # write(f, s)
693     # . . push args
694     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x10/disp8      .                 # push *(EBP+16)
695     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
696     # . . call
697     e8/call  write/disp32
698     # . . discard args
699     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
700     # write(f, " expected")
701     # . . push args
702     68/push  " expected"/imm32
703     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
704     # . . call
705     e8/call  write/disp32
706     # . . discard args
707     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
708     # write(f, Newline)
709     # . . push args
710     68/push  Newline/imm32
711     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
712     # . . call
713     e8/call  write/disp32
714     # . . discard args
715     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
716     # stop(ed, 1)
717     # . . push args
718     68/push  1/imm32
719     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         .                 # push *(EBP+8)
720     # . . call
721     e8/call  stop/disp32
722     # should never get past this point
723     # . epilog
724     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
725     5d/pop-to-EBP
726     c3/return
727 
728 # read a byte from 'f', and save it in 'Look'
729 get-char:  # f : (address buffered-file) -> <void>
730     # . prolog
731     55/push-EBP
732     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
733     # . save registers
734     50/push-EAX
735     # read-byte(f)
736     # . . push args
737     ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       .                 # push *(EBP+8)
738     # . . call
739     e8/call  read-byte/disp32
740     # . . discard args
741     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
742     # save EAX to Look
743     89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Look/disp32     .                 # copy EAX to *Look
744     # . restore registers
745     58/pop-to-EAX
746     # . epilog
747     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
748     5d/pop-to-EBP
749     c3/return
750 
751 is-digit?:  # c : int -> bool/EAX
752     # . prolog
753     55/push-EBP
754     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
755     # EAX = false
756     b8/copy-to-EAX  0/imm32
757     # if c < '0' return false
758     81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       0x30/imm32        # compare *(EBP+8)
759     7c/jump-if-lesser  $is-digit?:end/disp8
760     # if c > '9' return false
761     81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/disp8       0x39/imm32        # compare *(EBP+8)
762     7f/jump-if-greater  $is-digit?:end/disp8
763     # otherwise return true
764     b8/copy-to-EAX  1/imm32
765 $is-digit?:end:
766     # . epilog
767     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
768     5d/pop-to-EBP
769     c3/return
770 
771 == data
772 
773 Look: # (char)
774     00 00 00 00  # = 0
775 
776 _test-output-stream:
777     # current write index
778     00 00 00 00
779     # current read index
780     00 00 00 00
781     # length (= 8)
782     08 00 00 00
783     # data
784     00 00 00 00 00 00 00 00  # 8 bytes
785 
786 _test-error-stream:
787     # current write index
788     00 00 00 00
789     # current read index
790     00 00 00 00
791     # length (= 8)
792     08 00 00 00
793     # data
794     00 00 00 00 00 00 00 00  # 8 bytes
795 
796 # . . vim:nowrap:textwidth=0