This commit is contained in:
2026-07-16 12:44:08 +10:00
4 changed files with 73 additions and 1 deletions

9
src/assembler/assemble.c Normal file
View File

@@ -0,0 +1,9 @@
#include "assemble.h"
#include <stdlib.h>
#include "../instruction.h"
void assemble(const char* input, const char* outputFileName) {
}

3
src/assembler/assemble.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
void assemble(const char* input, const char* outputFileName);

60
src/assembler/main.c Normal file
View File

@@ -0,0 +1,60 @@
#include <stdlib.h>
#include <stdio.h>
#include "assemble.h"
char* getFileContents(const char* filename) {
// https://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c
FILE* fp;
long lSize;
char* file;
fp = fopen(filename, "rb");
if (!fp) {
perror(filename);
exit(1);
}
fseek(fp, 0L, SEEK_END);
lSize = ftell(fp);
rewind(fp);
file = calloc(1, lSize + 1);
if (!file) {
fclose(fp);
fprintf(stderr, "memory allocation fail when reading file %s\n", filename);
exit(1);
}
if (1!=fread(file, lSize, 1, fp)) {
fclose(fp);
free(file);
fputs("couldn't read entire file", stderr);
exit(1);
}
// we done
fclose(fp);
return file;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s\n <input file> [output file]\n", argv[0]);
return 1;
}
char* outputName = "out.bin";
if (argc >= 3) {
outputName = argv[2];
}
char* asmContents = getFileContents(argv[1]);
if (asmContents == NULL) {
printf("couldn't read file %s\n", argv[1]);
return 1;
}
// TODO: implement
assemble(asmContents, outputName);
}

View File

@@ -32,7 +32,7 @@ typedef uint16_t SerializedInst;
typedef struct { typedef struct {
InstructionOpcode opcode; InstructionOpcode opcode;
uint8_t operands[3]; Register operands[3];
uint16_t immediate; // unused for almost all instructions uint16_t immediate; // unused for almost all instructions
bool hasImm16; bool hasImm16;
} Instruction; } Instruction;