Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions

Question

a. When did your ancestors come to the United States?

Answered: 1 week ago

Question

d. What language(s) did they speak?

Answered: 1 week ago

Question

e. What difficulties did they encounter?

Answered: 1 week ago