-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass2.c
124 lines (120 loc) · 3.57 KB
/
pass2.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
#include "pass1.h"
void pass2(char *file)
{
pass1(file);
printf("Running pass2 of Assembler...\n");
FILE *input;
FILE *output;
input=fopen("inter.txt","r");
// input=fopen(file,"r");
file[strlen(file)-3]='o';
file[strlen(file)-2]='b';
file[strlen(file)-1]='j';
file[strlen(file)]='\0';
char *fileOut=(char*)malloc(20);
strcpy(fileOut,file);
output=fopen(fileOut,"w");
char *line=(char*)malloc(50);
fgets(line,50,input);
tokenize(line);
while(strcmp(label,"START")!=0)
{
fgets(line,50,input);
line[strlen(line)-1] = '\0';
tokenize(line);
}
// fprintf(output,"\n");
// int i=1;
while(strcmp(label,"END")!=0)
{
fgets(line,50,input);
tokenize(line);
if(strcmp(label,"END")==0) break;
// printf("iter: %d\n",i++);
// printf("%s\n%s\n%s\n",label,opcode,operand);
if(label[0]==';') continue;
char *OP=search(opcode);
// printf("%s\n",OP);
if(OP!="NULL")
{
fprintf(output,"\n%s",OP);
if(operand[0]!='\0')
{
if(search2(operand)==-1)
{
int lenop=strlen(operand);
// printf("%d",lenop);
if(lenop<=3)
{
fprintf(output,"\n%s",operand);
}
else
{
fprintf(output,"\n%c%c 1",operand[2],operand[3]);
fprintf(output,"\n%c%c 1",operand[0],operand[1]);
}
}
else
{
int var=search2(operand);
// printf("%d\n",var);
if(var<=9)
{
fprintf(output,"\n0%d 0\n00 0",var);
}
else if(var<100)
{
char* l;
l=(char*)malloc(5);
decToHexa(var,l);
if(strlen(l)<2)
{
fprintf(output,"\n0%s 0\n00 0",l);
}
else fprintf(output,"\n%s 0\n00 0",l);
}
else
{
char *l,*h;
l=(char*)malloc(5);
h=(char*)malloc(5);
// printf("X");
decToHexa(var%100,l);
// printf("X");
decToHexa(var/100,h);
if(strlen(l)<3)
{
fprintf(output,"\n0%s 0",l);
}
else fprintf(output,"\n%s 0",l);
if(strlen(h)<3)
{
fprintf(output,"\n0%s 0",h);
}
else fprintf(output,"\n%s 0",h);
}
}
}
else
{
//fprintf(output,"0\n");
//assemble the object code instruction
}
}
else{
fprintf(output,"\n0");
printf("ERROR2!!");
return;
}
// fgets(line,50,input);
// tokenize(line);
}
printf("Execution of pass2 of Assembler Completed!\n");
fclose(input);
fclose(output);
}
int main(int argc,char **argv)
{
pass2(argv[1]);
return 0;
}