Untitled
unknown
plain_text
8 months ago
3.5 kB
14
Indexable
void execute_instructions(System *sys)
{
while (1) // set to true for now
{
int current_eip = sys->registers[EIP]; // get current eip
// valid eip
if (current_eip < 0 || current_eip % 4 != 0)
{
break;
}
int instruction_loc = current_eip / 4; // location of instruction
if (instruction_loc >= sys->memory.num_instructions) // break loop in this case
{
break;
}
char *instruction_string = sys->memory.instruction[instruction_loc]; // instruction string
// if its end, break the loop
if (strcmp(instruction_string, "END") == 0)
{
break;
}
// code to skip over the labels
if (instruction_string[0] == '.')
{
sys->registers[EIP] += 4;
continue; // go to next iteration
}
char inst_copy[256];
strcpy(inst_copy, instruction_string); // copy over
// string operations to get operator, src, and dst
char *operation = strtok(inst_copy, " ,\t");
char *src = strtok(NULL, " ,\t");
char *dst = strtok(NULL, " ,\t");
// set the reult to success for now
ExecResult result = SUCCESS;
// check operartion
if (strcmp(operation, "MOVL") == 0) // movl
{
if (!src || !dst)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_movl(sys, src, dst);
}
}
else if (strcmp(operation, "ADDL") == 0) // addl
{
if (!src || !dst)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_addl(sys, src, dst);
}
}
else if (strcmp(operation, "PUSHL") == 0) // pushl
{
if (!src)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_push(sys, src);
}
}
else if (strcmp(operation, "POPL") == 0) // popl
{
if (!src)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_pop(sys, src);
}
}
else if (strcmp(operation, "CMPL") == 0) // comparison
{
if (!src || !dst)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_cmpl(sys, src, dst);
}
}
else if (strcmp(operation, "CALL") == 0) // call
{
if (!src)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_call(sys, src);
}
}
else if (strcmp(operation, "RET") == 0) // returning
{
result = execute_ret(sys);
}
else if (strcmp(operation, "JMP") == 0 || strcmp(operation, "JE") == 0 ||
strcmp(operation, "JNE") == 0 || strcmp(operation, "JL") == 0 ||
strcmp(operation, "JG") == 0) // handle the jump operators
{
if (!src)
{
result = INSTRUCTION_ERROR;
}
else
{
result = execute_jmp(sys, operation, src);
}
}
else
{
// instruction doesn't work
sys->registers[EIP] += 4;
continue;
}
// check for execution issues
if (result != SUCCESS)
{
break;
}
else
{
// increment program counter
if (strcmp(operation, "MOVL") == 0 || strcmp(operation, "ADDL") == 0 ||
strcmp(operation, "PUSHL") == 0 || strcmp(operation, "POPL") == 0 ||
strcmp(operation, "CMPL") == 0)
{
sys->registers[EIP] += 4;
}
}
}
}Editor is loading...
Leave a Comment