/
fatorial.asm
127 lines (112 loc) · 2.15 KB
/
fatorial.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
.export
.data
zero: 0
a: 0
b: 1 // fact
c: 12 // n
tmp: 0
tmpb: 0
tmpc: 0
sig: 0
one: 1
minusOne: -1
max: 0
inc: 0
.text
start:
zero zero factLoop // goto factLoop;
exit:
zero zero exit
factLoop:
zero c exit // if (c <= 0) goto exit;
zero zero mult // a = b*c;
back:
// b = a;
b b
a zero
zero b
zero zero
one c // c--;
zero zero factLoop // goto factLoop;
mult:
a a // a = 0;
sig sig // sig = 0;
// if (-b <= 0) goto bpos;
tmp tmp
b tmp bpos
minusOne sig // sig++;
// tmpb = -b;
tmpb tmpb
b tmpb
zero zero checkc // goto check;
bpos:
// tmpb = b;
tmpb tmpb
b zero
zero tmpb
zero zero
checkc:
// if (-c <= 0) goto cpos;
tmp tmp
c tmp cpos
minusOne sig // sig++;
// tmpc = -c;
tmpc tmpc
c tmpc
zero zero multMax // goto multMax;
cpos:
// tmpc = c;
tmpc tmpc
c zero
zero tmpc
zero zero
multMax:
// if (tmpb - tmpc <= zero) goto loopB;
tmp tmp
tmpb zero
zero tmp
zero zero
tmpc tmp loopB
zero zero loopC // goto loopC;
loopB:
// max = tmpb;
max max
tmpb zero
zero max
zero zero
// inc = -tmpc;
inc inc
tmpc inc multLoop // goto multLoop;
loopC:
// max = tmpc;
max max
tmpc zero
zero max
zero zero
// inc = -tmpb;
inc inc
tmpb inc
multLoop:
zero max multSig // if (max <= 0) goto multSig;
inc a // a = a - (-tmpc or -tmpb);
one max // max--;
zero zero multLoop // goto multLoop;
multSig:
one sig checkgeq // sig--; if (sig <= 0) goto checkgeq;
zero zero multDone // goto multDone;
checkgeq:
// if (-sig <= 0) goto multNeg;
tmp tmp
sig tmp multNeg
zero zero multDone // goto multDone;
multNeg:
// tmp = a;
tmp tmp
a zero
zero tmp
zero zero
// a = -tmp;
a a
tmp a
multDone:
zero zero back