Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you help me with the code where my pc count in not working properly. when in pass this text as in input A

can you help me with the code where my pc count in not working properly. when in pass this text as in input "A000A101A202A303A404A505A606A707A808A909AA0AAB0BAC0CAD0DAE0EAF0F" the output for pc should be 22 but it was 10 and 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;
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){
cpu->memory[i++]= instruction;
// printf(" from process:%04X
", instruction);
}
cpu->memory[i]=0xDEAD; // Termination instruction
}
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;
switch (opcode){
case 0x0: // ADD
case 0x1: // SUB
case 0x2: // MUL
case 0x3: // DIV
case 0x4: // AND
case 0x5: // ORR
{
unsigned short r1=(operands & 0xF00)>>8;
unsigned short r2=(operands & 0x0F0)>>4;
unsigned short r3= operands & 0x00F;
cpu->registers[r1]=(opcode ==0x0)?(cpu->registers[r2]+ cpu->registers[r3]) :
(opcode ==0x1)?(cpu->registers[r2]- cpu->registers[r3]) :
(opcode ==0x2)?(cpu->registers[r2]* cpu->registers[r3]) :
(opcode ==0x3)?(cpu->registers[r2]/ cpu->registers[r3]) :
(opcode ==0x4)?(cpu->registers[r2] & cpu->registers[r3]) :
(cpu->registers[r2]| cpu->registers[r3]);
break;
}
case 0x6: // NOT
{
unsigned short r1=(operands & 0xF00)>>8;
unsigned short r2=(operands & 0x0F0)>>4;
cpu->registers[r1]= ~cpu->registers[r2];
break;
}
case 0xA: // MOV
{
unsigned short r =(operands & 0xF00)>>8;
unsigned short constant = operands & 0x0FF;
cpu->registers[r]= constant;
break;
}
case 0xB: // LDR
{
unsigned short rdest =(operands & 0xF00)>>8;
unsigned short raddr =(operands & 0x0F0)>>4;
cpu->registers[rdest]= cpu->memory[cpu->registers[raddr]];
break;
}
case 0xC: // STR
{
unsigned short rsrc =(operands & 0xF00)>>8;
unsigned short raddr =(operands & 0x0F0)>>4;
cpu->memory[cpu->registers[raddr]]= cpu->registers[rsrc];
break;
}
case 0xE: // JMP
{
short offset = operands & 0x0FFF;
if (offset & 0x0800){
offset |=0xF000;
}
cpu->pc += offset;
break;
}
case 0xF: // JEQ
{
if (cpu->status_flags ==1){
short offset = operands & 0x0FFF;
if (offset & 0x0800){
offset |=0xF000;
}
cpu->pc += offset;
}
break;
}
default:
printf("Invalid opcode: %X
", opcode);
return;
}
}
}
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 +=8){
printf("0x%04X: ", i);
for (int j =0; j 8; j++){
if (i + j MEMORY_SIZE){
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

Database Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions