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