Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you help me with my code. When i pass these text example 1 A 0 0 0 A 1 0 1 A 2

can you help me with my code. When i pass these text example1"A000A101A202A303A404A505A606A707A808A909AA0AAB0BAC0CAD0DAE0EAF0F" and example2"A101A2019010F0040112EFF8" the output shoud be as shown in image, but it is not and this 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;
case SUB: r1=(operands & 0xF00)>>8; r2=(operands & 0x0F0)>>4; r3= operands & 0x00F; cpu->registers[r1]= cpu->registers[r2]- cpu->registers[r3]; break;
case MUL: r1=(operands & 0xF00)>>8; r2=(operands & 0x0F0)>>4; r3= operands & 0x00F; cpu->registers[r1]= cpu->registers[r2]* cpu->registers[r3]; break;
case DIV: r1=(operands & 0xF00)>>8; r2=(operands & 0x0F0)>>4; r3= operands & 0x00F; cpu->registers[r1]= cpu->registers[r2]/ cpu->registers[r3]; break;
case AND: r1=(operands & 0xF00)>>8; r2=(operands & 0x0F0)>>4; r3= operands & 0x00F; cpu->registers[r1]= cpu->registers[r2] & cpu->registers[r3]; break;
case ORR: r1=(operands & 0xF00)>>8; r2=(operands & 0x0F0)>>4; r3= operands & 0x00F; cpu->registers[r1]= cpu->registers[r2]| cpu->registers[r3]; break;
case NOT: r1=(operands & 0xF00)>>8; r2=(operands & 0x0F0)>>4; cpu->registers[r1]= ~cpu->registers[r2]; break;
case MOV: r1=(operands & 0xF00)>>8; constant = operands & 0x0FF; cpu->registers[r1]= constant; break;
case LDR: rdest =(operands & 0xF00)>>8; raddr =(operands & 0x0F0)>>4; cpu->registers[rdest]= cpu->memory[cpu->registers[raddr]]; break;
case STR: rsrc =(operands & 0xF00)>>8; raddr =(operands & 0x0F0)>>4; cpu->memory[cpu->registers[raddr]]= cpu->registers[rsrc]; break;
case JMP: offset = operands & 0x0FFF; if (offset & 0x0800){ offset |=0xF000; } cpu->pc += offset; break;
case JEQ: if (cpu->status_flags ==1){ offset = operands & 0x0FFF; if (offset & 0x0800){ offset |=0xF000; } cpu->pc += offset; } break;
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;
}
2.10.1 Example 1
regiater 11: OrDooB
register 12: 0xDooc
register 13: 0xDooD
regiater 14: OxDO0E
register 15: Or000F
register PC: OxDO22
Ox0000: A0001101 k20

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

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions