1+ #include <stdlib.h>
2+ #include <stdio.h>
3+ #include "stack.h"
4+ #include "instruction.h"
5+ #include <stdbool.h>
6+ #include <string.h>
7+
8+
9+ bool isEqualString (char * str1 , char * str2 ){
10+ return strcmp (str1 , str2 ) == 0 ;
11+ }
12+
13+ int searchLabelIndex (InstructionVector * instructions , int numberOfInstructions , char * label ){
14+ for (int i = 0 ; i < numberOfInstructions ; i ++ ){
15+ if (isEqualString (instructions [i ].label , label ))
16+ return i ;
17+ }
18+ return -1 ;
19+ }
20+
21+ void executeProgram (InstructionVector * instructions , int numberOfInstructions ) {
22+ int stack [500 ] = {0 }, addr = 0 , aux , instructionAddr = 0 , n , m ;
23+
24+ while (instructionAddr != numberOfInstructions ){
25+ printf ("\n\nInstruction: %s %s %s %s" , instructions [instructionAddr ].label , instructions [instructionAddr ].instruction , instructions [instructionAddr ].param1 , instructions [instructionAddr ].param2 );
26+ getchar ();
27+ if (isEqualString (instructions [instructionAddr ].instruction , "LDC " )) {
28+ addr ++ ;
29+ stack [addr ] = toInt (instructions [instructionAddr ].param1 );
30+ } else if (isEqualString (instructions [instructionAddr ].instruction , "LDV " )) {
31+ addr ++ ;
32+ stack [addr ] = stack [toInt (instructions [instructionAddr ].param1 )];
33+ } else if (isEqualString (instructions [instructionAddr ].instruction , "ADD " )) {
34+ stack [addr - 1 ] = stack [addr - 1 ] + stack [addr ];
35+ addr -- ;
36+ } else if (isEqualString (instructions [instructionAddr ].instruction , "SUB " )) {
37+ stack [addr - 1 ] = stack [addr - 1 ] - stack [addr ];
38+ addr -- ;
39+ } else if (isEqualString (instructions [instructionAddr ].instruction , "MULT " )) {
40+ stack [addr - 1 ] = stack [addr - 1 ] * stack [addr ];
41+ addr -- ;
42+ } else if (isEqualString (instructions [instructionAddr ].instruction , "DIVI " )) {
43+ stack [addr - 1 ] = stack [addr - 1 ] / stack [addr ];
44+ addr -- ;
45+ } else if (isEqualString (instructions [instructionAddr ].instruction , "INV " )) {
46+ stack [addr ] = - stack [addr ];
47+ } else if (isEqualString (instructions [instructionAddr ].instruction , "AND " )) {
48+ if (stack [addr - 1 ] == 1 && stack [addr ] == 1 ){
49+ stack [addr - 1 ] = 1 ;
50+ }else {
51+ stack [addr - 1 ] = 0 ;
52+ }
53+ addr -- ;
54+ } else if (isEqualString (instructions [instructionAddr ].instruction , "OR " )) {
55+ if (stack [addr - 1 ] == 1 || stack [addr ] == 1 ){
56+ stack [addr - 1 ] = 1 ;
57+ }else {
58+ stack [addr - 1 ] = 0 ;
59+ }
60+ addr -- ;
61+ } else if (isEqualString (instructions [instructionAddr ].instruction , "NEG " )) {
62+ stack [addr ] = 1 - stack [addr ];
63+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CME " )) {
64+ if (stack [addr - 1 ] < stack [addr ]){
65+ stack [addr - 1 ] = 1 ;
66+ }else {
67+ stack [addr - 1 ] = 0 ;
68+ }
69+ addr -- ;
70+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CMA " )) {
71+ if (stack [addr - 1 ] > stack [addr ]){
72+ stack [addr - 1 ] = 1 ;
73+ }else {
74+ stack [addr - 1 ] = 0 ;
75+ }
76+ addr -- ;
77+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CEQ " )) {
78+ if (stack [addr - 1 ] == stack [addr ]){
79+ stack [addr - 1 ] = 1 ;
80+ }else {
81+ stack [addr - 1 ] = 0 ;
82+ }
83+ addr -- ;
84+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CDIF " )) {
85+ if (stack [addr - 1 ] != stack [addr ]){
86+ stack [addr - 1 ] = 1 ;
87+ }else {
88+ stack [addr - 1 ] = 0 ;
89+ }
90+ addr -- ;
91+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CMEQ " )) {
92+ if (stack [addr - 1 ] <= stack [addr ]){
93+ stack [addr - 1 ] = 1 ;
94+ }else {
95+ stack [addr - 1 ] = 0 ;
96+ }
97+ addr -- ;
98+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CMAQ " )) {
99+ if (stack [addr - 1 ] >= stack [addr ]){
100+ stack [addr - 1 ] = 1 ;
101+ }else {
102+ stack [addr - 1 ] = 0 ;
103+ }
104+ addr -- ;
105+ } else if (isEqualString (instructions [instructionAddr ].instruction , "STR " )) {
106+ stack [toInt (instructions [instructionAddr ].param1 )] = stack [addr ];
107+ addr -- ;
108+ } else if (isEqualString (instructions [instructionAddr ].instruction , "JMP " )) {
109+ aux = searchLabelIndex (instructions , numberOfInstructions , instructions [instructionAddr ].param1 );
110+ instructionAddr = aux ;//Instrução logo após o "LABEL NULL";
111+ } else if (isEqualString (instructions [instructionAddr ].instruction , "JMPF " )) {
112+ if (stack [addr ] == 0 ){
113+ aux = searchLabelIndex (instructions , numberOfInstructions , instructions [instructionAddr ].param1 );
114+ instructionAddr = aux ;//Instrução logo após o "LABEL NULL";
115+ }
116+ addr -- ;
117+ } else if (isEqualString (instructions [instructionAddr ].instruction , "RD " )) {
118+ addr ++ ;
119+ scanf ("%d" , & aux );
120+ stack [addr ] = aux ;
121+ } else if (isEqualString (instructions [instructionAddr ].instruction , "PRN " )) {
122+ printf ("\n\n\n\n\n\n%d" , stack [addr ]);
123+ addr -- ;
124+ } else if (isEqualString (instructions [instructionAddr ].instruction , "START " )) {
125+ addr -- ;
126+ } else if (isEqualString (instructions [instructionAddr ].instruction , "ALLOC " )) {
127+ printf ("\nANTES ALLOC\n\n" );
128+ printStack (stack , addr );
129+ m = toInt (instructions [instructionAddr ].param1 );
130+ n = toInt (instructions [instructionAddr ].param2 );
131+ for (int k = 0 ; k < n ; k ++ ){
132+ addr ++ ;
133+ stack [addr ] = stack [m + k ];
134+ }
135+ printf ("\nDEPOIS ALLOC\n\n" );
136+ printStack (stack , addr );
137+ } else if (isEqualString (instructions [instructionAddr ].instruction , "DALLOC " )) {
138+ printf ("\nANTES DALLOC\n\n" );
139+ printStack (stack , addr );
140+ m = toInt (instructions [instructionAddr ].param1 );
141+ n = toInt (instructions [instructionAddr ].param2 );
142+ for (int k = n - 1 ; k >= 0 ; k -- ){
143+ stack [m + k ] = stack [addr ];
144+ addr -- ;
145+ }
146+ printf ("\nDEPOIS DALLOC\n\n" );
147+ printStack (stack , addr );
148+ } else if (isEqualString (instructions [instructionAddr ].instruction , "HLT " )) {
149+ return ;
150+ } else if (isEqualString (instructions [instructionAddr ].instruction , "CALL " )) {
151+ addr ++ ;
152+ stack [addr ] = instructionAddr + 1 ;
153+ aux = searchLabelIndex (instructions , numberOfInstructions , instructions [instructionAddr ].param1 );
154+ instructionAddr = aux ;//Instrução logo após o "LABEL NULL";
155+ } else if (isEqualString (instructions [instructionAddr ].instruction , "RETURN " )) {
156+ instructionAddr = (stack [addr ] - 1 ); //Atualiza no final
157+ addr -- ;
158+ } else if (isEqualString (instructions [instructionAddr ].instruction , "RETURNF " )) { //VERIFICAR SE Ë ESSE MESMO O RETORNO QUE PRECISA
159+ instructionAddr = stack [addr ] - 1 ;
160+ addr -- ;
161+ }
162+
163+ instructionAddr ++ ;
164+ }
165+ }
166+
167+ int main () {
168+ FILE * sourceFile ;
169+ int instructionAddr = 0 ;
170+ sourceFile = fopen ("AssemblyCode.obj" , "r" );
171+
172+ if (!sourceFile ) {
173+ printf ("\nError! File could not be opened!\n" );
174+ exit (1 );
175+ }
176+
177+ InstructionVector instructions [2048 ] = {0 };
178+ // Stack *stack = NULL;
179+
180+ readInstructions (sourceFile , instructions , & instructionAddr );
181+
182+ //printInstructions(instructions, instructionAddr);
183+
184+ executeProgram (instructions , instructionAddr );
185+
186+ fclose (sourceFile );
187+ // freeInstructions(&instructions);
188+ // freeStack(&stack);
189+
190+ return 0 ;
191+ }
0 commit comments