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-lesser-or-equal break/disp8
35
36 (kernel-string-equal? *(ebp+8) "test")
37 3d/compare-eax-and 0/imm32
38 74/jump-if-equal 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-greater 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 53/push-ebx
64
65 81 7/subop/compare *(ebp+8) 1/imm32
66 {
67 7f/jump-if-greater break/disp8
68 b8/copy-to-eax 1/imm32
69 }
70
71 {
72 7e/jump-if-lesser-or-equal break/disp8
73 8b/-> *(ebp+8) 3/r32/ebx
74 4b/decrement-ebx
75 (factorial %ebx)
76 f7 4/subop/multiply-into-eax *(ebp+8)
77 }
78
79 5b/pop-to-ebx
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