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 b8/copy-to-eax 1/imm32/exit
56 cd/syscall 0x80/imm8
57
58 factorial:
59
60 55/push-ebp
61 89/<- %ebp 4/r32/esp
62
63 51/push-ecx
64
65 81 7/subop/compare *(ebp+8) 1/imm32
66 {
67 7f/jump-if-> break/disp8
68 b8/copy-to-eax 1/imm32
69 }
70
71 {
72 7e/jump-if-<= break/disp8
73
74 8b/-> *(ebp+8) 1/r32/ecx
75 49/decrement-ecx
76 (factorial %ecx)
77 f7 4/subop/multiply-into-eax *(ebp+8)
78 }
79
80 59/pop-to-ecx
81
82 89/<- %esp 5/r32/ebp
83 5d/pop-to-ebp
84 c3/return
85
86 test-factorial:
87 (factorial 5)
88 (check-ints-equal %eax 0x78 "F - test-factorial")
89 c3/return