Added a distinction between constant numbers and what are effectively memory addresses (i.e. strings and labels).
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
|
|
||||||
#define OPCODECOUNT 10
|
#define OPCODECOUNT 11
|
||||||
#define REGISTERCOUNT 8
|
#define REGISTERCOUNT 8
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+5
-4
@@ -26,7 +26,7 @@ typedef enum {
|
|||||||
DB, ORG,
|
DB, ORG,
|
||||||
//Opcodes
|
//Opcodes
|
||||||
COPY, ADD, SUB, JZ, INT, YLD, RET,
|
COPY, ADD, SUB, JZ, INT, YLD, RET,
|
||||||
CMP, IN, OUT,
|
CMP, NOP, IN, OUT,
|
||||||
//Registers
|
//Registers
|
||||||
R1, R2, R3, R4, R5, R6, R7, R8
|
R1, R2, R3, R4, R5, R6, R7, R8
|
||||||
} TokenType;
|
} TokenType;
|
||||||
@@ -34,9 +34,10 @@ typedef enum {
|
|||||||
typedef enum {
|
typedef enum {
|
||||||
None = 0,
|
None = 0,
|
||||||
Reg = 1,
|
Reg = 1,
|
||||||
Immediate16 = 2,
|
Constant = 2,
|
||||||
Opcode = 4,
|
Address = 4,
|
||||||
Directive = 8
|
Opcode = 8,
|
||||||
|
Directive = 16
|
||||||
} TokenClass;
|
} TokenClass;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|||||||
+35
-8
@@ -3,18 +3,45 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
Instruction instructions[OPCODECOUNT] = {
|
Instruction instructions[OPCODECOUNT] = {
|
||||||
{ "copy", COPY, Reg, Reg | Immediate16 },
|
{ "copy", COPY, Reg | Address, Reg | Constant | Address },
|
||||||
{ "add", ADD, Reg, Reg | Immediate16 },
|
{ "add", ADD, Reg, Reg | Constant },
|
||||||
{ "sub", SUB, Reg, Reg | Immediate16 },
|
{ "sub", SUB, Reg, Reg | Constant },
|
||||||
{ "jz", JZ, Immediate16, None },
|
{ "jz", JZ, Address, None },
|
||||||
{ "int", INT, Immediate16, None },
|
{ "int", INT, Constant, None },
|
||||||
{ "yld", YLD, None, None },
|
{ "yld", YLD, None, None },
|
||||||
{ "ret", RET, None, None },
|
{ "ret", RET, None, None },
|
||||||
{ "cmp", CMP, Reg | Immediate16, Reg | Immediate16 },
|
{ "cmp", CMP, Reg, Reg | Constant },
|
||||||
{ "in", IN, None, None},
|
{ "in", IN, Constant | Reg, None},
|
||||||
{ "out", OUT, None, None}
|
{ "out", OUT, None, None},
|
||||||
|
{ "nop", NOP, None, None}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
NOP - 0000 0000
|
||||||
|
JZ - 0000 0001 JZ Address
|
||||||
|
INT - 0000 0010 INT Constant
|
||||||
|
YLD - 0000 0011 YLD
|
||||||
|
RET - 0000 0100 RET
|
||||||
|
CALL - 0000 0101 CALL Address
|
||||||
|
//IN reads from a port number specified by either a register or a constant.
|
||||||
|
IN - 0000 0110 IN Constant
|
||||||
|
- 0000 0111 IN REG
|
||||||
|
COPY - 0000 1XXX COPY REG, REG
|
||||||
|
- 0001 0XXX COPY REG, Constant
|
||||||
|
- 0010 0XXX COPY REG, Address
|
||||||
|
- 0010 1XXX COPY Address, REG
|
||||||
|
- 0011 0XXX COPY Address, Constant
|
||||||
|
- 0011 1XXX COPY Address, Address
|
||||||
|
ADD - 0100 0XXX ADD REG, REG
|
||||||
|
- 0100 1XXX ADD REG, Constant
|
||||||
|
SUB - 0101 0XXX SUB REG, REG
|
||||||
|
- 0101 1XXX SUB REG, Constant
|
||||||
|
CMP - 0110 0XXX CMP REG, REG
|
||||||
|
- 0110 1XXX CMP REG, Constant
|
||||||
|
- 0111 0XXX CMP REG, Address
|
||||||
|
OUT - 0111 1XXX OUT REG
|
||||||
|
*/
|
||||||
|
|
||||||
Register registers[REGISTERCOUNT] = {
|
Register registers[REGISTERCOUNT] = {
|
||||||
{ "r1", R1 },
|
{ "r1", R1 },
|
||||||
{ "r2", R2 },
|
{ "r2", R2 },
|
||||||
|
|||||||
+16
-3
@@ -38,10 +38,13 @@ int ExpectTokenClass(int, ...);
|
|||||||
const Symbol* GetSymbol(char*);
|
const Symbol* GetSymbol(char*);
|
||||||
List* SymbolsTable;
|
List* SymbolsTable;
|
||||||
unsigned int ProgramCounter = 0;
|
unsigned int ProgramCounter = 0;
|
||||||
|
char Memory[HIGHMEMORY];
|
||||||
|
|
||||||
void ParseTokens(List* tokens) {
|
void ParseTokens(List* tokens) {
|
||||||
if (!tokens) return;
|
if (!tokens) return;
|
||||||
|
|
||||||
|
memset(Memory, 0, sizeof(Memory));
|
||||||
|
|
||||||
TokensList = tokens;
|
TokensList = tokens;
|
||||||
|
|
||||||
SymbolsTable = CreateList();
|
SymbolsTable = CreateList();
|
||||||
@@ -56,7 +59,7 @@ void ParseTokens(List* tokens) {
|
|||||||
case Opcode:
|
case Opcode:
|
||||||
HandleOperation();
|
HandleOperation();
|
||||||
break;
|
break;
|
||||||
case Immediate16:
|
case Address:
|
||||||
if (t->type == LABEL) {
|
if (t->type == LABEL) {
|
||||||
printf("%s: \n", t->lexeme);
|
printf("%s: \n", t->lexeme);
|
||||||
AddSymbol(t, ProgramCounter);
|
AddSymbol(t, ProgramCounter);
|
||||||
@@ -71,6 +74,11 @@ void ParseTokens(List* tokens) {
|
|||||||
|
|
||||||
PrintSymbols();
|
PrintSymbols();
|
||||||
DestroyList(SymbolsTable);
|
DestroyList(SymbolsTable);
|
||||||
|
// for(int i = 0; i < 256; i++) {
|
||||||
|
// if (i != 0 && i % 80 == 0) printf("\n");
|
||||||
|
// printf("%02X ", Memory[i] & 0xFF);
|
||||||
|
// }
|
||||||
|
// printf("\n");
|
||||||
// printf("Program Counter: %d\n", ProgramCounter);
|
// printf("Program Counter: %d\n", ProgramCounter);
|
||||||
//printf("Heap Top: %#06X\n", HeapTop);
|
//printf("Heap Top: %#06X\n", HeapTop);
|
||||||
}
|
}
|
||||||
@@ -93,6 +101,10 @@ void HandleAssemblerDirective(void) {
|
|||||||
case STRING:
|
case STRING:
|
||||||
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, PeekToken()->lexeme, ProgramCounter);
|
printf("[INFO] %s = '%s' (%#04X)\n", identifier->lexeme, PeekToken()->lexeme, ProgramCounter);
|
||||||
|
|
||||||
|
for(int i = 0; i < strlen(PeekToken()->lexeme); i++) {
|
||||||
|
Memory[ProgramCounter + i] = PeekToken()->lexeme[i];
|
||||||
|
}
|
||||||
|
|
||||||
ProgramCounter += strlen(PeekToken()->lexeme);
|
ProgramCounter += strlen(PeekToken()->lexeme);
|
||||||
|
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
@@ -106,6 +118,7 @@ void HandleAssemblerDirective(void) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//Add the NULL byte.
|
//Add the NULL byte.
|
||||||
|
Memory[ProgramCounter] = '\0';
|
||||||
ProgramCounter++;
|
ProgramCounter++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -142,7 +155,7 @@ void HandleOperation(void) {
|
|||||||
//Encode the register number here.
|
//Encode the register number here.
|
||||||
printf("%s ", PeekToken()->lexeme);
|
printf("%s ", PeekToken()->lexeme);
|
||||||
}
|
}
|
||||||
else if (inst->parameter_one & Immediate16) {
|
else if (inst->parameter_one & Address || inst->parameter_one & Constant) {
|
||||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||||
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||||
|
|
||||||
@@ -176,7 +189,7 @@ void HandleOperation(void) {
|
|||||||
|
|
||||||
if (inst->parameter_two && PeekToken()->token_class > 0) {
|
if (inst->parameter_two && PeekToken()->token_class > 0) {
|
||||||
//printf("Matched 2 '%s'\n", PeekToken()->lexeme);
|
//printf("Matched 2 '%s'\n", PeekToken()->lexeme);
|
||||||
if (inst->parameter_two & Immediate16) {
|
if (inst->parameter_two & Address || inst->parameter_two & Constant) {
|
||||||
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
if (PeekToken()->type == IDENTIFIER || PeekToken()->type == LABEL) {
|
||||||
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
const Symbol* symbol = GetSymbol(PeekToken()->lexeme);
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -28,8 +28,9 @@ TokenClass GetTokenClass(TokenType type) {
|
|||||||
case STRING:
|
case STRING:
|
||||||
case IDENTIFIER:
|
case IDENTIFIER:
|
||||||
case LABEL:
|
case LABEL:
|
||||||
|
return Address;
|
||||||
case NUMBER:
|
case NUMBER:
|
||||||
return Immediate16;
|
return Constant;
|
||||||
default:
|
default:
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user