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 (from the subx directory):
  5 #   $ subx translate *.subx apps/crenshaw2.1.subx -o 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 to ESP
 29   # exit(0)
 30   bb/copy                         .               .             .           .             .           .           .               0/imm32           # copy to EBX
 31   b8/copy                         .               .             .           .             .           .           .               1/imm32/exit      # copy to EAX
 32   cd/syscall  0x80/imm8
 33 
 34 ## 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 to ESP
 46   # exit(1)
 47   bb/copy                         .               .             .           .             .           .           .               1/imm32           # copy to EBX
 48   b8/copy                         .               .             .           .             .           .           .               1/imm32/exit      # copy 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 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 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 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 to ESP
 81   # end
 82   c3/return
 83 
 84 write_stdout:  # s : (address array byte) -> <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   52/push-EDX
 92   53/push-EBX
 93   # write(1/stdout, (data) s+4, (size) *s)
 94     # fd = 1 (stdout)
 95   bb/copy                         .               .             .           .             .           .           .               1/imm32           # copy to EBX
 96     # x = s+4
 97   8b/copy                         1/mod/*+disp8   4/rm32/SIB    5/base/EBP  4/index/none  .           1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 98   81          0/subop/add         3/mod/direct    1/rm32/ECX    .           .             .           .           .               4/imm32           # add to ECX
 99     # size = *s
100   8b/copy                         1/mod/*+disp8   4/rm32/SIB    5/base/EBP  4/index/none  .           2/r32/EDX   8/disp8         .                 # copy *(EBP+8) to EDX
101   8b/copy                         0/mod/indirect  2/rm32/EDX    .           .             .           2/r32/EDX   .               .                 # copy *EDX to EDX
102     # call write()
103   b8/copy                         .               .             .           .             .           .           .               4/imm32/write     # copy to EAX
104   cd/syscall  0x80/imm8
105   # restore registers
106   5b/pop-to-EBX
107   5a/pop-to-EDX
108   59/pop-to-ECX
109   58/pop-to-EAX
110   # end
111   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
112   5d/pop-to-EBP
113   c3/return
114 
115 # vim:ft=subx:nowrap:so=0