ZOJ1072 Microprocessor Simulation

      这道题我觉得加法这里比较难懂,和分成高字和低字,分别存放在寄存器AB中。

#include <iostream>
using namespace std;

const int MAXSIZE = 256;//内存最多有个字
const int WORDWIDTH = 4;//每个字位
int memory[MAXSIZE];//内存

//9种指令
typedef enum 
{
    LD ,
    ST ,
    SWP ,
    ADD ,
    INC ,
    DEC , 
    BZ , 
    BR ,
    STP 
};

int main(void)
{
    
char ch;
    
int i;
    
while (true)
    {
        
//输入数据
        memset(memory,0,MAXSIZE);//初始化内存
        ch = getchar();
        
while((ch < '0' || ch > '9'&& (ch < 'A' || ch > 'F'))
            cin
>>ch;
        
        
if(ch == '8')
            
break;
        
//输入内存数据
        for (i=1;i<MAXSIZE;++i)
        {
            cin
>>ch;
            
if (ch>='0'&&ch<='9')
            {
                memory[i] 
= ch-'0';
            }
            
else if (ch>='A'&&ch<='F')
            {
                memory[i] 
= ch-'A'+10;
            }
        }
        
int curOpt,accumA=0,accumB=0,high,low,pos,sum,pc=0;
        
bool bFinished = false;
        
while (!bFinished)
        {
            curOpt 
= memory[pc++];//当前指令
            switch(curOpt)
            {
            
case LD:
                {
//加载数据到寄存器A
                    high = memory[pc++];//高字位
                    low = memory[pc++];//低字位
                    pos = (high<<WORDWIDTH)+low;//实际位置
                    accumA = memory[pos];
                }
                
break;
            
case ST:
                {
//将寄存器A中数据存到内存
                    high = memory[pc++];//高字位
                    low = memory[pc++];//低字位
                    pos = (high<<WORDWIDTH)+low;//实际位置
                    memory[pos] = accumA;
                }
                
break;
            
case SWP:
                {
//交换两个数(这种方式避免内存溢出)
                    accumA = accumA^accumB;
                    accumB 
= accumA^accumB;
                    accumA 
= accumA^accumB; 
                }
                
break;
            
case ADD:
                {
//寄存器A和寄存器B中数据相加,低字放在A中,高字放在B中
                    sum     =   (accumA + accumB) % 0x100;
                    accumA   
=   sum & 0x0f;
                    accumB   
=   (sum & 0xf0>> WORDWIDTH;
                }
                
break;
            
case INC:
                {
//寄存器A中数据自增
                    
                    
if (++accumA==16)
                    {
                        accumA 
= 0;
                    }
                }
                
break;
            
case DEC:
                {
//寄存器A中数据自减
                    if (accumA==0)
                    {
                        accumA 
= 15;
                    }
                    
else
                        
--accumA;
                }
                
break;
            
case BZ:
                {
//寄存器A中数据等于则跳转
                    high    =   memory[pc++];
                    low     
=   memory[pc++];
                    pos     
=   (high << WORDWIDTH) + low;
                    
if(accumA == 0)
                        pc 
= pos;//程序计数器指向指定跳转位置
                }
                
break;
            
case BR://pc自增
                pc = memory[pc+1];//指向下一个位置
                break;
            
case STP://停止执行
                bFinished = true;
                
break;
            }
        }
        
//显示最终内存快照
        for (i=0;i<MAXSIZE;++i)
        {
            
if (memory[i]<10)
            {
                cout
<<memory[i];
            }
            
else
            {
                ch 
= memory[i]-10+'A';
                cout
<<ch;
            }    
        }
        cout
<<endl;
    }
    
return 0;
}

posted on 2008-10-27 20:25  Phinecos(洞庭散人)  阅读(671)  评论(5编辑  收藏  举报

导航