/* * Description: * Grabs an instruction from the instruction trace (if possible) * Inputs: * trace: instruction trace with all the instructions executed * Returns: * None */ void fetch(instruction_trace_t* trace) { /* ECE552: YOUR CODE GOES HERE */ /* ECE552 Assignment 3 - BEGIN CODE */ //I have valid index instruction_t *currInstr = get_instr(trace, fetch_index); instr_push(currInstr); fetch_index++; //REMOVE THIS BEFORE SUBMISSION /* ECE552 Assignment 3 - END CODE */ }
void machine_t::exec(Op operation) { switch(operation) { default: error("Unknown instruction"); break; case NOP: instr_nop(); break; // Strictly speaking, SUB can be implemented // by ADDing the minuend with the two's complement // of the subtrahend -- but that's not necessarily // portable down to native code case ADD: instr_add(); break; case SUB: instr_sub(); break; // non-primitive // Strictly speaking, all but NOT and AND are // non-primitive (or some other combination of // two operations) case AND: instr_and(); break; case OR: instr_or(); break; case XOR: instr_xor(); break; case NOT: instr_not(); break; case COMPL: instr_compl(); break; // Should be replaced with x86 INT-like operations case IN: instr_in(); break; case OUT: instr_out(); break; case LOAD: instr_load(); break; case STOR: instr_stor(); break; case PUSH: instr_push(); break; case DROP: instr_drop(); break; case PUSHIP: instr_puship(); break; case POPIP: instr_popip(); break; case DROPIP: instr_dropip(); break; case JZ: instr_jz(); break; case JMP: instr_jmp(); break; // non-primitive case JNZ: instr_jnz(); break; // non-primitive case DUP: instr_dup(); break; // non-primitive case SWAP: instr_swap(); break; // non-primitive case ROL3: instr_rol3(); break; // non-primitive case OUTNUM: instr_outnum(); break; // non-primitive } }
int assemble(FILE *in, FILE *out) { /* Write magic */ int magic = FILE_MAGIC; fwrite(&magic, sizeof(int), 1, out); char line[1024]; while(fgets(line,1024,in) != NULL) { lineno++; if(line[0] == '#' || line[0] == '\n') { continue; } else if(!strncmp(line, "push ", 5)) { instr_push(out,line+5); } else if(!strncmp(line, "push_i ", 7)) { instr_push_i(out,line+7); } else if(!strncmp(line, "push_o ", 7)) { instr_push_o(out,line+7); } else if(!strncmp(line, "push_k ", 7)) { instr_push_k(out,line+7); } else if(!strncmp(line, "node ", 5)) { instr_node(out,line+5); } else if(!strncmp(line, "nodem ", 6)) { instr_nodem(out,line+6); } else if(!strncmp(line, "redex", 5)) { instr_redex(out,line+5); } else if(!strncmp(line, "reactum", 7)) { instr_reactum(out,line+7); } else if(!strncmp(line, "reaction", 8)) { instr_reaction(out,line+8); } else { fprintf(stderr, "Error: invalid instruction at line %d:\n%s", lineno, line); return FALSE; } } if(stackptr > 0) { fprintf(stderr, "Warning: spare tokens on symbol stack\n"); } return TRUE; }