-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_iterativo_funcao.c
180 lines (171 loc) · 6.57 KB
/
fibonacci_iterativo_funcao.c
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
// Aluno: Igor Lima Rocha
#define CXMAX 200
#define STACKSIZE 500
const int amax = 2047, levmax = 3;
enum fct {LIT, OPR, LOD, STO, CAL, INT, JMP, JPC};
typedef struct {
enum fct f;
int l;
int a;
} Instruction;
Instruction code[CXMAX];
int s[STACKSIZE];
int p, b, t;
int base(int l){
int b1;
b1 = b;
while(l > 0){
b1 = s[b1];
l = l-1;
}
return b1;
}
int main(){
int _i = 0;
// main():
code[_i].f = INT; code[_i].l = 0; code[_i++].a = 4; // Inicia memoria
code[_i].f = LIT; code[_i].l = 0; code[_i++].a = 5; // Configura maximo
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 4+7; // Passa maximo por parametro pra função
code[_i].f = CAL; code[_i].l = 0; code[_i++].a = 6; // Chama funcao
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 4+5; // Pega o resultado
code[_i].f = OPR; code[_i].l = 0; code[_i++].a = 0; // FIM
// fib(x):
code[_i].f = INT; code[_i].l = 0; code[_i++].a = 8; // Inicia memoria
code[_i].f = LIT; code[_i].l = 0; code[_i++].a = 0; // Inicia T1
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 3; // Salva T1
code[_i].f = LIT; code[_i].l = 0; code[_i++].a = 1; // Inicia T2
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 4; // Salva T2
code[_i].f = LIT; code[_i].l = 0; code[_i++].a = 2; // Inicia Contador
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 6; // Salva contador
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 7; // Salva max
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 3; // Inicio do loop (load T1)-----
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 4; // Load T2
code[_i].f = OPR; code[_i].l = 0; code[_i++].a = 2; // T1 + T2
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 5; // Salva o fib
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 4; // Load T2
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 3; // T1 = T2
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 5; // Load Fib
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 4; // T2 = Fib
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 6; // Load Contador
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 7; // Load Máximo
code[_i].f = OPR; code[_i].l = 0; code[_i++].a = 10; // Menor que
code[_i].f = JPC; code[_i].l = 0; code[_i++].a = 30; // Saida do loop -------
code[_i].f = LOD; code[_i].l = 0; code[_i++].a = 6; // Load Contador
code[_i].f = OPR; code[_i].l = 0; code[_i++].a = 2; // Contador + 1
code[_i].f = STO; code[_i].l = 0; code[_i++].a = 6; // Salva Contador
code[_i].f = JMP; code[_i].l = 0; code[_i++].a = 14; // Fim do loop
code[_i].f = OPR; code[_i].l = 0; code[_i++].a = 0;
// PCode Machine:
Instruction i;
{
printf(" start pl/0 ");
t = 0;
b = 1;
p = 0;
s[1] = 0;
s[2] = 0;
s[3] = 0;
printf("\n| t | b | p |");
do {
i = code[p];
printf("\n|%2d | %2d | %2d | ", t, b, p);
p = p+1;
switch (i.f){
case LIT:
t = t+1;
s[t] = i.a;
break;
case OPR:
switch(i.a){
case 0: // Return
t = b - 1;
p = s[t + 3];
b = s[t + 2];
break;
case 1: // Negativo
s[t] = -s[t];
break;
case 2: // Soma
t = t - 1;
s[t] = s[t] + s[t + 1];
break;
case 3: // Subtração
t = t - 1;
s[t] = s[t] - s[t + 1];
break;
case 4: // Multiplicação
t = t - 1;
s[t] = s[t] * s[t + 1];
break;
case 5: // Divisão
t = t - 1;
s[t] = s[t] / s[t + 1];
break;
case 6:
s[t] = (s[t]) % 2;
break;
case 8:
t = t - 1;
s[t] = (s[t] == s[t + 1]);
break;
case 9:
t = t - 1;
s[t] = (s[t] != s[t + 1]);
break;
case 10:
t = t - 1;
s[t] = (s[t] < s[t + 1]);
break;
case 11:
t = t - 1;
s[t] = (s[t] >= s[t + 1]);
break;
case 12:
t = t - 1;
s[t] = (s[t] > s[t + 1]);
break;
case 13:
t = t - 1;
s[t] = (s[t] <= s[t + 1]);
break;
}
break;
case LOD:
t = t + 1;
s[t] = s[base(i.l) + i.a];
break;
case STO:
s[base(i.l) + i.a] = s[t];
t = t - 1;
break;
case CAL:
{
s[t + 1] = base(i.l);
s[t + 2] = b;
s[t + 3] = p;
b = t + 1;
p = i.a;
}
break;
case INT:
t = t + i.a;
break;
case JMP:
p = i.a;
break;
case JPC:
if (s[t] == 0){
p = i.a;
t = t - 1;
}
break;
}
for(int j = 1; j <= t; j++){
printf("[%d]", s[j]);
}
} while(p != 0);
}
return 0;
}