-
Notifications
You must be signed in to change notification settings - Fork 0
/
backendOfCompiler.c
63 lines (55 loc) · 1.52 KB
/
backendOfCompiler.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
#include<stdio.h>
#include<string.h>
char arg1[10], arg2[10], op[10], res[10], f[100];
int main(){
FILE *fp1, *fp2;
printf("Enter the input filename: ");
scanf("%[^\n]s", f);
fp1 = fopen(f, "r");
fp2 = fopen("output.txt", "w");
if (fp1 == NULL)
{
printf("Error: Could not open input file.\n");
return 1;
}
if (fp2 == NULL)
{
printf("Error: Could not open output file.\n");
fclose(fp1);
return 1;
}
while(!feof(fp1)){
fscanf(fp1, "%s%s%s%s", op, arg1, arg2, res);
if(strcmp(op, "+") == 0){
fprintf(fp2, "\nMOV R0, %s", arg1);
fprintf(fp2, "\nADD R0, %s", arg2);
fprintf(fp2, "\nMOV %s, R0", res);
}
if (strcmp(op, "*") == 0){
fprintf(fp2, "\nMOV R0, %s", arg1);
fprintf(fp2, "\nMUL R0, %s", arg2);
fprintf(fp2, "\nMOV %s, R0", res);
}
if (strcmp(op, "/") == 0)
{
fprintf(fp2, "\nMOV R0, %s", arg1);
fprintf(fp2, "\nDIV R0, %s", arg2);
fprintf(fp2, "\nMOV %s, R0", res);
}
if (strcmp(op, "-") == 0)
{
fprintf(fp2, "\nMOV R0, %s", arg1);
fprintf(fp2, "\nSUB R0, %s", arg2);
fprintf(fp2, "\nMOV %s, R0", res);
}
if (strcmp(op, "=") == 0)
{
fprintf(fp2, "\nMOV R0, %s", arg1);
fprintf(fp2, "\nMOV %s, R0", res);
}
}
fclose(fp1);
fclose(fp2);
getchar();
return 0;
}