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 apps/factorial.subx -o apps/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     # . . discard args
     95     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
     96     # var buffer/ecx: (stream byte 10)  # number of decimal digits a 32-bit number can have
     97     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xa/imm32         # subtract from esp
     98     68/push  0xa/imm32/decimal-digits-in-32bit-number
     99     68/push  0/imm32/read
    100     68/push  0/imm32/write
    101     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    102     # write-int32-decimal(buffer, eax)
    103     # . . push args
    104     50/push-eax
    105     51/push-ecx
    106     # . . call
    107     e8/call  write-int32-decimal/disp32
    108     # . . discard args
    109     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    110     # write-stream(stderr, buffer)
    111     # . . push args
    112     51/push-ecx
    113     68/push  2/imm32/stderr
    114     # . . call
    115     e8/call  write-stream/disp32
    116     # . . discard args
    117     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    118     # write(stderr, Newline)
    119     # . . push args
    120     68/push  Newline/imm32
    121     68/push  2/imm32/stderr
    122     # . . call
    123     e8/call  write/disp32
    124     # . . discard args
    125     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    126     #
    127     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
    128     eb/jump  $main:end/disp8
    129 $main:run-tests:
    130     # otherwise if first arg is "test", then return run_tests()
    131     # if (!kernel-string-equal?(argv[1], "test")) goto do-nothing
    132     # . eax = kernel-string-equal?(argv[1], "test")
    133     # . . push args
    134     68/push  "test"/imm32
    135     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    136     # . . call
    137     e8/call  kernel-string-equal?/disp32
    138     # . . discard args
    139     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    140     # . if (eax == false) goto do-nothing
    141     3d/compare-eax-and  0/imm32/false
    142     74/jump-if-=  $main:do-nothing/disp8
    143     # run-tests()
    144     e8/call  run-tests/disp32
    145     # exit(*Num-test-failures)
    146     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
    147     eb/jump  $main:end/disp8
    148 $main:do-nothing:
    149     bb/copy-to-ebx  0/imm32
    150 $main:end:
    151     e8/call  syscall_exit/disp32
    152
    153 # . . vim:nowrap:textwidth=0