bool Assembler::SetLabels(Assembly& assembly, LabelVector& labels) { dword *bytecode = assembly.bytecode; dword curpos = assembly.curpos; size_t size = labels.size(); for(dword *cp = bytecode; cp < bytecode + curpos; cp += 2) { if(IsJumpOp(cp[0])) { if(cp[1] < size) { dword pos = labels.at(cp[1]).pos; if(pos != -1) cp[1] = pos; else { // this is a common error printf("Label not found: %s\n", labels.at(cp[1]).label.c_str()); labels.clear(); return false; } } else { // if we get here, there's a bug in the assembler ;( printf("Label index not found: %d\n", cp[1]); labels.clear(); return false; } } } labels.clear(); return true; }
bool Assembler::ParseLine(string& line, int linecount, Assembly& assembly, LabelVector& labels) { string op; int i = NextWord(line, op); // skip commented and empty lines if(i == -1 || op == "//" || op == ";") return true; // this is a a very slow way of doing things, but then this code will only // be used during development and for debugging OpDesc *opcode; int codecount = sizeof(opcodes)/sizeof(OpDesc); int j = 0; for(; j < codecount; j ++) { if(stricmp(opcodes[j].name, op.c_str()) == 0) { opcode = &opcodes[j]; break; } } // is this a label? if(j == codecount) { size_t k = op.length() - 1; if(op.find_first_of(":") == k) { string name = op.substr(0, k); labels.at(AddLabel(name, labels)).pos = assembly.curpos; return true; } else return false; } else { assembly.WriteDword(opcode->code); string param; i = NextWord(line, param, i); if(IsJumpOp(opcode->code)) { // write the label index, we'll later replace this with the code position assembly.WriteDword(AddLabel(param, labels)); } else { if(i == -1) { assembly.WriteDword(0); if(opcode->paramcount > 0) return false; // if we need more params, bail } else assembly.WriteDword(StringToOperand(param, opcode)); } } return true; }