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;
}
目录
相关文章
|
3天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
1天前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。
|
2天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
|
5天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
555 2
|
3天前
|
Linux 虚拟化 iOS开发
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
793 4
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
kde
|
5天前
|
人工智能 关系型数据库 PostgreSQL
n8n Docker 部署手册
n8n是一款开源工作流自动化平台,支持低代码与可编程模式,集成400+服务节点,原生支持AI与API连接,可自托管部署,助力团队构建安全高效的自动化流程。
kde
374 3
|
4天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践
本文介绍RAG(检索增强生成)技术,结合Spring AI与本地及云知识库实现学术分析AI应用,利用阿里云Qwen-Plus模型提升回答准确性与可信度。
264 91
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践