https://github.com/akkartik/mu/blob/main/linux/factorial.subx
  1 ## compute the factorial of 5, and print the result
  2 #
  3 # To run:
  4 #   $ bootstrap/bootstrap translate [01]*.subx factorial.subx -o factorial
  5 #   $ bootstrap/bootstrap run factorial
  6 # Expected result:
  7 #   $ echo $?
  8 #   120
  9 #
 10 # You can also run the automated test suite:
 11 #   $ bootstrap/bootstrap run factorial test
 12 # Expected output:
 13 #   ........
 14 # Every '.' indicates a passing test. Failing tests get a 'F'.
 15 
 16 == code
 17 #   instruction                     effective address                                                   register    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 factorial:  # n: int -> _/eax: int
 22     # . prologue
 23     55/push-ebp
 24     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 25     51/push-ecx
 26     # if (n <= 1) return 1
 27     b8/copy-to-eax  1/imm32
 28     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         1/imm32           # compare *(ebp+8)
 29     7e/jump-if-<=  $factorial:end/disp8
 30     # var ecx: int = n-1
 31     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
 32     49/decrement-ecx
 33     # var eax: int = factorial(n-1)
 34     # . . push args
 35     51/push-ecx
 36     # . . call
 37     e8/call  factorial/disp32
 38     # . . discard args
 39     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 40     # return n * factorial(n-1)
 41     f7          4/subop/multiply    1/mod/*+disp8   5/rm32/ebp    .           .                                     8/disp8         .                 # multiply *(ebp+8) into eax
 42     # TODO: check for overflow
 43 $factorial:end:
 44     # restore registers
 45     59/pop-to-ecx
 46     # . epilogue
 47     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 48     5d/pop-to-ebp
 49     c3/return
 50 
 51 test-factorial:
 52     # factorial(5)
 53     # . . push args
 54     68/push  5/imm32
 55     # . . call
 56     e8/call  factorial/disp32
 57     # . . discard args
 58     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 59     # check-ints-equal(eax, 120, msg)
 60     # . . push args
 61     68/push  "F - test-factorial"/imm32
 62     68/push  0x78/imm32/expected-120
 63     50/push-eax
 64     # . . call
 65     e8/call  check-ints-equal/disp32
 66     # . . discard args
 67     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 68     # end
 69     c3/return
 70 
 71 Entry:  # run tests if necessary, compute `factorial(5)` if not
 72     # . prologue
 73     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 74 
 75     # initialize heap (needed by tests elsewhere)
 76     # . Heap = new-segment(Heap-size)
 77     # . . push args
 78     68/push  Heap/imm32
 79     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Heap-size/disp32                  # push *Heap-size
 80     # . . call
 81     e8/call  new-segment/disp32
 82     # . . discard args
 83     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 84 
 85     # if (argc <= 1) return factorial(5)
 86 $run-main:
 87     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0/disp8         1/imm32           # compare *ebp
 88     7f/jump-if->  $main:run-tests/disp8
 89     # eax = factorial(5)
 90     # . . push args
 91     68/push  5/imm32
 92     # . . call
 93     e8/call  factorial/disp32
 94
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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