220 lines
6.5 KiB
C
220 lines
6.5 KiB
C
#include <bits/types/FILE.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sysexits.h>
|
|
#include "../includes/list.h"
|
|
#include "../includes/parser.h"
|
|
#include "../includes/futil.h"
|
|
#include "../includes/scanner.h"
|
|
|
|
unsigned char Memory[2000];
|
|
|
|
void Setup(IRState*);
|
|
|
|
int main(int argc, char* args[]) {
|
|
if (argc == 1) {
|
|
printf("Usage: assm <file1.asm>\n");
|
|
return EX_USAGE;
|
|
}
|
|
|
|
char* source_code;
|
|
size_t bytes_read;
|
|
|
|
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
|
|
|
List* list = GenerateTokenList(source_code);
|
|
// char mnemonic[12];
|
|
|
|
// for(int i = 0; i < list->size; i++) {
|
|
// Token* t = (Token*) list->content[i];
|
|
|
|
// if (t->EndOfFile) {
|
|
// printf("EOF\n");
|
|
// break;
|
|
// }
|
|
|
|
// if (t->Class == PunctuationClass){
|
|
// if(t->Value.Punctuation == NewLine) {
|
|
// printf("\n");
|
|
// continue;
|
|
// }
|
|
|
|
// printf("<%d>", t->LineNumber);
|
|
|
|
// printf("%c ", t->Value.Punctuation);
|
|
// continue;
|
|
// }
|
|
|
|
// printf("<%d>", t->LineNumber);
|
|
|
|
// if (t->Class == LabelClass) {
|
|
// printf("[L]%s* ", t->Lemexe);
|
|
// continue;
|
|
// }
|
|
|
|
// if (t->Class == IdentifierClass) {
|
|
// printf("[I]%s ", t->Lemexe);
|
|
// continue;
|
|
// }
|
|
|
|
// if (t->Class == RegisterClass) {
|
|
// printf("[R]%d", t->Value.Register);
|
|
// continue;
|
|
// }
|
|
|
|
// if (t->Class == NumberClass) {
|
|
// printf("[N]%d ", t->Value.Number);
|
|
// continue;
|
|
// }
|
|
|
|
// if (t->Class == MnemonicClass) {
|
|
// GetMnemonicText(t->Value.Mnemonic, mnemonic);
|
|
// printf("[M]%s ", mnemonic);
|
|
// continue;
|
|
// }
|
|
|
|
// if (t->Class == DirectiveClass) {
|
|
// printf("[D]%d ", t->Value.Directive);
|
|
|
|
// }
|
|
|
|
// if (t->Class == CharacterClass) {
|
|
// printf("%s ", t->Lemexe);
|
|
// }
|
|
// }
|
|
|
|
IRState* image = ParseTokens(list);
|
|
char instruction[12];
|
|
char reg[3];
|
|
|
|
for(int i = 0; i < image->Instructions->size; i++) {
|
|
Instruction* ins = image->Instructions->content[i];
|
|
|
|
GetMnemonicText(ins->Mnemonic, instruction);
|
|
|
|
printf("%s ", instruction);
|
|
|
|
if (!ins->ParameterOne) {
|
|
printf("\n");
|
|
continue;
|
|
}
|
|
|
|
switch(ins->ParameterOne->ParameterType) {
|
|
case RegisterParameter:
|
|
GetRegisterText(ins->ParameterOne->Value.Register, reg);
|
|
|
|
if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%s]", reg);
|
|
else printf("%s", reg);
|
|
break;
|
|
case ConstantParameter:
|
|
if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%d]", ins->ParameterOne->Value.Number);
|
|
else printf("%d", ins->ParameterOne->Value.Number);
|
|
break;
|
|
case AddressParameter:
|
|
if (ins->ParameterOne->InterpretedAs == AddressParameter) printf("[%s]", ins->ParameterOne->Value.Symbol);
|
|
else {
|
|
Symbol* s = TryGetSymbol(ins->ParameterOne->Value.Symbol, image->SymbolsTable);
|
|
|
|
switch(s->Type) {
|
|
case NumericSymbol:
|
|
printf("%s {%d}", s->Name, s->Value.Number);
|
|
break;
|
|
default:
|
|
printf("%s", ins->ParameterOne->Value.Symbol);
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
printf("\n");
|
|
}
|
|
|
|
if (!ins->ParameterTwo) {
|
|
printf("\n");
|
|
continue;
|
|
}
|
|
|
|
switch(ins->ParameterTwo->ParameterType) {
|
|
case RegisterParameter:
|
|
GetRegisterText(ins->ParameterTwo->Value.Register, reg);
|
|
|
|
if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%s]\n", reg);
|
|
else printf(", %s\n", reg);
|
|
break;
|
|
case ConstantParameter:
|
|
if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%d]\n", ins->ParameterTwo->Value.Number);
|
|
else printf(", %d\n", ins->ParameterTwo->Value.Number);
|
|
break;
|
|
case AddressParameter:
|
|
if (ins->ParameterTwo->InterpretedAs == AddressParameter) printf(", [%s]\n", ins->ParameterTwo->Value.Symbol);
|
|
else {
|
|
Symbol* s = TryGetSymbol(ins->ParameterTwo->Value.Symbol, image->SymbolsTable);
|
|
|
|
switch(s->Type) {
|
|
case NumericSymbol:
|
|
printf(", %s {%d}\n", s->Name, s->Value.Number);
|
|
break;
|
|
default:
|
|
printf(", %s\n", ins->ParameterTwo->Value.Symbol);
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
Setup(image);
|
|
//free(image);
|
|
//Disassemble(image);
|
|
//for(int i = 0; i < image->Opcodes->size; i++) {
|
|
// printf("%d\n", ((IROpcode*) image->Opcodes->content[i])->Mnemonic);
|
|
//}
|
|
|
|
//FILE* program = fopen("program.bin", "w+b");
|
|
|
|
//fwrite(image, 1, image[2] + image[3], program);
|
|
|
|
//fclose(program);
|
|
|
|
free(source_code);
|
|
}
|
|
|
|
void Setup(IRState* state) {
|
|
int start = 4;
|
|
int end = start;
|
|
|
|
for(int i = 0; i < state->SymbolsTable->Size; i++) {
|
|
Symbol* symbol = state->SymbolsTable->Symbols[i];
|
|
|
|
switch(symbol->Type) {
|
|
case StringSymbol:
|
|
memccpy(&Memory[end], symbol->Value.String->String, 0, strlen(symbol->Value.String->String));
|
|
|
|
end += strlen(symbol->Value.String->String);
|
|
|
|
if (symbol->Value.String->Terminated) {
|
|
Memory[end] = symbol->Value.String->TerminatingByte;
|
|
end++;
|
|
}
|
|
|
|
//end++;
|
|
break;
|
|
case NumericSymbol:
|
|
Memory[end] = symbol->Value.Number >> 8 & 0xFF;
|
|
Memory[end + 1] = symbol->Value.Number & 0xFF;
|
|
end += 2;
|
|
break;
|
|
case InstructionPointerSymbol:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
for(int i = 0; i < end; i++) {
|
|
if (i != 0 && i % 8 == 0) printf("\n");
|
|
printf("%02x ", Memory[i]);
|
|
}
|
|
printf("\n");
|
|
} |