Ejemplo n.º 1
0
    void assembleFile() {
        std::ifstream in(inPath_.c_str());
        if (!in) {
            error("couldn't open: " + errorString());
        }

        while (getline(in, line_)) {
            ++lineNumber_;
            p_ = line_.c_str();

            // Handle labels.
            if (isalnum(*p_)) {
                handleLabel();
            } else {
                // Trim leading whitespace.
                trimLeft();
                if (*p_ == 0 || *p_ == '#') {
                    // Skip blank or comment lines.
                } else if (*p_ == '.') {
                    handleDirective();
                } else {
                    handleInstruction();
                }
            }
        }

        fixForwardBranches();
        writeBytes(&code_[0], &code_[code_.size()]);
    }
Ejemplo n.º 2
0
int instruction_cycle(CPU *cpu) {
	int halted = 1;
	
	(*cpu).ir = (*cpu).mem[(*cpu).pc];
	handleInstruction(cpu);
	switch ((*cpu).opcode){
		case 0:
		  halted = 0;
		  printf("Halt!");
		  break;
		case 1:
		  ld(cpu);
		  break;
		case 2:
		  st(cpu);	
		  break;
		case 3:
		  add(cpu);
		  break;
		case 4:
		  neg(cpu);
		  break;
		case 5:
		  ldm(cpu);
		  break;
		case 6:
		  addm(cpu);
		  break;
		case 7:
		  br(cpu);
		  break;
		case 8:
		  brp(cpu);
		  break;
		case 9:
		  io(cpu);
		  break;
		default:
		  printf("Error:Invalid opcode at address %d,Invalid number: %d",(*cpu).pc,(*cpu).mem[(*cpu).pc]);
		  break;
	}
	(*cpu).pc++;
	return halted;
	// For Lab 7, we just print a message and halt after the 10th call
	//
	//char suffix[][4] = {"", "st","nd","rd","th"};
	//printf("Calling instruction_cycle for %d%s time\n", call_nbr, suffix[min(call_nbr,4)]);

}