计算机如何区分数据和指令? [英] How does a computer distinguish between Data and Instructions?

查看:861
本文介绍了计算机如何区分数据和指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在8位PC上,我观看了视频的程序-手动

I watched a video on an 8-bit pc being fed a program - manually, using physics switches.

美联储程序为:

MAIN:
    0000 0001 0100     # 0 = LDA [4]
    0001 0010 0101     # 1 = ADD [5]
    0010 0101 0000     # 2 = OUT
    0011 1111 0000     # 3 = HLT

DATA:
    0100 00001110      # 4 = #14
    0101 00011100      # 5 = #28

我想知道的是计算机如何区分数据和指令,因为没有标志将数据与指令区分开。

What I want to know is how the computer, if it does, distinguishes between Data and Instructions, because there are no flags that divide data from instructions.

0001 0001 0010可以解释为:

0001 0001 0010 may be interpreted as either:

1 = LDA [2]

或:

1 = #10

是因为在程序运行时,地址被视为指令。但是由于HLT的原因,程序停止执行内存地址,就好像它们是指令一样,并留下更高的地址;然后LDA / ADD / SUB等将内存中的所有位置都视为二进制值。

Is it because while the program runs, addresses are treated as instructions. but because of the HLT, the program stops executing the memory addresses as if they were instructions, and leaves higher addresses; and then LDA / ADD / SUB etc. treat all locations in memory as binary values.

在那种情况下,将:

0000 0010 0000解释为:

0000 0010 0000 be interpreted as:

0 = ADD #32

而不是

0 = ADD [ ADD [ ADD [ ADD ...]]]

**在写这个问题时,我意识到自己正在前进的新事物

** While writing this question I realised new things as I was going along

更好的例子:

如果不存在暂停,程序是否可以正常工作,但请继续浏览数据并解释为:

If the halt wasn't there, would the program work fine, but then keep on going down to the data and interpret as:

0010 0000 1110      # 4 = NOP [14]
0101 0001 1100      # 5 = LDA [12]

如果是这样,计算机将崩溃1:因为给NOP一个操作数,而2:因为未定义内存地址12和14 。

If so, would the computer crash 1: because NOP is given an operand, and 2: because memory addresses 12 and 14 are undefined.

推荐答案

您正处在一个重要认识的边缘:没有元数据就没有意义-为了理解给定的位序列,必须有一些知识

You are on the verge of an important realization: data has no meaning without metadata - in order to make sense of a given sequence of bits, there has to be some "knowledge" about how those bits are supposed to be interpreted.

就指令而言,CPU的指令集定义了每条指令及其附带数据的大小。每条指令均以 opcode 开头,并且以下数据通常为固定大小(并且大小取决于操作码)。每条指令都是从某个硬连接到CPU的初始地址开始按顺序执行(直到遇到跳转指令为止)。

As far as instructions are concerned, the CPU's instruction set defines the size of each instruction and its accompanying data. Each instruction begins with the opcode, and the following data is typically fixed size (and the size depends on the opcode). Each instruction is executed in order (until a jump instruction is encountered), starting from some initial address that is hardwired into the CPU.

因此,如果初始地址恰好是 MAIN 标签的地址,CPU看到的第一个操作码将是 0000 0001 ,因此它将知道这是一条 LDA 指令,它知道应该跟随一个四位数字。下一条指令将紧随这四个位之后。

So if the initial address happens to be the address of the MAIN label, the first opcode the CPU sees will be 0000 0001, and so it will know that it is an LDA instruction, which it knows is supposed to be followed by a four bit number. Whatever follows those four bits is the next instruction.

如果稍后执行了错误的跳转指令,将CPU发送到示例中的第三个位会发生什么情况? (根据您的示例,我猜测CPU会以四位字节运行。)然后,确实,CPU会将 0100 0001 误认为是操作码和某些代码。以下位的位数作为该操作码的数据,则事情很可能会出错。

What happens if a bad jump instruction is executed later, sending the CPU to the third bit group in your example? (Based on your example, I'm guessing that the CPU operates with four-bit "bytes".) Then indeed, the CPU will mistake 0100 0001 for an opcode and some number of the following bits as the data for that opcode, and things will likely go very wrong.

这篇关于计算机如何区分数据和指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆