这是一个通过程序来模拟简单的冯诺衣曼体系结构的计算机
其中 内存为32B 程序计数器5比特 寄存器8比特编码
000xxxxx STA x store the value of the accu into memory byte x
001xxxxx LDA x load the value of memory byte x into the accu
010xxxxx BEQ x if the value of the accu is 0 load the value x into the pc
011----- NOP no operation
100----- DEC subtract 1 from the accu
101----- INC add 1 to the accu
110xxxxx JMP x load the value x into the pc
111----- HLT terminate program
模拟代码
#include <stdio.h>
#include <string.h>
char a[32][8];
char ac[9];
char pc[5];
char pcc[5];
int init()
{
int i=0;
int j=0;
for(i=0;i<8;i++)
{
ac[i]='0';
}
ac[9]='/0';
for(i=0;i<5;i++)
{
pc[i]='0';
}
for(i=0;i<5;i++)
{
pcc[i]='0';
}
for(i=0;i<32;i++)
{
for(j=0;j<8;j++)
{
if(scanf("%c",&a[i][j])==EOF)
return 0;
}
getchar();
}
return 1;
}
int solve1()
{
int k=0;
int ko=0;
int kk;
int kp;
for(kk=4;kk>-1;kk--)
{
ko=0;
if(pcc[kk]=='1')
{
ko=1;
for(kp=4-kk;kp>0;kp--)
ko=ko*2;
}
k=k+ko;
}
return k;
}
int solve2()
{
int k=0;
int ko=0;
int kk;
int kp;
for(kk=4;kk>-1;kk--)
{
ko=0;
if(pc[kk]=='1')
{
ko=1;
for(kp=4-kk;kp>0;kp--)
ko=ko*2;
}
k=k+ko;
}
return k;
}
void solve()
{
int e;
int aa;
int q;
int p;
q=0;
while(!(a[q][0]=='1'&&a[q][1]=='1'&&a[q][2]=='1'))
{
for(aa=4;aa>-1;aa--)
{
if(pc[aa]=='1')
pc[aa]='0';
else
{pc[aa]='1';break;}
}
if(a[q][0]=='0'&&a[q][1]=='0'&&a[q][2]=='0')
{
for(e=0;e<5;e++)
{
pcc[e]=a[q][e+3];
}
p=solve1();
for(e=0;e<8;e++)
{
a[p][e]=ac[e];
}
}
else if(a[q][0]=='0'&&a[q][1]=='0'&&a[q][2]=='1')
{
for(e=0;e<5;e++)
{
pcc[e]=a[q][e+3];
}
p=solve1();
for(e=0;e<8;e++)
{
ac[e]=a[p][e];
}
}
else if(a[q][0]=='0'&&a[q][1]=='1'&&a[q][2]=='0')
{
if(ac[0]=='0'&&ac[1]=='0'&&ac[2]=='0'&&ac[3]=='0'&&ac[4]=='0'&&ac[5]=='0'&&ac[6]=='0'&&ac[7]=='0')
for(e=0;e<5;e++)
{
pc[e]=a[q][e+3];
}
}
else if(a[q][0]=='0'&&a[q][1]=='1'&&a[q][2]=='1')
{
}
else if(a[q][0]=='1'&&a[q][1]=='0'&&a[q][2]=='0')
{
for(aa=7;aa>-1;aa--)
{
if(ac[aa]=='0')
ac[aa]='1';
else
{ac[aa]='0';break;}
}
}
else if(a[q][0]=='1'&&a[q][1]=='0'&&a[q][2]=='1')
{
for(aa=7;aa>-1;aa--)
{
if(ac[aa]=='1')
ac[aa]='0';
else
{ac[aa]='1';break;}
}
}
else if(a[q][0]=='1'&&a[q][1]=='1'&&a[q][2]=='0')
{
for(e=0;e<5;e++)
{
pc[e]=a[q][e+3];
}
}
q=solve2();
}
printf("%s/n",ac);
return;
}
int main()
{
while(init())
solve();
return 0;
}
呵呵测试数据我接下来再给 当然你也可以去JOJ上查找 题号为1099 呵呵 认真学一下啊
本文介绍了一个基于冯诺依曼架构的简单计算机模拟器实现。该模拟器使用32字节内存、5位程序计数器及8位寄存器,并支持如加载、存储等基本指令集。
295

被折叠的 条评论
为什么被折叠?



