1 ## port of https://github.com/akkartik/crenshaw/blob/master/tutor2.1.pas
  2 # corresponds to the section "single digits" in https://compilers.iecc.com/crenshaw/tutor2.txt
  3 #
  4 # To run:
  5 #   $ subx translate apps/crenshaw2.1.subx crenshaw 2.1
  6 #   $ echo '3'  |subx run apps/crenshaw2.1  |xxd -
  7 # Expected output:
  8 #   TODO
  9 #
 10 # The output is the code a function would need to include, returning the
 11 # result in EAX.
 12 #
 13 # Major note: byte strings are not null-terminated. Instead they're prefixed
 14 # with a 32-bit length.
 15 
 16 == code
 17 # instruction                     effective address                                                   operand     displacement    immediate
 18 # op          subop               mod             rm32          base        index         scale       r32
 19 # 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
 20 
 21 # main:
 22   # abort("Integer")
 23     # push args
 24   68/push  "Integer"/imm32
 25     # call
 26   e8/call  abort/disp32
 27     # discard arg
 28   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
 29   # exit(0)
 30   bb/copy                         .               .             .           .             .           .           .               0/imm32           # copy 0 to EBX
 31   b8/copy                         .               .             .           .             .           .           .               1/imm32/exit      # copy 1 to EAX
 32   cd/syscall  0x80/imm8
 33 
 34 ## compiler helpers
 35 
 36 # print error message and exit
 37 # really maps to the 'Expected' function in Crenshaw
 38 abort:  # s : (address array byte) -> <void>
 39   # error(s)
 40     # push args
 41   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           .           4/disp8         .                 # push *(ESP+4)
 42     # call
 43   e8/call  error/disp32
 44     # discard arg
 45   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
 46   # exit(1)
 47   bb/copy                         .               .             .           .             .           .           .               1/imm32           # copy 1 to EBX
 48   b8/copy                         .               .             .           .             .           .           .               1/imm32/exit      # copy 1 to EAX
 49   cd/syscall  0x80/imm8
 50 
 51 # print out "Error: #{s} expected\n" to stderr
 52 error:  # s : (address array byte) -> <void>
 53   # write_stderr("Error: ")
 54     # push args
 55   68/push  "Error: "/imm32
 56     # call
 57   e8/call  write_stderr/disp32
 58     # discard arg
 59   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
 60   # write_stderr(s)
 61     # push args
 62   ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           .           4/disp8         .                 # push *(ESP+4)
 63     # call
 64   e8/call  write_stderr/disp32
 65     # discard arg
 66   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
 67   # write_stderr(" expected")
 68     # push args
 69   68/push  " expected"/imm32
 70     # call
 71   e8/call  write_stderr/disp32
 72     # discard arg
 73   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
 74   # write_stderr("\n")
 75     # push args
 76   68/push  Newline/imm32
 77     # call
 78   e8/call  write_stderr/disp32
 79     # discard arg
 80   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
 81   # end
 82   c3/return
 83 
 84 ## helpers
 85 
 86 # print msg to stderr if a != b, otherwise print "."
 87 check_ints_equal:  # (a : int, b : int, msg : (address array byte)) -> boolean
 88   # load args into EAX, EBX and ECX
 89   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           0/r32/EAX   0xc/disp8       .                 # copy *(ESP+12) to EAX
 90   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           3/r32/EBX   0x8/disp8       .                 # copy *(ESP+8) to EBX
 91   # if EAX == b/EBX
 92   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX and EBX
 93   75/jump-if-unequal  $check_ints_equal:else/disp8
 94     # print('.')
 95       # push args
 96   68/push  "."/imm32
 97       # call
 98   e8/call  write_stderr/disp32
 99       # discard arg
100   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
101     # return
102   c3/return
103   # else:
104 $check_ints_equal:else:
105   # copy msg into ECX
106   8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   4/disp8         .                 # copy *(ESP+4) to ECX
107     # print(ECX)
108       # push args
109   51/push-ECX
110       # call
111   e8/call  write_stderr/disp32
112       # discard arg
113   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
114     # print newline
115       # push args
116   68/push  Newline/imm32
117       # call
118   e8/call  write_stderr/disp32
119       # discard arg
120   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add 4 to ESP
121   # end
122   c3/return
123 
124 # compare a null-terminated ascii string with a more idiomatic length-prefixed byte array
125 # reason for the name: the only place we should have null-terminated ascii strings is from commandline args
126 argv_equal:  # s : null-terminated ascii string, benchmark : length-prefixed ascii string -> EAX : boolean
127 +-- 58 lines: # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
185 +--134 lines: # tests for argv_equal -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
319 
320 write_stderr:  # s : (address array byte) -> <void>
321   # save registers
322   50/push-EAX
323   51/push-ECX
324   52/push-EDX
325   53/push-EBX
326   # write(2/stderr, (data) s+4, (size) *s)
327     # fd = 2 (stderr)
328   bb/copy                         .               .             .           .             .           .           .               2/imm32           # copy 2 to EBX
329     # x = s+4
330   8b/copy                         1/mod/*+disp8   4/rm32/SIB    4/base/ESP  4/index/none  .           1/r32/ECX   0x14/disp8      .                 # copy *(ESP+20) to ECX
331   81          0/subop/add         3/mod/direct    1/rm32/ECX    .           .             .           .           .               4/imm32           # add 4 to ECX
332     # size = *s
333   8b/copy                         1/mod/*+disp8   4/rm32/SIB    4/base/ESP  4/index/none  .           2/r32/EDX   0x14/disp8      .                 # copy *(ESP+20) to EDX
334   8b/copy                         0/mod/indirect  2/rm32/EDX    .           .             .           2/r32/EDX   .               .                 # copy *EDX to EDX
335     # call write()
336   b8/copy                         .               .             .           .             .           .           .               4/imm32/write     # copy 1 to EAX
337   cd/syscall  0x80/imm8
338   # restore registers
339   5b/pop-EBX
340   5a/pop-EDX
341   59/pop-ECX
342   58/pop-EAX
343   # end
344   c3/return
345 
346 write_stdout:  # s : (address array byte) -> <void>
347   # save registers
348   50/push-EAX
349   51/push-ECX
350   52/push-EDX
351   53/push-EBX
352   # write(1/stdout, (data) s+4, (size) *s)
353     # fd = 1 (stdout)
354   bb/copy                         .               .             .           .             .           .           .               1/imm32           # copy 1 to EBX
355     # x = s+4
356   8b/copy                         1/mod/*+disp8   4/rm32/SIB    4/base/ESP  4/index/none  .           1/r32/ECX   0x14/disp8      .                 # copy *(ESP+20) to ECX
357   81          0/subop/add         3/mod/direct    1/rm32/ECX    .           .             .           .           .               4/imm32           # add 4 to ECX
358     # size = *s
359   8b/copy                         1/mod/*+disp8   4/rm32/SIB    4/base/ESP  4/index/none  .           2/r32/EDX   0x14/disp8      .                 # copy *(ESP+20) to EDX
360   8b/copy                         0/mod/indirect  2/rm32/EDX    .           .             .           2/r32/EDX   .               .                 # copy *EDX to EDX
361     # call write()
362   b8/copy                         .               .             .           .             .           .           .               4/imm32/write     # copy 1 to EAX
363   cd/syscall  0x80/imm8
364   # restore registers
365   5b/pop-EBX
366   5a/pop-EDX
367   59/pop-ECX
368   58/pop-EAX
369   # end
370   c3/return
371 
372 == data
373 Newline:
374   # size
375   01 00 00 00
376   # data
377   0a/newline
378 
379 # for argv_equal tests
380 Null_argv:
381   00/null
382 Abc_argv:
383   41/A 62/b 63/c 00/null
384 
385 # vim:ft=subx:nowrap:so=0