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