https://github.com/akkartik/mu/blob/master/subx/apps/factorial.subx
  1 ## compute the factorial of 5, and return the result in the exit code
  2 #
  3 # To run (from the subx directory):
  4 #   $ ./subx translate apps/factorial.subx -o apps/factorial
  5 #   $ ./subx run apps/factorial
  6 # Expected result:
  7 #   $ echo $?
  8 #   120
  9 #
 10 # You can also run the automated test suite:
 11 #   $ ./subx run apps/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 Entry:  # run tests if necessary, compute `factorial(5)` if not
 22 
 23 #?     # for debugging: run a single test; don't bother setting status code
 24 #?     e8/call test-get-num-reads-single-digit/disp32
 25 #?     eb/jump  $main:end/disp8
 26 
 27     # . prolog
 28     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 29     # - if argc > 1 and argv[1] == "test", then return run_tests()
 30     # . argc > 1
 31     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0/disp8         1/imm32           # compare *EBP
 32     7e/jump-if-lesser-or-equal  $run-main/disp8
 33     # . argv[1] == "test"
 34     # . . push args
 35     68/push  "test"/imm32
 36     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 37     # . . call
 38     e8/call  kernel-string-equal?/disp32
 39     # . . discard args
 40     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 41     # . check result
 42     3d/compare-EAX-and  1/imm32
 43     75/jump-if-not-equal  $run-main/disp8
 44     # . run-tests()
 45     e8/call  run-tests/disp32
 46     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Num-test-failures/disp32          # copy *Num-test-failures to EAX
 47     eb/jump  $main:end/disp8  # where EAX will get copied to EBX
 48 $run-main:
 49     # - otherwise return factorial(5)
 50     # . . push args
 51     68/push  5/imm32
 52     # . . call
 53     e8/call  factorial/disp32
 54     # . . discard args
 55     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 56 $main:end:
 57     # syscall(exit, EAX)
 58     89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EBX
 59     b8/copy-to-EAX  1/imm32/exit
 60     cd/syscall  0x80/imm8
 61 
 62 factorial:  # n : int -> int/EAX
 63     # . prolog
 64     55/push-EBP
 65     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 66     53/push-EBX
 67     # EAX = 1 (base case)
 68     b8/copy-to-EAX  1/imm32
 69     # if (n <= 1) return
 70     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         1/imm32           # compare *(EBP+8)
 71     7e/jump-if-<=  $factorial:end/disp8
 72     # EBX = n-1
 73     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         3/r32/EBX   8/disp8         .                 # copy *(EBP+8) to EBX
 74     81          5/subop/subtract    3/mod/direct    3/rm32/EBX    .           .             .           .           .               1/imm32           # subtract from EBX
 75     # EAX = factorial(n-1)
 76     # . . push args
 77     53/push-EBX
 78     # . . call
 79     e8/call  factorial/disp32
 80     # . . discard args
 81     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 82     # return n * factorial(n-1)
 83     f7          4/subop/multiply    1/mod/*+disp8   5/rm32/EBP    .           .                                     8/disp8         .                 # multiply *(EBP+8) into EAX
 84     # TODO: check for overflow
 85 $factorial:end:
 86     # . epilog
 87     5b/pop-to-EBX
 88     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 89     5d/pop-to-EBP
 90     c3/return
 91 
 92 test-factorial:
 93     # factorial(5)
 94     # . . push args
 95     68/push  5/imm32
 96     # . . call
 97     e8/call  factorial/disp32
 98     # . . discard args
 99     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
100     # check-ints-equal(EAX, 120, msg)
101     # . . push args
102     68/push  "F - test-factorial"/imm32
103     68/push  0x78/imm32/expected-120
104     50/push-EAX
105     # . . call
106     e8/call  check-ints-equal/disp32
107     # . . discard args
108     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
109     # end
110     c3/return
111 
112 # . . vim:nowrap:textwidth=0