-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.c
128 lines (108 loc) · 2.99 KB
/
assembler.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE;
int main(void)
{
FILE * fpw, *fpr;
char instruc [10], filename [15], fileout[15];
int line = 0;
printf("enter input file: (specify type e.g. .txt \n");
scanf("%s",filename);
fpr= fopen(filename, "r");
for(int j = 0; filename[j]!='.';j++)
{
fileout[j]=filename[j];
}
fpw= fopen(strcat(fileout,"_out"), "w");
fprintf(fpw,"v2.0 raw\n0\n" );
while((fscanf(fpr,"%s",instruc))!=EOF){
int addr =0;
int iaddr =0;
char ass_instruc [10]="";
for (int x=0; instruc[x]!='\0'; x++ ) {
if ((instruc[x]>='a' && instruc[x]<='z') || (instruc[x]>='A' && instruc[x]<='Z') ){
ass_instruc [iaddr] = instruc[x];
iaddr++;
}
else if(instruc[x]>='0' && instruc[x]<='9') {
addr =(addr * 10) + (instruc[x]-48);
}
}
if (strcmp(ass_instruc, "LDA") == 0){
fprintf(fpw,"1%x \n",addr);
}
else if (strcmp(ass_instruc, "ADD") == 0){
fprintf(fpw,"2%x \n",addr);
}
else if (strcmp(ass_instruc, "J") == 0){
fprintf(fpw,"3%x \n",addr);
}
else if (strcmp(ass_instruc, "STA") == 0){
fprintf(fpw,"4%x \n",addr);
}
else if (strcmp(ass_instruc, "LDI") == 0){
fprintf(fpw,"5%x \n",addr);
}
else if (strcmp(ass_instruc, "SUB") == 0){
fprintf(fpw,"6%x \n",addr);
}
else if (strcmp(ass_instruc, "OUT") == 0){
fprintf(fpw,"e%x \n",0);
}
else if (strcmp(ass_instruc, "HLT") == 0){
fprintf(fpw,"f%x \n",0);
}
else if (strcmp(ass_instruc, "JC") == 0){
fprintf(fpw,"a%x \n",addr);
}
else if (strcmp(ass_instruc, "JZ") == 0){
fprintf(fpw,"9%x \n",addr);
}
else if (strcmp(ass_instruc, "JCZ") == 0){
fprintf(fpw,"8%x \n",addr);
}
else if (strcmp(ass_instruc, "I") == 0){
fprintf(fpw,"8%x \n",addr);
}
else {
printf("\n ERROR: Invalid instruction at %d \n", line );
}
if(ass_instruc=="J"){
if (addr=="1"){
(strcmp(ass_instruc, "LDA") == 0);
}
else if(addr="2"){
(strcmp(ass_instruc, "ADD") == 0);
}
else if(addr="4"){
(strcmp(ass_instruc, "STA") == 0);
}
else if(addr="5"){
(strcmp(ass_instruc, "LDI") == 0);
}
else if(addr="6"){
(strcmp(ass_instruc, "SUB") == 0);
}
else if(addr="e"){
(strcmp(ass_instruc, "OUT") == 0);
}
else if(addr="f"){
(strcmp(ass_instruc, "HLT") == 0);
}
else if(addr="a"){
(strcmp(ass_instruc, "JC") == 0);
}
else if(addr="9"){
(strcmp(ass_instruc, "JZ") == 0);
}
else if(addr="8"){
(strcmp(ass_instruc, "JCZ") == 0);
}
r
}
line++;
}
fclose(fpr);
fclose(fpw);
}