中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 未整理篇
acm.jlu.edu.cn-1099-SimpleComputers
作者:macox 时间:2003-02-18 11:10 出处:互联网 责编:chinaitpower
              摘要:acm.jlu.edu.cn-1099-SimpleComputers
You are to write an interpreter for a simple computer. This computer uses a processor with a small number of machine instructions. Furthermore, it is equipped with 32 byte of memory, one 8-bit accumulator (accu) and a 5-bit program counter (pc). The memory contains data as well as code, which is the usual von Neumann architecture.

The program counter holds the address of the instruction to be executed next. Each instruction has a length of 1 byte - the highest 3 bits define the type of instruction and the lowest 5 bits define an optional operand which is always a memory address (xxxxx). For instructions that don't need an operand the lowest 5 bits have no meaning (-----). Here is a list of the machine instructions and their semantics:

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

In the beginning, program counter and accumulator are set to 0. After fetching an instruction but before its execution, the program counter is incremented. You can assume that programs will terminate.

Input Specification

The input file contains several test cases. Each test case specifies the contents of the memory prior to execution of the program. Byte 0 through 31 are given on separate lines in binary representation. A byte is denoted by its highest-to-lowest bits. Input is terminated by EOF.

Output Specification

For each test case, output on a line the value of the accumulator on termination in binary representation, again highest bits first.

Sample Input

00111110
10100000
01010000
11100000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00111111
10000000
00000010
11000010
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
11111111
10001001

Sample Output

10000111

#include<iostream> using namespace std; int mem[32]; int ins; int ope; int pc; int ac; int loadmem(char* s) { int d=0; for(int i=0;i<8;i++) d=d*2+s[i]-'0'; return d; } void loadins() { ins=(mem[pc]&224)>>5; } void loadope() { ope=(mem[pc]&31); } int main() { char s[9]; while(cin>>s) { mem[0]=loadmem(s); for(int i=1;i<32;i++) { cin>>s; mem[i]=loadmem(s); } pc=0;ac=0; loadins(); loadope(); pc++; while(!(ins==7)) { switch(ins) { case 0: mem[ope]=ac; break; case 1: ac=mem[ope]; break; case 2: if(ac==0) pc=ope; break; case 3: break; case 4: ac--; if(ac==-1) ac=255; break; case 5: ac++; if(ac==256) ac=0; break; case 6: pc=ope; break; case 7: goto L1; default: ; } loadins(); loadope(); pc++; if(pc==32) pc=0; } L1: int r=128; for(int i=0;i<8;i++) { if(ac>=r) { cout<<1; ac-=r; } else cout<<0; r/=2; } cout<<endl; } return 0; }

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有