ZOJ1098-Simple Computer
来源:互联网 发布:仿手绘软件 编辑:程序博客网 时间:2024/05/09 18:37
模拟CPU
要注意的几个地方
pc > 31 accu < 0 accu > 255
#include <cstdio>enum Instructions{iSTA,iLDA,iBEQ,iNOP,iDEC,iINC,iJMP,iHLT};int strToInt( char * str ){int result = 0;for(int i=7;i>=0;i--){if( str[7 - i] == '1' )result += ( 1 << i );}return result;}void intToStr(int x){char buf[9 + 1];buf[8] = '\0';for(int i=0;i<8;i++){if( ( x >> ( 7 - i) ) & 1 )buf[i] = '1';elsebuf[i] = '0';}printf("%s\n", buf);}int memBuf[32 + 1];char strBuf[8 + 1];int main(){int pc;int accu;while( scanf("%s", strBuf) != EOF ){pc = 0;accu = 0;memBuf[0] = strToInt( strBuf );for(int i=1;i<32;i++){scanf("%s", strBuf);memBuf[i] = strToInt(strBuf);}while(true){int inst = memBuf[pc++];pc %= 32;int type = inst >> 5;int addr = inst % 32;if( type == iSTA ){memBuf[addr] = accu;}else if( type == iLDA ){accu = memBuf[addr];}else if( type == iBEQ ){if( accu == 0 )pc = addr;}else if( type == iNOP ){}else if( type == iDEC ){accu--;if(accu < 0)accu = 255;}else if( type == iINC ){accu++;if( accu > 255 )accu = 0;}else if( type == iJMP ){pc = addr;}else //if( type == iHLT ){break;}}intToStr(accu);}return 0;}
- ZOJ1098-Simple Computer
- FOJ 1024 Simple Computer
- Simple Computer Security: Disinfect Your PC
- A Simple Introduction To Computer Networking
- computer
- computer
- computer
- computer
- computer
- Simple Computer Transfer and Backup: Don't Lose your Music and Photos
- simple
- Simple
- Computer and Computer Science
- Computer Glossary
- computer network
- Computer sid
- Computer Architecture
- computer system
- JavaBean任意层次代码生成器——基于Builder设计模式
- 笔记第10课内核启动流程
- struts 2的OGNL
- CC2541 BLE源码阅读知识积累之外设从机Peripheral工作模式
- CC2541 BLE源码阅读知识积累之OSAL小结
- ZOJ1098-Simple Computer
- 软RAID磁盘阵列配置
- 黑马程序员-基础知识梳理4
- ZOJ1099-HTML
- ISCSI 存储服务器的配置与管理
- 函数function(重点)
- Using Github for version control
- 3Dshader之球形环境映射(sphere environment mapping)
- 利用vc调用dll