Question: can you help me with my code the example text 1 is A 0 0 0 A 1 0 1 A 2 0 2

can you help me with my code the example text 1 is "A000A101A202A303A404A505A606A707A808A909AA0AAB0BAC0CAD0DAE0EAF0F" and example text 2 is "A101A2019010F0040112EFF8" but the output is note matcing the output givenin the image i attached. Here is my code:
#include
#include
#define MEMORY_SIZE 4096
#define REGISTER_COUNT 16
typedef struct {
unsigned short memory[MEMORY_SIZE];
unsigned short registers[REGISTER_COUNT];
unsigned short pc;
unsigned short status_flags;
} CPU;
enum Opcode {
ADD =0x0,
SUB =0x1,
MUL =0x2,
DIV =0x3,
AND =0x4,
ORR =0x5,
NOT =0x6,
MOV =0xA,
LDR =0xB,
STR =0xC,
JMP =0xE,
JEQ =0xF
};
void initialize_cpu(CPU *cpu){
memset(cpu->memory, 0, sizeof(cpu->memory));
memset(cpu->registers, 0, sizeof(cpu->registers));
cpu->pc =0;
cpu->status_flags =0;
}
void process_input(CPU *cpu){
unsigned short instruction;
int i =0;
while (scanf("%4hx", &instruction)!= EOF){
if (i >= MEMORY_SIZE){
printf("Error: Memory is full, cannot read more instructions
");
break;
}
cpu->memory[i++]= instruction;
}
if (i MEMORY_SIZE){
cpu->memory[i]=0xDEAD; // Termination instruction
} else {
printf("Error: Memory is full, cannot add termination instruction
");
}
}
void execute_instruction(CPU *cpu, unsigned short opcode, unsigned short operands){
unsigned short r1, r2, r3, rdest, raddr, rsrc, constant;
short offset;
switch (opcode){
case ADD:
r1=(operands & 0xF00)>>8;
r2=(operands & 0x0F0)>>4;
r3= operands & 0x00F;
cpu->registers[r1]= cpu->registers[r2]+ cpu->registers[r3];
break;
// Other cases for different instructions
default:
printf("Invalid opcode: %X
", opcode);
return;
}
}
void simulate_cpu(CPU *cpu){
while (cpu->memory[cpu->pc]!=0xDEAD){
unsigned short instruction = cpu->memory[cpu->pc++];
unsigned short opcode = instruction >>12;
unsigned short operands = instruction & 0x0FFF;
execute_instruction(cpu, opcode, operands);
}
}
void display_output(CPU *cpu){
for (int i =0; i REGISTER_COUNT; i++){
printf("register %2d: 0x%04X
", i, cpu->registers[i]);
}
printf("register PC: 0x%04X
", cpu->pc);
for (int i =0; i MEMORY_SIZE; i +=16){
printf("0x%04X: ", i);
for (int j =0; j 16; j +=2){
printf("%04X ", cpu->memory[i + j]);
}
printf("
");
}
}
int main(){
CPU cpu;
initialize_cpu(&cpu);
process_input(&cpu);
simulate_cpu(&cpu);
display_output(&cpu);
return 0;
}
 can you help me with my code the example text 1

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 Databases Questions!