Created
April 6, 2012 23:44
-
-
Save lessandro/2324048 to your computer and use it in GitHub Desktop.
Yet another DCPU16 assembler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
unsigned short out[100], *curr, *next; | |
const char *regs[] = { | |
"a", "b", "c", "x", | |
"y", "z", "i", "j" | |
}; | |
int val(char *va) | |
{ | |
for (int i=0; i<8; i++) | |
if (!strcmp(regs[i], va)) | |
return i; | |
return 0; | |
} | |
const char *instrs[] = { | |
"", "SET", "ADD", "SUB", | |
"MUL", "DIV", "MOD", "SHL", | |
"SHR", "AND", "BOR", "XOR", | |
"IFE", "IFN", "IFG", "IFB" | |
}; | |
int instr(const char *vi) | |
{ | |
for (int i=0; i<16; i++) | |
if (!strcmp(instrs[i], vi)) | |
return i; | |
return 0; | |
} | |
void assemble(char *line) | |
{ | |
char *vi = strsep(&line, " "); | |
char *va = strsep(&line, ","); | |
char *vb = strsep(&line, "\n"); | |
curr = next++; | |
int n = instr(vi); | |
n |= val(va) << 4; | |
n |= val(vb) << 12; | |
*curr = n; | |
} | |
int main() | |
{ | |
next = out; | |
char buf[100]; | |
while (gets(buf) != 0) { | |
assemble(buf); | |
} | |
for (unsigned short *p = out; p != next; p++) { | |
printf("%04x ", *p); | |
} | |
puts(""); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment