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