-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.c
More file actions
73 lines (59 loc) · 1.56 KB
/
assembler.c
File metadata and controls
73 lines (59 loc) · 1.56 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Lmax 100
#define none 101
char get_opcode (char *inst) // loads the opcode of each specific instruction
{
if(strcmp(inst,"HLT") == 0)
return 0;
if(strcmp(inst,"LA") == 0)
return 1;
if(strcmp(inst,"LB") == 0)
return 2;
if(strcmp(inst,"ADD") == 0)
return 3;
if(strcmp(inst,"SUB") == 0)
return 4;
if(strcmp(inst,"MUL") == 0)
return 5;
if(strcmp(inst,"PRN") == 0)
return 6;
if(strcmp(inst,"JMP") == 0)
return 7;
if(strcmp(inst,"JZ") == 0)
return 8;
if(strcmp(inst,"INP_A") == 0)
return 9;
if(strcmp(inst,"INP_B") == 0)
return 10;
return none;
}
int main(int argc , char *argv[])
{
if(argc > 3)
{
printf("insufficient arguments passed !!");
exit(1);
}
FILE *input = fopen(argv[1],"r"); // read only mode
FILE *output = fopen(argv[2],"wb"); // writing in output file in binary
char line[Lmax];
while (fgets(line,sizeof(line),input))
{
line[strcspn(line,"\n\r")] = 0;
if (strlen(line) == 0)
continue;
char opcode = get_opcode(line); // conversion to binary
if(opcode == none)
{
printf("Invalid instruction : %s\n",line);
continue;
}
fwrite(&opcode ,sizeof(char), 1 ,output); // writing in the output binary file
}
fclose(input);
fclose(output);
printf("Assembling the program is done ");
return 0;
}