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