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