void cpu_je(CPU_t* This) { elem_t a = Stack_pop(This); elem_t b = Stack_pop(This); if (a > b) { This->cmp_flag = 1; } else if (a == b) { This->cmp_flag = 0; } else This->cmp_flag = -1; if (This->cmp_flag == 0) { cpu_jmp(This); } This->pc++; }
/** This function exec byte code of instructions @param code array of byte code @return number of error(0 if no error) */ int cpu_run(char* addr_code) { assert(addr_code); cpu_t* cpu = NULL; cpu_ctor(&cpu, CPU_STACK_MAX, addr_code); char* current_byte = addr_code; start_logging("logs.txt"); while(FOREVER) { int cmd = *current_byte; current_byte++; //printf("%d \n",cmd); switch(cmd) { case PUSH: if(cpu_push(cpu, ¤t_byte)) printf("CPU CRASHED! PUSH HAVEN'T FULFILLED"); break; case POP: if(cpu_pop(cpu, ¤t_byte)) printf("CPU CRASHED! POP HAVEN'T FULFILLED"); break; case OK: if(cpu_ok(cpu)) printf("CPU CRASHED! CPU ISN'T OK"); break; case DUMP: if(cpu_dump(cpu)) printf("CPU CRASHED! DUMP HAVEN'T FULFILLED"); break; case ADD: if(cpu_add(cpu)) printf("CPU CRASHED! ADD HAVEN'T FULFILLED"); break; case SUB: if(cpu_sub(cpu)) printf("CPU CRASHED! SUB HAVEN'T FULFILLED"); break; case MUL: if(cpu_mul(cpu)) printf("CPU CRASHED! MUL HAVEN'T FULFILLED"); break; case DIR: if(cpu_dir(cpu)) printf("CPU CRASHED! DIR HAVEN'T FULFILLED"); break; case SIN: if(cpu_sin(cpu)) printf("CPU CRASHED! SIN HAVEN'T FULFILLED"); break; case COS: if(cpu_cos(cpu)) printf("CPU CRASHED! COS HAVEN'T FULFILLED"); break; case SQRT: if(cpu_sqrt(cpu)) printf("CPU CRASHED! SQRT HAVEN'T FULFILLED"); break; case JA: if(cpu_ja(cpu, ¤t_byte)) printf("CPU CRASHED! JA HAVEN'T FULFILLED"); break; case JAE: if(cpu_jae(cpu, ¤t_byte)) printf("CPU CRASHED! JAE HAVEN'T FULFILLED"); break; case JB: if(cpu_jb(cpu, ¤t_byte)) printf("CPU CRASHED! JB HAVEN'T FULFILLED"); break; case JBE: if(cpu_jbe(cpu, ¤t_byte)) printf("CPU CRASHED! JBE HAVEN'T FULFILLED"); break; case JE: if(cpu_je(cpu, ¤t_byte)) printf("CPU CRASHED! JE HAVEN'T FULFILLED"); break; case JNE: if(cpu_jne(cpu, ¤t_byte)) printf("CPU CRASHED! JNE HAVEN'T FULFILLED"); break; case JMP: if(cpu_jmp(cpu, ¤t_byte)) printf("CPU CRASHED! JMP HAVEN'T FULFILLED"); break; case OUT: if(cpu_out(cpu)) printf("CPU CRASHED! OUT HAVEN'T FULFILLED"); break; case MOV: if(cpu_mov(cpu, ¤t_byte)) printf("CPU CRASHED! MOV HAVEN'T FULFILLED"); break; case INC: if(cpu_inc(cpu, ¤t_byte)) printf("CPU CRASHED! INC HAVEN'T FULFILLED"); break; case DEC: if(cpu_dec(cpu, ¤t_byte)) printf("CPU CRASHED! DEC HAVEN'T FULFILLED"); break; case CALL: if(cpu_call(cpu, ¤t_byte)) printf("CPU CRASHED! CALL HAVEN'T FULFILLED"); break; case RET: if(cpu_ret(cpu, ¤t_byte)) printf("CPU CRASHED! RET HAVEN'T FULFILLED"); break; case END: return 0; break; default: { fprintf(stderr, "ERROR! Unknown command at address %o. \n", (int)current_byte); return 1; } } } return 0; }
void cpu_jne(CPU_t* This) { cpu_cmp(This); if (This->cmp_flag != 0) { cpu_jmp(This); } }