-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.cpp
143 lines (121 loc) · 4.93 KB
/
asm.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <assert.h>
#include <string.h>
#include "asm.h"
ASM_ERROR asm_read_file(PROGRAM_CODE* programCodeInfo, const int argc, const char* argv[])
{
asm_open_input_file (programCodeInfo, argc, argv);
asm_count_symbols_in_file(programCodeInfo);
asm_read_commands_in_file(programCodeInfo);
asm_create_output_file (programCodeInfo);
return NONE;
}
ASM_ERROR asm_open_input_file(PROGRAM_CODE* programCodeInfo, const int argc, const char* argv[])
{
if (argc < 2)
{
return INPUT_FILE_ALLOCATION_ERROR;
}
programCodeInfo->input_file = fopen(argv[1], "rb");
if (!programCodeInfo->input_file)
{
return INPUT_FILE_ALLOCATION_ERROR;
}
return NONE;
}
ASM_ERROR asm_count_symbols_in_file(PROGRAM_CODE* programCodeInfo)
{
assert(programCodeInfo && "null pointer on programCodeInfo in asm_count_symbols_in_file\n");
fseek(programCodeInfo->input_file, 0, SEEK_END);
programCodeInfo->size = ftell(programCodeInfo->input_file);
fseek(programCodeInfo->input_file, 0, SEEK_SET);
if (!programCodeInfo->input_file)
{
// TODO I WILL MAKE IT BRIGHT!!!
return INPUT_FILE_ALLOCATION_ERROR;
}
if (programCodeInfo->size < 0)
{
// TODO add return in log file
return INVALID_SIZE;
}
return NONE;
}
ASM_ERROR asm_remove_newline_symbols(PROGRAM_CODE* programCodeInfo)
{
assert(programCodeInfo && "null pointer on programCodeInfo in asm_remove_newline_symbols\n");
programCodeInfo->words_quantity = 0;
for (ssize_t symbol_index = 0; symbol_index < programCodeInfo->size; symbol_index++)
{
if (programCodeInfo->program_code[symbol_index] == '\n' ||
programCodeInfo->program_code[symbol_index] == ' ')
{
programCodeInfo->words_quantity++;
programCodeInfo->program_code[symbol_index] = '\0';
}
}
return NONE;
}
ASM_ERROR asm_count_words_in_file(PROGRAM_CODE* programCodeInfo)
{
assert(programCodeInfo && "null pointer on programCodeInfo in asm_count_words_in_file\n");
programCodeInfo->word_pointer = (char** ) calloc(programCodeInfo->words_quantity + 1, sizeof(char*));
ssize_t word_pointer_index = 0;
programCodeInfo->word_pointer[word_pointer_index++] = programCodeInfo->program_code;
for (ssize_t symbol_index = 0; symbol_index < programCodeInfo->size - 1; symbol_index++)
{
if (programCodeInfo->program_code[symbol_index] == '\0')
{
programCodeInfo->word_pointer[word_pointer_index++] = &programCodeInfo->program_code[symbol_index + 1];
}
}
return NONE;
}
ASM_ERROR asm_read_commands_in_file(PROGRAM_CODE* programCodeInfo)
{
assert(programCodeInfo && "null pointer on programCodeInfo in asm_read_commands_in_file\n");
programCodeInfo->program_code = (char*) calloc(programCodeInfo->size + 1, sizeof(char));
if (!programCodeInfo->program_code) {
// TODO add clever verification
return PROGRAM_CODE_ALLOCATION_ERROR;
}
fread(programCodeInfo->program_code, sizeof(char), programCodeInfo->size, programCodeInfo->input_file);
asm_remove_newline_symbols(programCodeInfo);
asm_count_words_in_file (programCodeInfo);
return NONE;
}
ASM_ERROR asm_command_switch(PROGRAM_CODE* programCodeInfo, const char* command)
{
if (!programCodeInfo)
{
return STRUCT_ALLOCATION_ERROR;
}
if (strcmp(command, "hlt" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _hlt ); return NONE;}
if (strcmp(command, "out" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _out ); return NONE;}
if (strcmp(command, "push" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _push ); return NONE;}
if (strcmp(command, "pop" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _pop ); return NONE;}
if (strcmp(command, "add" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _add ); return NONE;}
if (strcmp(command, "sub" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _sub ); return NONE;}
if (strcmp(command, "mult" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _mult ); return NONE;}
if (strcmp(command, "div" ) == 0) {fprintf(programCodeInfo->output_file, "%d ", _div ); return NONE;}
else
{
fprintf(programCodeInfo->output_file, "%s ", command);
}
return NONE;
}
ASM_ERROR asm_create_output_file(PROGRAM_CODE* programCodeInfo)
{
assert(programCodeInfo && "null pointer on programCodeInfo in asm_create_output_file\n");
programCodeInfo->output_file = fopen("a.asm", "w");
if (!programCodeInfo->output_file)
{
printf("null pointer on output_file in asm_create_output_file\n");
return OUTPUT_FILE_ALLOCATION_ERROR;
}
for (ssize_t string_index = 0; string_index < programCodeInfo->words_quantity; string_index++)
{
asm_command_switch(programCodeInfo, programCodeInfo->word_pointer[string_index]);
}
fclose(programCodeInfo->output_file);
return NONE;
}