Commit | Line | Data |
---|---|---|
79bfeef6 PC |
1 | .data 32 |
2 | str: | |
3 | .c "%.0lf\n" | |
4 | .code | |
5 | jmpi main | |
6 | /* | |
7 | * double factorial(unsigned long n) { | |
8 | * double r = 1; | |
9 | * while (n > 1) { | |
10 | * r *= n; | |
11 | * --n; | |
12 | * } | |
13 | * return r; | |
14 | * } | |
15 | */ | |
16 | factorial: | |
17 | prolog | |
18 | arg $n | |
19 | getarg %r0 $n | |
20 | movi_d %f0 1.0 | |
21 | extr_d %f1 %r0 | |
22 | movr_d %f2 %f0 | |
23 | loop: | |
24 | bltr_d done %f1 %f2 | |
25 | mulr_d %f0 %f0 %f1 | |
26 | subr_d %f1 %f1 %f2 | |
27 | jmpi loop | |
28 | done: | |
29 | retr_d %f0 | |
30 | epilog | |
31 | ||
32 | /* | |
33 | * int main(int argc, char *argv[]) { | |
34 | * unsigned long v; | |
35 | * double d; | |
36 | * if (argc == 2) | |
37 | * v = strtoul(argv[1], NULL, 0); | |
38 | * else | |
39 | * v = 32; | |
40 | * d = factorial(v); | |
41 | * printf("%.0lf\n", d); | |
42 | * return 0; | |
43 | * } | |
44 | */ | |
45 | main: | |
46 | prolog | |
47 | arg $argc | |
48 | arg $argv | |
49 | getarg %r0 $argc | |
50 | bnei default %r0 2 | |
51 | getarg %v0 $argv | |
52 | ldxi %r0 %v0 $(__WORDSIZE >> 3) | |
53 | prepare | |
54 | pushargr %r0 | |
55 | pushargi 0 | |
56 | pushargi 0 | |
57 | finishi @strtoul | |
58 | retval %v0 | |
59 | jmpi call | |
60 | default: | |
61 | movi %v0 32 | |
62 | call: | |
63 | prepare | |
64 | pushargr %v0 | |
65 | finishi factorial | |
66 | retval_d %f0 | |
67 | prepare | |
68 | pushargi str | |
69 | ellipsis | |
70 | pushargr_d %f0 | |
71 | finishi @printf | |
72 | reti 0 | |
73 | epilog |