Answered step by step
Verified Expert Solution
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 exampleAAAAAAAAAAAAAABBACCADDAEEAFF and exampleAAFEFF the output shoud be as shown in image, but it is not and this 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;
case SUB: roperands & xF; roperands & xF; r operands & xF; cpuregistersr cpuregistersr cpuregistersr; break;
case MUL: roperands & xF; roperands & xF; r operands & xF; cpuregistersr cpuregistersr cpuregistersr; break;
case DIV: roperands & xF; roperands & xF; r operands & xF; cpuregistersr cpuregistersr cpuregistersr; break;
case AND: roperands & xF; roperands & xF; r operands & xF; cpuregistersr cpuregistersr & cpuregistersr; break;
case ORR: roperands & xF; roperands & xF; r operands & xF; cpuregistersr cpuregistersr cpuregistersr; break;
case NOT: roperands & xF; roperands & xF; cpuregistersr ~cpuregistersr; break;
case MOV: roperands & xF; constant operands & xFF; cpuregistersr constant; break;
case LDR: rdest operands & xF; raddr operands & xF; cpuregistersrdest cpumemorycpuregistersraddr; break;
case STR: rsrc operands & xF; raddr operands & xF; cpumemorycpuregistersraddr cpuregistersrsrc; break;
case JMP: offset operands & xFFF; if offset & x offset xF; cpupc offset; break;
case JEQ: if cpustatusflags offset operands & xFFF; if offset & x offset xF; cpupc offset; break;
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 ;
Example
regiater : OrDooB
register : xDooc
register : xDooD
regiater : OxDOE
register : OrF
register PC: OxDO
Ox: A k
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started