Beispiel #1
0
void assemble(const char *fn) {
	u16 pc, n;
	fin = fopen(fn, "r");
	filename = fn;
	linenumber = 0;
	if (!fin) die("cannot read file");

	for (;;) {
		next();
		switch (token) {
		case tEOF:
			goto done;
		case tCOLON:
			expect(tSTRING);
			set_label(tstring, PC);
			continue;
		case tWORD:
			assemble_imm_or_label();
			continue;
		case tSET: case tADD: case tSUB: case tMUL:
		case tDIV: case tMOD: case tSHL: case tSHR:
		case tAND: case tBOR: case tXOR: case tIFE:
		case tIFN: case tIFG: case tIFB:
			assemble_binop(token - tXXX); 
			continue;
		case tJSR:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | 0x0010;
			continue;
		default:
			die("unexpected: %s", tnames[token]);
		}
	}
done:
	fclose(fin);
}
Beispiel #2
0
void assemble(const char *fn) {
	u16 pc, n;
	fin = fopen(fn, "r");
	filename = fn;
	linenumber = 0;
	if (!fin) die("cannot read file");

	for (;;) {
		next();
again:
		switch (token) {
		case tEOF:
			goto done;
		case tSTRING:
			expect(tCOLON);
			set_label(tstring, PC);
			continue;
		case tCOLON:
			expect(tSTRING);
			set_label(tstring, PC);
			continue;
		case tWORD: case tDAT: case tDATA: case tDW:
			assemble_imm_or_label();
			goto again;
		case tJMP: // alias for SET PC, ...
			assemble_jump();
			continue;
		case tMOV: // alias for SET
			token = tSET;
		case tSET: case tADD: case tSUB: case tMUL:
		case tDIV: case tMOD: case tSHL: case tSHR:
		case tAND: case tBOR: case tXOR: case tIFE:
		case tIFN: case tIFG: case tIFB:
		case tPUSH: case tPOP: case tNOP:
			assemble_binop();
			continue;
		case tJSR:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x01 << 5);
			continue;
		case tINT:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x08 << 5);
			continue;
		case tIAG:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x09 << 5);
			continue;
		case tIAS:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x0a << 5);
			continue;
		case tRFI:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x0b << 5);
			continue;
		case tIAQ:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x0c << 5);
			continue;
		case tHWN:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x10 << 5);
			continue;
		case tHWQ:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x11 << 5);
			continue;
		case tHWI:
			pc = PC++;
			n = assemble_operand();
			image[pc] = (n << 10) | (0x12 << 5);
			continue;
		default:
			die("unexpected: %s", tnames[token]);
		}
	}
done:
	fclose(fin);
}