Simple Computers

这是一个通过程序来模拟简单的冯诺衣曼体系结构的计算机

其中 内存为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 呵呵 认真学一下啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值