Permalink
Browse files

fixed bug in define, added test

  • Loading branch information...
1 parent 87dff50 commit cbb492234867d2fe6bfa7941c9340fd679e6b5a3 @noname22 noname22 committed Apr 12, 2012
View
@@ -220,10 +220,13 @@ uint16_t Assemble(Dasm* me, const char* ifilename, int addr, int depth)
int toknum = 0;
for(;;){
+ // note: GetToken "consumes" the line, that's why it's line = GetToken
line = GetToken(me, line, token);
if(StrEmpty(token)) break;
+ //LogD("token: '%s'", token);
+
char tokenUpper[MAX_STR_SIZE];
for(int i = 0; i < strlen(token) + 1; i++) tokenUpper[i] = toupper(token[i]);
@@ -349,7 +352,7 @@ uint16_t Assemble(Dasm* me, const char* ifilename, int addr, int depth)
continue;
}
- if(insnum < DINS_EXT_BASE){ LAssertError(toknum == 3, "baisc instructions expect 2 operands (not %d)", toknum - 1);}
+ if(insnum < DINS_EXT_BASE){ LAssertError(toknum == 3, "basic instructions expect 2 operands (not %d)", toknum - 1);}
else { LAssertError(toknum == 2, "extended instructions expect 1 operand (not %d)", toknum - 1); }
// Line parsed, write parsed stuff
View
@@ -18,11 +18,10 @@ bool GetLine(Dasm* me, FILE* f, char* buffer)
}
}
-// XXX: Handle spaces inside []
-char* GetToken(Dasm* me, char* buffer, char* token)
+char* GetToken(Dasm* me, char* line, char* token)
{
// skip spaces etc.
- while((*buffer <= 32 || *buffer == ',') && *buffer != 0) buffer++;
+ while((*line <= 32 || *line == ',') && *line != 0) line++;
// read into token until the next space or end of string
int at = 0;
@@ -33,15 +32,15 @@ char* GetToken(Dasm* me, char* buffer, char* token)
char end[] = "\"']";
- while((expecting || (*buffer > 32 && *buffer != ',')) && *buffer != 0){
+ while((expecting || (*line > 32 && *line != ',')) && *line != 0){
if(expecting){
- if(*buffer == expecting) expecting = 0;
+ if(*line == expecting) expecting = 0;
}else{
for(int i = 0; i < sizeof(start); i++){
- if(*buffer == start[i]) expecting = end[i];
+ if(*line == start[i]) expecting = end[i];
}
}
- token[at++] = *(buffer++);
+ token[at++] = *(line++);
}
LAssertError(!expecting, "unterminated quotation, expected: '%c'", expecting);
@@ -50,11 +49,12 @@ char* GetToken(Dasm* me, char* buffer, char* token)
// Handle defines
Define* it;
Vector_ForEach(*me->defines, it){
- if(!strcmp(buffer, it->searchReplace[0]));
- strcpy(buffer, it->searchReplace[1]);
+ if(!strcmp(token, it->searchReplace[0])){
+ strcpy(token, it->searchReplace[1]);
+ }
}
- return buffer;
+ return line;
}
@@ -0,0 +1,2 @@
+.define something a
+set something, 1
@@ -0,0 +1 @@
+�
@@ -2,7 +2,7 @@
set -e
-for t in "dw"; do
+for t in "dw" "define"; do
../../dasm -v1 $t.dasm /tmp/out.dbin
diff ${t}_correct.dbin /tmp/out.dbin
done
View
@@ -5,9 +5,10 @@ static const char* valNames[] = VALNAMES;
int logLevel;
-void Disasm(uint16_t* ram)
+void Disasm(uint16_t* ram, uint16_t start)
{
- int pc = 0;
+ LogV("starting disassembly at: 0x%04x", start);
+ int pc = start;
int end = 0xffff;
while(ram[--end] == 0);
@@ -62,16 +63,45 @@ void Disasm(uint16_t* ram)
int main(int argc, char** argv)
{
- logLevel = 1;
+ const char* usage = "usage: %s (-vX | -d) [dcpu-16 binary]";
+ LAssert(argc >= 2, usage, argv[0]);
+
+ logLevel = 2;
+
+ const char* file = NULL;
+ unsigned start = 0;
+ int numFiles = 0;
+
+ for(int i = 1; i < argc; i++){
+ char* v = argv[i];
+ if(v[0] == '-'){
+ if(!strcmp(v, "-h")){
+ LogI(usage, argv[0]);
+ LogI(" ");
+ LogI("Available flags:");
+ LogI(" -vX set log level, where X is [0-5] - default: 2");
+ LogI(" -sX start disassembly at address X - default 0");
+ return 0;
+ }
+ else if(sscanf(v, "-v%d", &logLevel) == 1){}
+ else if(sscanf(v, "-s0x%x", &start) || sscanf(v, "-s%d", &start) == 1){}
+ else{
+ LogF("No such flag: %s", v);
+ return 1;
+ }
+ }else{
+ numFiles++;
+ file = v;
+ }
+ }
- LAssert(argc == 2, "usage: %s [dcpu-16 binary]", argv[0]);
+ LAssert(numFiles == 1, "Please specify one file to disassemble");
// Allocate 64 kword RAM file
- uint16_t* ram = malloc(sizeof(uint16_t) * 0x10000);
+ uint16_t* ram = calloc(1, sizeof(uint16_t) * 0x10000);
- LoadRam(ram, argv[1]);
-
- Disasm(ram);
+ LoadRam(ram, file);
+ Disasm(ram, start);
free(ram);
}

0 comments on commit cbb4922

Please sign in to comment.