-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembler.c
More file actions
175 lines (133 loc) · 3.69 KB
/
Copy pathassembler.c
File metadata and controls
175 lines (133 loc) · 3.69 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#define MAX_PROGRAM 256
#define MAX_LABELS 64
#define MAX_LINE 128
/* ===================== DATA STRUCTURES ===================== */
typedef struct {
char name[32];
uint8_t address;
} Label;
typedef struct {
char mnemonic[4];
uint8_t opcode;
uint8_t size;
} Instruction;
/* ===================== GLOBALS ===================== */
Label labels[MAX_LABELS];
int label_count = 0;
uint8_t program[MAX_PROGRAM];
uint8_t pc = 0;
/* ===================== OPCODE TABLE ===================== */
Instruction table[] = {
{"LDA", 0x01, 2},
{"STA", 0x02, 2},
{"ADD", 0x03, 2},
{"SUB", 0x04, 2},
{"JMP", 0x05, 2},
{"JZ", 0x06, 2},
{"OUT", 0x07, 1},
{"HLT", 0xFF, 1}
};
int table_size = sizeof(table) / sizeof(table[0]);
/* ===================== UTILITIES ===================== */
Instruction* find_instruction(char *name) {
for (int i = 0; i < table_size; i++)
if (strcmp(table[i].mnemonic, name) == 0)
return &table[i];
return NULL;
}
int find_label(char *name) {
for (int i = 0; i < label_count; i++)
if (strcmp(labels[i].name, name) == 0)
return labels[i].address;
return -1;
}
void trim_comment(char *line) {
for (int i = 0; line[i]; i++) {
if (line[i] == ';') {
line[i] = 0;
return;
}
}
}
/* ===================== PASS 1 ===================== */
/* Find label addresses */
void pass1(FILE *file) {
char line[MAX_LINE];
pc = 0;
while (fgets(line, sizeof(line), file)) {
trim_comment(line);
char *token = strtok(line, " \t\n");
if (!token) continue;
/* label */
if (token[strlen(token) - 1] == ':') {
token[strlen(token) - 1] = 0;
strcpy(labels[label_count].name, token);
labels[label_count].address = pc;
label_count++;
continue;
}
Instruction *inst = find_instruction(token);
if (inst)
pc += inst->size;
}
}
/* ===================== PASS 2 ===================== */
/* Generate machine code */
void pass2(FILE *file) {
char line[MAX_LINE];
pc = 0;
while (fgets(line, sizeof(line), file)) {
trim_comment(line);
char *token = strtok(line, " \t\n");
if (!token) continue;
if (token[strlen(token) - 1] == ':')
continue;
Instruction *inst = find_instruction(token);
if (!inst) continue;
program[pc++] = inst->opcode;
if (inst->size == 2) {
char *op = strtok(NULL, " \t\n");
int value;
if (isalpha(op[0])) {
value = find_label(op);
if (value < 0) {
printf("Unknown label: %s\n", op);
exit(1);
}
} else {
value = strtol(op, NULL, 0);
}
program[pc++] = (uint8_t)value;
}
}
}
/* ===================== BINARY DISPLAY ===================== */
void print_binary(uint8_t byte) {
for (int i = 7; i >= 0; i--)
printf("%d", (byte >> i) & 1);
}
/* ===================== MAIN ===================== */
int main() {
FILE *file = fopen("program.asm", "r");
if (!file) {
printf("ERROR: program.asm not found\n");
return 1;
}
pass1(file);
rewind(file);
pass2(file);
fclose(file);
printf("\n===== MACHINE CODE OUTPUT =====\n\n");
for (int i = 0; i < pc; i++) {
printf("0x%02X ", i);
print_binary(program[i]);
printf("\n");
}
printf("\nProgram size: %d bytes\n", pc);
return 0;
}