ZOJ - 1098 Simple Computer 解题报告

简介: Simple Computers Time Limit: 1 Second      Memory Limit: 32768 KB You are to write an interpreter for a simple computer.
Simple Computers
Time Limit: 1 Second      Memory Limit: 32768 KB

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

 

          题意简要说明:一个简单的8位计算机,32个字节的内存单元,8位累加器,5位PC(即可寻址能力为32 bytes)。每一个存储器中的byte的高三位是指令码,低五位是存储器地址。其指令含义如上面题目中描述。现在给出存储器的32个字节的初始状态,pc(程序计数器)和accu(累加器)初始值为0,要求输出计算机对上面的代码段运行结束时的累加器值(按照二进制方式输出)。

          题目很基本,因此基本解题步骤是:解析输入,即把类似“11110000”这样的字符串换算成相应的数字,然后模拟运行,然后在输出累加器值,即再把值换算成字符串。代码如下:

 

img_1c53668bcee393edac0d7b3b3daff1ae.gif img_405b18b4b6584ae338e0f6ecaf736533.gif Code_1098
/* ZOL - 1098 Simple Computers */
#include 
<stdio.h>
#include 
<string.h>

/*计算机的32bytes内存*/
unsigned 
char memory[32];
/*pc: program counter, 程序计数器,即程序运行时的地址指针 */
unsigned 
char pc;
/*accu: accumulator, 即累加器*/
unsigned 
char accu;

/*每次读取的一行(byte)*/
char line[9];

/*解析读入的一行字节的值*/
unsigned 
char ParseFromLine(char* s)
{
    unsigned 
char result = 0;
    
int base = 128;
    
while(*s)
    {
        result 
+= (*s-'0')*base;
        
base = (base>>1);
        s
++;
    }
    
return result;
}

/*把值解析成二进制表达的字符串,准备打印*/
void ParseToLine(unsigned char number, char* buf)
{
    
int i=7;
    memset(buf, 
'0', 8);
    buf[
8]=0;/* NULL terminated */
    
while(number)
    {
        buf[i
--] = (number % 2 ) + '0';
        number 
= (number>>1);
    }
}

/*运行,返回累加器的值*/
unsigned 
char Run()
{
    
/*分别是当前指令,指令码(高三位),地址(低五位)*/
    unsigned 
char instruction, code, addr;
    
/*初始化*/
    pc
=0;
    accu
=0;    
    
    
/*运行循环*/
    
while(1)
    {
        
/*取指*/
        instruction 
= memory[pc++];
        
/*一旦pc越过内存界限,就回退到0。没有这个语句会WA,这个语句导致AC!*/
        
if(pc>=32) pc=0;
        code 
= (instruction >> 5);
        addr 
= (instruction & 0x1f);
        
        
switch(code)
        {
            
/* 000xxxxx   STA x   store the value of the accu into memory byte x */
            
case 0:
                memory[addr] 
= accu;
                
break;
            
/* 001xxxxx   LDA x   load the value of memory byte x into the accu */
            
case 1:
                accu 
= memory[addr];
                
break;
            
/* 010xxxxx   BEQ x   if the value of the accu is 0 load the value x into the pc */
            
case 2:
                
if(accu == 0) pc=addr;
                
break;
            
/* 011-----   NOP     no operation */
            
case 3:
                
break;
            
/* 100-----   DEC     subtract 1 from the accu */
            
case 4:
                accu
--;
                
break;
            
/* 101-----   INC     add 1 to the accu */
            
case 5:
                accu
++;
                
break;
            
/* 110xxxxx   JMP x   load the value x into the pc */
            
case 6:
                pc
=addr;
                
break;
            
/* 111-----   HLT     terminate program */
            
case 7:
                
return accu;
                
break;
        }
    }
    
return accu;
}

int main()
{
    
int i=0;
    unsigned 
char result;
    
while(scanf("%s",line)!=EOF)
    {
        memory[
0]=ParseFromLine(line);
        
for(i=1;i<32;i++)
        {
            scanf(
"%s",line);
            memory[i]
=ParseFromLine(line);
        }
        result 
= Run();
        ParseToLine(result, line);
        printf(
"%s\n", line);
    }
    
return 0;
}
目录
相关文章
|
9天前
|
人工智能 JSON API
全网刷屏的 Jev 模型正式开放!一手实战测评 + 保姆级教程
全网爆火的 Jev 模型是什么?有什么用?怎么使用?怎么接入 AI 编程工具?效果真的好么?傻子可懂的 Jev 保姆级实战教程 + 项目实战测评来啦
7684 13
|
7天前
|
人工智能 测试技术 API
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
Jev是TypeSafe AI推出的“系统一模型”,不生成文本,专做毫秒级结构化决策:Choice(多选)、Score(打分)、Noul(是非概率)。响应快193倍、成本低444倍,适合工单路由、内容审核、测试定级等高频判断场景。
1642 4
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
|
4天前
|
人工智能 JavaScript 芯片
DeepSeek 官方偷偷上传 Harness 桌面端安装包,我已经用上了。。附最新下载地址
DeepSeek Harness 官方的桌面端安装包被网友扒出来了,2 分钟讲明白如何使用,体验如何,适合作为 AI 编程工具么?附最新 Windows 和 Mac 双端的下载地址
1407 1
|
8天前
|
人工智能 并行计算 PyTorch
秋叶 ComfyUI 2026 整合包 v3.2 完整部署教程:Python 3.13 + Torch 2.13 全栈升级
秋叶aaaki ComfyUI 2026年8月整合包v3.2正式发布!全面升级Python 3.13.11、PyTorch 2.13.0+cu130及ComfyUI v0.30.2,原生支持MiniMax H3、Wan 2.2、Qwen-Image-2.1等2026主流音视频/图像模型,解压即用,无需环境配置。
1187 9
|
21天前
|
人工智能 自然语言处理 安全
阿里云千问办公 QwenWork详细介绍:产品核心能力、典型场景、价格及常见问题解答
千问办公是阿里云推出的一站式AI办公平台,主打"不止于对话,更注重交付",依托通义千问旗舰大模型,用户一句话即可完成数据分析、PPT生成、视频剪辑等复杂任务,直接输出可用成果。产品深度打通钉钉生态与企业OA,覆盖桌面端、网页端,提供企业标准版198元/人/月等多档订阅方案,新用户注册即赠2000积分,适配工程师、HR、财务等多职业办公场景,成为能动手干活的"全能AI同事"。
3670 10
|
5天前
|
编解码 缓存 PyTorch
16G 显卡能跑 Qwen-Image 2.1 吗?
9月20日,阿里Qwen开源Qwen-Image-2.1:7B DiT图像模型+8B文本编码器+VAE,单模型支持文生图与图像编辑,原生输出2K PNG(含Alpha通道),支持10张参考图。在自建Qwen-Image-Bench达60.28分(开源模型第一),GenAI Showdown文生图排名7/15。16G显存可跑1024×1024(需INT8量化+ComfyUI优化),但2K需24G以上。注意其Qwen Research License限非商业用途。
609 1
|
6天前
|
人工智能 编解码 并行计算
MiniMax-H3 一键整合包技术文档:8G 显存运行 AI 漫剧制作 —— 角色替换 / 动作迁移 / 文图生视频部署与调参指南
MiniMax H3 是 MiniMax 开源的全模态视频生成模型,支持文/图/音/视多条件输入,输出最高2K、15秒带双声道音频视频。本文档详述其Int8量化版在8GB显存下的本地一键部署、三段式工作流(EDIT/REPLACE/CONTINUE)、参数调优及常见问题排查。(239字)
|
16天前
|
缓存 IDE Java
【保姆级】Android Studio下载、安装和汉化教程(2026最新)
Android Studio 是 Google 官方推出的免费 Android 应用开发集成环境,基于 IntelliJ IDEA,内置模拟器、调试器、性能分析及 Compose 界面工具,功能全面,文档丰富,是安卓开发首选工具。(239字)
1720 1

热门文章

最新文章