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