Question: i need help implementing the logic for this program void lmsm _ cap _ value ( int * val ) { / / TODO -

i need help implementing the logic for this program
void lmsm_cap_value(int * val){//TODO - implement capping the value pointed to by this pointer between 999 and -999 if (*val >999){*val =999; // Cap the value at 999} else if (*val <-999){*val =-999; // Cap the value at -999}} int lmsm_has_two_values_on_stack(lmsm *our_little_machine){//TODO - return 0
}
//======================================================
// Instruction Implementation
//======================================================
void lmsm_i_jal(lmsm *our_little_machine){
}
void lmsm_i_ret(lmsm *our_little_machine){
}
void lmsm_i_push(lmsm *our_little_machine){
}
void lmsm_i_pop(lmsm *our_little_machine){
}
void lmsm_i_dup(lmsm *our_little_machine){
}
void lmsm_i_drop(lmsm *our_little_machine){
}
void lmsm_i_swap(lmsm *our_little_machine){
}
void lmsm_i_sadd(lmsm *our_little_machine){
}
void lmsm_i_ssub(lmsm *our_little_machine){
}
void lmsm_i_smax(lmsm *our_little_machine){
}
void lmsm_i_smin(lmsm *our_little_machine){
}
void lmsm_i_smul(lmsm *our_little_machine){
}
void lmsm_i_sdiv(lmsm *our_little_machine){
}
void lmsm_i_out(lmsm *our_little_machine){
// TODO, append the current accumulator to the output_buffer in the LMSM
}
void lmsm_i_inp(lmsm *our_little_machine){
// TODO read a value from the command line and store it as an int in the accumulator
}
void lmsm_i_load(lmsm *our_little_machine, int location){
}
void lmsm_i_add(lmsm *our_little_machine, int location){
our_little_machine->accumulator += our_little_machine->memory[location];
}
void lmsm_i_sub(lmsm *our_little_machine, int location){
our_little_machine->accumulator -= our_little_machine->memory[location];
}
void lmsm_i_load_immediate(lmsm *our_little_machine, int value){
}
void lmsm_i_store(lmsm *our_little_machine, int location){
}
void lmsm_i_halt(lmsm *our_little_machine){
}
void lmsm_i_branch_unconditional(lmsm *our_little_machine, int location){
}
void lmsm_i_branch_if_zero(lmsm *our_little_machine, int location){
}
void lmsm_i_branch_if_positive(lmsm *our_little_machine, int location){
}
void lmsm_step(lmsm *our_little_machine){
// TODO : if the machine is not halted, we need to read the instruction in the memory slot
// pointed to by the program counter, bump the program counter then execute
// the instruction
if (our_little_machine->status != STATUS_HALTED){
int next_instruction = our_little_machine->memory[our_little_machine->program_counter];
our_little_machine->program_counter++;
our_little_machine->current_instruction = next_instruction;
int instruction = our_little_machine->current_instruction;
lmsm_exec_instruction(our_little_machine, instruction);
}
}
//======================================================
// LMSM Implementation
//======================================================
void lmsm_exec_instruction(lmsm *our_little_machine, int instruction){
// TODO - dispatch the rest of the instruction set and implement
// the instructions above
if (instruction ==0){
lmsm_i_halt(our_little_machine);
} else if (100<= instruction && instruction <=199){
lmsm_i_add(our_little_machine, instruction -100);
} else if (200<= instruction && instruction <=299){
lmsm_i_sub(our_little_machine, instruction -200);
} else if (600<= instruction && instruction <=699){
lmsm_i_branch_unconditional(our_little_machine, instruction -600);
} else if (instruction ==920){
lmsm_i_push(our_little_machine);
} else if (instruction ==930){
lmsm_i_sadd(our_little_machine);
} else {
our_little_machine->error_code = ERROR_UNKNOWN_INSTRUCTION;
our_little_machine->status = STATUS_HALTED;
}
lmsm_cap_value(&our_little_machine->accumulator);
}
void lmsm_load(lmsm *our_little_machine, int *program, int length){
for (int i =0; i < length; ++i){
our_little_machine->memory[i]= program[i];
}
}
void lmsm_init(lmsm *the_machine){
the_machine->accumulator =0;
the_machine->status = STATUS_READY;
the_machine->error_code = ERROR_NONE;
the_machine->program_counter =0;
the_machine->current_instruction =0;
the_machine->stack_pointer = TOP_OF

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!