diff --git a/instructions.c b/instructions.c index e86eea3..b8d94c1 100644 --- a/instructions.c +++ b/instructions.c @@ -1,4 +1,4 @@ -// PID: 730677144 +// PID: 730677144 // I pledge the COMP 211 honor code. #include @@ -16,7 +16,7 @@ // Return: instruction_type: R_TYPE or I_TYPE (see structures) // instruction_type get_type_of_instruction(uint32_t instruct) { - if (bit_select(instruct, 26, 32) == 0) { + if (bit_select(instruct, OPCODE_START_BIT, OPCODE_END_BIT) == 0) { return R_TYPE; } else { return I_TYPE; @@ -35,8 +35,15 @@ instruction_type get_type_of_instruction(uint32_t instruct) { // you will have to set: rs, rt, rd, shamt, func // r_instruction* create_r_instruction(uint32_t instruct) { - // TODO - return NULL; + r_instruction* r = malloc(sizeof(r_instruction)); + + r->rs = bit_select(instruct, RS_START_BIT, RS_END_BIT); + r->rt = bit_select(instruct, RT_START_BIT, RT_END_BIT); + r->rd = bit_select(instruct, RD_START_BIT, RD_END_BIT); + r->shamt = bit_select(instruct, SHAMT_START_BIT, SHAMT_END_BIT); + r->func = bit_select(instruct, FUNC_START_BIT, FUNC_END_BIT); + + return r; } // end create_r_instruction() function // ------------------------------------ @@ -50,6 +57,12 @@ r_instruction* create_r_instruction(uint32_t instruct) { // you will have to set: opcode, rs, rt, immediate // i_instruction* create_i_instruction(uint32_t instruct) { - // TODO - return NULL; + i_instruction* i = malloc(sizeof(i_instruction)); + + i->opcode = bit_select(instruct, OPCODE_START_BIT, OPCODE_END_BIT); + i->rs = bit_select(instruct, RS_START_BIT, RS_END_BIT); + i->rt = bit_select(instruct, RT_START_BIT, RT_END_BIT); + i->immediate = bit_select(instruct, IMMEDIATE_START_BIT, IMMEDIATE_END_BIT); + + return i; } // end create_i_instruction() function diff --git a/lab04 b/lab04 index 8f81b49..4f12e03 100755 Binary files a/lab04 and b/lab04 differ diff --git a/lab04.c b/lab04.c index a6f0bea..b46690d 100644 --- a/lab04.c +++ b/lab04.c @@ -1,4 +1,4 @@ -// PID: 730677144 +// PID: 730677144 // I pledge the COMP 211 honor code. #include @@ -32,12 +32,10 @@ int main() { return EXIT_SUCCESS; if (get_type_of_instruction(instruct) == R_TYPE) { - printf("R_TYPE instruction\n"); r_instruction* r_instruct = create_r_instruction(instruct); execute_r_instruction(r_instruct); free(r_instruct); } else { // I_TYPE - printf("I_TYPE instruction\n"); i_instruction* i_instruct = create_i_instruction(instruct); execute_i_instruction(i_instruct); free(i_instruct); @@ -72,7 +70,36 @@ int main() { // Return: None // void execute_r_instruction(r_instruction* instruct) { - // TODO + switch (instruct->func) { + case SLL_FUNC: + registers[instruct->rd] = registers[instruct->rt] + << instruct->shamt; + break; + case SRA_FUNC: + registers[instruct->rd] = + registers[instruct->rt] >> instruct->shamt; + break; + case ADD_FUNC: + registers[instruct->rd] = + registers[instruct->rs] + registers[instruct->rt]; + break; + case SUB_FUNC: + registers[instruct->rd] = + registers[instruct->rs] - registers[instruct->rt]; + break; + case AND_FUNC: + registers[instruct->rd] = + registers[instruct->rs] & registers[instruct->rt]; + break; + case OR_FUNC: + registers[instruct->rd] = + registers[instruct->rs] | registers[instruct->rt]; + break; + default: + fprintf(stderr, "Invalid function code!\n"); + exit(EXIT_FAILURE); + } + } // end execute_r_instruction() function // ------------------------------------ @@ -89,5 +116,21 @@ void execute_r_instruction(r_instruction* instruct) { // Return: None // void execute_i_instruction(i_instruction* instruct) { - // TODO + switch (instruct->opcode) { + case ADDI_OPCODE: + registers[instruct->rt] = + registers[instruct->rs] + instruct->immediate; + break; + case ANDI_OPCODE: + registers[instruct->rt] = + registers[instruct->rs] & instruct->immediate; + break; + case ORI_OPCODE: + registers[instruct->rt] = + registers[instruct->rs] | instruct->immediate; + break; + default: + fprintf(stderr, "Invalid opcode!\n"); + exit(EXIT_FAILURE); + } } // end execute_i_instruction() function