https://github.com/akkartik/mu/blob/main/apps/factorial3.subx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 == code
23
24 factorial:
25
26 55/push-ebp
27 89/<- %ebp 4/r32/esp
28
29 51/push-ecx
30
31 b8/copy-to-eax 1/imm32
32 81 7/subop/compare *(ebp+8) 1/imm32
33 7e/jump-if-<= $factorial:end/disp8
34
35 8b/-> *(ebp+8) 1/r32/ecx
36 49/decrement-ecx
37 (factorial %ecx)
38 f7 4/subop/multiply-into-eax *(ebp+8)
39
40 $factorial:end:
41
42 59/pop-to-ecx
43
44 89/<- %esp 5/r32/ebp
45 5d/pop-to-ebp
46 c3/return
47
48 test-factorial:
49 (factorial 5)
50 (check-ints-equal %eax 0x78 "F - test-factorial")
51 c3/return
52
53 Entry:
54
55 89/<- %ebp 4/r32/esp
56
57
58 (new-segment *Heap-size Heap)
59
60
61 81 7/subop/compare *ebp 1/imm32
62 7f/jump-if-> $main:run-tests/disp8
63 (factorial 5)
64 89/<- %ebx 0/r32/eax
65 eb/jump $main:end/disp8
66 $main:run-tests:
67
68
69 (kernel-string-equal? *(ebp+8) "test")
70 3d/compare-eax-and 0/imm32/false
71 74/jump-if-= $main:do-nothing/disp8
72
73 (run-tests)
74
75 8b/-> *Num-test-failures 3/r32/ebx
76 eb/jump $main:end/disp8
77 $main:do-nothing:
78 bb/copy-to-ebx 0/imm32
79 $main:end:
80 e8/call syscall_exit/disp32