-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
128 lines (114 loc) · 3.24 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define instructionQuantity 4
enum {
MOV,
ADD,
SUB,
JNE
};
char *instructionList[instructionQuantity] = {"MOV", "ADD", "SUB", "JNE"};
int a = 0;
int b = 0;
int c = 0;
int d = 0;
bool srcOrImm(char *str) { //if num 0 | reg 1
int i = 0;
if (str[0] == '-') ++i;
for (; i < strlen(str); i++) {
if (str[i] - '0' > 9) return 1;
}
return 0;
}
int strToNum(char *str) {
int i = 0;
int res = 0;
int sign = 1;
if (str[0] == '-') {
sign = -1;
++i;
}
for (; i < strlen(str); i++) {
res *= 10;
res += str[i] - '0';
}
return res * sign;
}
int *returnAddressOfSpecifiedRegister(char reg) {
switch (reg) {
case 'a':
return &a;
case 'b':
return &b;
case 'c':
return &c;
case 'd':
return &d;
default:
printf("Called for not existing register");
exit(0x123);
}
}
int returnInstruction(char *instruction) {
for (int i = 0; i < instructionQuantity; i++) {
if (!strcmp(instruction, instructionList[i])) return i;
}
printf("None existing instruction were provided %s", instruction);
exit(321);
}
int operandValue(char *operand) {
if (srcOrImm(operand)) {
return *returnAddressOfSpecifiedRegister(operand[0]);
} else return strToNum(operand);
}
int setOperands(char ***callStack, char *instruction) {
static int sizeOfStack = 0;
int j = 0;
int prevPos = 0;
for (int i = 0; i <= strlen(instruction); i++) {
if (instruction[i] == ' ' || instruction[i] == '\000') {
callStack[sizeOfStack] = realloc(callStack[sizeOfStack], (j + 1) * sizeof(char *));
callStack[sizeOfStack][j] = calloc(i + 1, sizeof(char));
memcpy(callStack[sizeOfStack][j], &instruction[prevPos], i - prevPos);
++j;
prevPos = i + 1;
}
}
return ++sizeOfStack;
}
int main() {
scanf("%d%d%d%d", &a, &b, &c, &d);
int n = 0;
scanf("%d", &n);
fgetc(stdin);
char ***callStack = calloc(n, sizeof(char *));
for (int i = 0; i < n; i++) {
char instruction[17];
scanf("%[^\n]s", instruction);
fgetc(stdin);
setOperands(callStack, instruction);
}
for (int i = 0; i < n; i++) {
switch (returnInstruction(callStack[i][0])) {
case MOV:
(*returnAddressOfSpecifiedRegister(callStack[i][1][0])) = operandValue(callStack[i][2]);
break;
case ADD:
(*returnAddressOfSpecifiedRegister(callStack[i][1][0])) =
operandValue(callStack[i][2]) + operandValue(callStack[i][3]);
break;
case SUB:
(*returnAddressOfSpecifiedRegister(callStack[i][1][0])) =
operandValue(callStack[i][2]) - operandValue(callStack[i][3]);
break;
case JNE:
if (operandValue(callStack[i][2]) != operandValue(callStack[i][3])) {
i = operandValue(callStack[i][1]) - 1;
} else break;
}
}
printf("%d %d %d %d\n", a, b, c, d);
return 0;
}