https://github.com/akkartik/mu/blob/master/apps/factorial.subx
  1 ## compute the factorial of 5, and print the result
  2 #
  3 # To run:
  4 #   $ ./bootstrap translate init.linux 0*.subx apps/factorial.subx -o apps/factorial
  5 #   $ ./bootstrap run apps/factorial
  6 # Expected result:
  7 #   $ echo $?
  8 #   120
  9 #
 10 # You can also run the automated test suite:
 11 #   $ ./bootstrap 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 factorial:  # n: int -> int/eax
 22     # . prologue
 23     55/push-ebp
 24     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 25     53/push-ebx
 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 ebx: int = n-1
 31     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         3/r32/ebx   8/disp8         .                 # copy *(ebp+8) to ebx
 32     4b/decrement-ebx
 33     # var eax: int = factorial(n-1)
 34     # . . push args
 35     53/push-ebx
 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     # . epilogue
 45     5b/pop-to-ebx
 46     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 47     5d/pop-to-ebp
 48     c3/return
 49 
 50 test-factorial:
 51     # factorial(5)
 52     # . . push args
 53     68/push  5/imm32
 54     # . . call
 55     e8/call  factorial/disp32
 56     # . . discard args
 57     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 58     # check-ints-equal(eax, 120, msg)
 59     # . . push args
 60     68/push  "F - test-factorial"/imm32
 61     68/push  0x78/imm32/expected-120
 62     50/push-eax
 63     # . . call
 64     e8/call  check-ints-equal/disp32
 65     # . . discard args
 66     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 67     # end
 68     c3/return
 69 
 70 Entry:  # run tests if necessary, compute `factorial(5)` if not
 71     # . prologue
 72     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 73 
 74     # initialize heap
 75     # . Heap = new-segment(Heap-size)
 76     # . . push args
 77     68/push  Heap/imm32
 78     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Heap-size/disp32                  # push *Heap-size
 79     # . . call
 80     e8/call  new-segment/disp32
 81     # . . discard args
 82     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 83 
 84     # - if argc > 1 and argv[1] == "test", then return run_tests()
 85     # if (argc <= 1) goto run-main
 86     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0/disp8         1/imm32           # compare *ebp
 87     7e/jump-if-<=  $run-main/disp8
 88     # if (!kernel-string-equal?(argv[1], "test")) goto run-main
 89     # . eax = kernel-string-equal?(argv[1], "test")
 90     # . . push args
 91     68/push  "test"/imm32
 92     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 93     # . . call
 94     e8/call  kernel-string-equal?/disp32
 95     # . . discard args
 96     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 97     # . if (eax == false) goto run-main
 98     3d/compare-eax-and  0/imm32/false
 99     74/jump-if-=  $run-main/disp8
100     # run-tests()
101     e8/call  run-tests/disp32
102     # syscall(exit, *Num-test-failures)
103     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
104     eb/jump  $main:end/disp8
105 $run-main:
106     # - otherwise print factorial(5)
107     # eax = factorial(5)
108     # . . push args
109     68/push  5/imm32
110     # . . call
111     e8/call  factorial/disp32
112     # . . discard args
113     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
114     # var buffer/ecx: (stream byte 10)  # number of decimal digits a 32-bit number can have
115     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0xa/imm32         # subtract from esp
116     68/push  0xa/imm32/decimal-digits-in-32bit-number
117     68/push  0/imm32/read
118     68/push  0/imm32/write
119     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
120     # write-int32-decimal(buffer, eax)
121     # . . push args
122     50/push-eax
123     51/push-ecx
124     # . . call
125     e8/call  write-int32-decimal/disp32
126     # . . discard args
127     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
128     # write-stream(stderr, buffer)
129     # . . push args
130     51/push-ecx
131     68/push  2/imm32/stderr
132     # . . call
133     e8/call  write-stream/disp32
134     # . . discard args
135     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
136     # write(stderr, Newline)
137     # . . push args
138     68/push  Newline/imm32
139     68/push  2/imm32/stderr
140     # . . call
141     e8/call  write/disp32
142     # . . discard args
143     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
144     #
145     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
146 $main:end:
147     e8/call  syscall_exit/disp32
148 
149 # . . vim:nowrap:textwidth=0