ZOJ 1098 各位大牛帮忙解决哈

barry963 2011-03-15 09:26:33
Simple Computers
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

能不能解释解释这个输出是怎么得到的,看半天看不懂....
...全文
143 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
barry963 2011-03-17
  • 打赏
  • 举报
回复
多谢啦!
[Quote=引用 7 楼 lengxujun 的回复:]
“这个不一定”具体意思是:通常指令编码结构是 操作码 + 操作数 的情况,但是
不一定是一个字节长,而操作数也有多种情况,一时半会儿也解释不清楚.
lz可以找一本80x86汇编的教程看一下指令编码的章节就能明白了.
[/Quote]
lengxujun 2011-03-16
  • 打赏
  • 举报
回复
“这个不一定”具体意思是:通常指令编码结构是 操作码 + 操作数 的情况,但是
不一定是一个字节长,而操作数也有多种情况,一时半会儿也解释不清楚.
lz可以找一本80x86汇编的教程看一下指令编码的章节就能明白了.
barry963 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lengxujun 的回复:]
引用 4 楼 barry963 的回复:
引用 3 楼 lengxujun 的回复:
相信这样要写一个解释器应该很容易了吧。


也就是说后五位的地址也是其它指令可能的数据?这个在实际中也是这样吗?

这个不一定的,和具体硬件架构有关.
在这个例子里所有指令都是定长的.而如Intel的汇编指令字长不是定长的.
而ARM的指令又是定长的.lz可以学习些汇编的知识,而其中的指令编码知……
[/Quote]

嗯嗯,知识有限,多谢您耐心的解答!
再问一个问题,我不太明白您说的“这个不一定”具体是什么意思,是不是说在有的机器上,存在内存地址也是其它指令可能的数据的情况
lengxujun 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 barry963 的回复:]
引用 3 楼 lengxujun 的回复:
相信这样要写一个解释器应该很容易了吧。


也就是说后五位的地址也是其它指令可能的数据?这个在实际中也是这样吗?
[/Quote]
这个不一定的,和具体硬件架构有关.
在这个例子里所有指令都是定长的.而如Intel的汇编指令字长不是定长的.
而ARM的指令又是定长的.lz可以学习些汇编的知识,而其中的指令编码知识部分
可以解答lz的这个疑问.
barry963 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lengxujun 的回复:]
相信这样要写一个解释器应该很容易了吧。
[/Quote]

也就是说后五位的地址也是其它指令可能的数据?这个在实际中也是这样吗?
lengxujun 2011-03-15
  • 打赏
  • 举报
回复
相信这样要写一个解释器应该很容易了吧。
lengxujun 2011-03-15
  • 打赏
  • 举报
回复
下面逐条翻译指令(事实上要求用写一个程序(解释器)来做这个工作):
00111110 ; LDA 30:从存储单元30加载数据到accu,之后accu的值为 11111111
10100000 ; INC:accu加1,溢出,之后accu的值为 00000000
01010000 ; BEQ 16:如果accu为0,将pc置为16,程序将调到存储单元16处执行,而accu不变;否则执行下一条指令
10001000 ; 被修改后存储单元3。DEC:accu减1,此时accu值为10000111
11100000 ; HLT:终止程序
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00111111 ; LDA 31:从存储单元31加载数据到accu,之后accu的值为 10001001
10000000 ; DEC:accu减1,之后accu的值为 10001000
00000010 ; STA 2:将accu的值存入存储单元3,存储单元3变为10001000,而accu不变
11000010 ; JMP 2:跳转到第3条指令执行
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
11111111
10001001

兰色标记的为最后结果值。
lengxujun 2011-03-15
  • 打赏
  • 举报
回复
先翻译一下,水平有限,多多包涵!

Simple Computers
简单计算机

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).
此外,该计算机配备有32字节的存储器、一个8-bit的累加器(accu)和一个5-bit的程序计数器(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).
每条指令长1字节--高3位定义指令类型,而低5位定义可选的操作数,该操作数总为一存储器地址(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:
对不需要的操作数的指令而言,(指令的)低5位没有意义(-----)。这里有一份指令及其语义的清单:

000xxxxx STA x store the value of the accu into memory byte x
000xxxxx STA x:将accu(累加器)存入(存储单元)x

001xxxxx LDA x load the value of memory byte x into the accu
001xxxxx LDA x:从(存储单元)x加载数据到accu(累加器)

010xxxxx BEQ x if the value of the accu is 0 load the value x into the pc
010xxxxx BEQ x:如果accu(累加器)的值为0,加载值x到pc(程序计数器)

011----- NOP no operation
011----- NOP:空指令,无操作

100----- DEC subtract 1 from the accu
100----- DEC:accu减1

101----- INC add 1 to the accu
101----- INC:accu加1

110xxxxx JMP x load the value x into the pc
110xxxxx JMP x:将x的值加载到pc

111----- HLT terminate program
111----- HLT:终止程序

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.
首先,程序计数器和累加器设为0。在取得一条指令之后,但在该指令执行前,程序计数器加1。你可假定程
序将终止。

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.
输入说明
输入文件包含几个测试用例。每个测试用例指定了在程序指令执行前存储器中的内容。字节0~31为二进制形式,每行一个字节。每个字节表示为由最高位到最低位(的形式)。输入以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.
输出说明
对每个测试用例,在终端的一行上以二进制形式输出累加器的值,(和输入一样,输出的二进制数)首先输出
(二进制数的)的高bit。

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧