https://github.com/akkartik/mu/blob/master/apps/factorial3.subx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 == code
21
22 Entry:
23
24 89/<- %ebp 4/r32/esp
25
26
27 (new-segment *Heap-size Heap)
28
29
30
31 81 7/subop/compare *ebp 1/imm32
32 7e/jump-if-<= $run-main/disp8
33
34 (kernel-string-equal? *(ebp+8) "test")
35
36 3d/compare-eax-and 0/imm32/false
37 74/jump-if-= $run-main/disp8
38
39 (run-tests)
40
41 8b/-> *Num-test-failures 3/r32/ebx
42 eb/jump $main:end/disp8
43 $run-main:
44
45 (factorial 5)
46
47 89/<- %ebx 0/r32/eax
48 $main:end:
49 b8/copy-to-eax 1/imm32/exit
50 cd/syscall 0x80/imm8
51
52 factorial:
53
54 55/push-ebp
55 89/<- %ebp 4/r32/esp
56
57 53/push-ebx
58
59 b8/copy-to-eax 1/imm32
60 81 7/subop/compare *(ebp+8) 1/imm32
61 7e/jump-if-<= $factorial:end/disp8
62
63 8b/-> *(ebp+8) 3/r32/ebx
64 4b/decrement-ebx
65
66 (factorial %ebx)
67
68 f7 4/subop/multiply-into-eax *(ebp+8)
69
70 $factorial:end:
71
72 5b/pop-to-ebx
73
74 89/<- %esp 5/r32/ebp
75 5d/pop-to-ebp
76 c3/return
77
78 test-factorial:
79 (factorial 5)
80 (check-ints-equal %eax 0x78 "F - test-factorial")
81 c3/return