https://github.com/akkartik/mu/blob/master/apps/factorial2.subx
  1 ## compute the factorial of 5, and return the result in the exit code
  2 #
  3 # Uses syntax sugar for:
  4 #   rm32 operands
  5 #
  6 # To run:
  7 #   $ ./translate_subx init.linux 0*.subx apps/factorial.subx -o apps/factorial
  8 #   $ ./bootstrap run apps/factorial
  9 # Expected result:
 10 #   $ echo $?
 11 #   120
 12 #
 13 # You can also run the automated test suite:
 14 #   $ ./bootstrap run apps/factorial test
 15 # Expected output:
 16 #   ........
 17 # Every '.' indicates a passing test. Failing tests get a 'F'.
 18 
 19 == code
 20 
 21 Entry:  # run tests if necessary, compute `factorial(5)` if not
 22     # . prologue
 23     89/<- %ebp 4/r32/esp
 24 
 25     # initialize heap
 26     # . Heap = new-segment(Heap-size)
 27     # . . push args
 28     68/push Heap/imm32
 29     ff 6/subop/push *Heap-size
 30     # . . call
 31     e8/call new-segment/disp32
 32     # . . discard args
 33     81 0/subop/add %esp 8/imm32
 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 *ebp 1/imm32
 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 *(ebp+8)
 44     # . . call
 45     e8/call kernel-string-equal?/disp32
 46     # . . discard args
 47     81 0/subop/add %esp 8/imm32
 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/-> *Num-test-failures 3/r32/ebx
 55     eb/jump $main:end/disp8
 56 $run-main:
 57     # - otherwise return 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 %esp 4/imm32
 65     # syscall(exit, eax)
 66     89/<- %ebx 0/r32/eax
 67 $main:end:
 68     b8/copy-to-eax 1/imm32/exit
 69     cd/syscall 0x80/imm8
 70 
 71 factorial:  # n: int -> int/eax
 72     # . prologue
 73     55/push-ebp
 74     89/<- %ebp 4/r32/esp
 75     53/push-ebx
 76     # if (n <= 1) return 1
 77     b8/copy-to-eax 1/imm32
 78     81 7/subop/compare *(ebp+8) 1/imm32
 79     7e/jump-if-<= $factorial:end/disp8
 80     # var ebx: int = n-1
 81     8b/-> *(ebp+8) 3/r32/ebx
 82     4b/decrement-ebx
 83     # var eax: int = factorial(n-1)
 84     # . . push args
 85     53/push-ebx
 86     # . . call
 87     e8/call factorial/disp32
 88     # . . discard args
 89     81 0/subop/add %esp 4/imm32
 90     # return n * factorial(n-1)
 91     f7 4/subop/multiply-into-eax *(ebp+8)
 92     # TODO: check for overflow
 93 $factorial:end:
 94     # . epilogue
 95     5b/pop-to-ebx
 96     89/<- %esp 5/r32/ebp
 97     5d/pop-to-ebp
 98     c3/return
 99 
100 test-factorial:
101     # factorial(5)
102     # . . push args
103     68/push 5/imm32
104     # . . call
105     e8/call factorial/disp32
106     # . . discard args
107     81 0/subop/add %esp 4/imm32
108     # check-ints-equal(eax, 120, msg)
109     # . . push args
110     68/push "F - test-factorial"/imm32
111     68/push 0x78/imm32/expected-120
112     50/push-eax
113     # . . call
114     e8/call check-ints-equal/disp32
115     # . . discard args
116     81 0/subop/add %esp 0xc/imm32
117     # end
118     c3/return