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 is AAAAAAAAAAAAAABBACCADDAEEAFF and example text is AAFEFF but the output is note matcing the output givenin the image i attached. Here is my code:
#include
#include
#define MEMORYSIZE
#define REGISTERCOUNT
typedef struct
unsigned short memoryMEMORYSIZE;
unsigned short registersREGISTERCOUNT;
unsigned short pc;
unsigned short statusflags;
CPU;
enum Opcode
ADD x
SUB x
MUL x
DIV x
AND x
ORR x
NOT x
MOV xA
LDR xB
STR xC
JMP xE
JEQ xF
;
void initializecpuCPU cpu
memsetcpumemory, sizeofcpumemory;
memsetcpuregisters, sizeofcpuregisters;
cpupc ;
cpustatusflags ;
void processinputCPU cpu
unsigned short instruction;
int i ;
while scanfhx &instruction EOF
if i MEMORYSIZE
printfError: Memory is full, cannot read more instructions
;
break;
cpumemoryi instruction;
if i MEMORYSIZE
cpumemoryixDEAD; Termination instruction
else
printfError: Memory is full, cannot add termination instruction
;
void executeinstructionCPU cpu unsigned short opcode, unsigned short operands
unsigned short r r r rdest, raddr, rsrc constant;
short offset;
switch opcode
case ADD:
roperands & xF;
roperands & xF;
r operands & xF;
cpuregistersr cpuregistersr cpuregistersr;
break;
Other cases for different instructions
default:
printfInvalid opcode: X
opcode;
return;
void simulatecpuCPU cpu
while cpumemorycpupcxDEAD
unsigned short instruction cpumemorycpupc;
unsigned short opcode instruction ;
unsigned short operands instruction & xFFF;
executeinstructioncpu opcode, operands;
void displayoutputCPU cpu
for int i ; i REGISTERCOUNT; i
printfregister d: xX
i cpuregistersi;
printfregister PC: xX
cpupc;
for int i ; i MEMORYSIZE; i
printfxX: i;
for int j ; j ; j
printfX cpumemoryi j;
printf
;
int main
CPU cpu;
initializecpu&cpu;
processinput&cpu;
simulatecpu&cpu;
displayoutput&cpu;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
