How does a computer distinguish between Data and Instructions?

前端 未结 2 1619
灰色年华
灰色年华 2021-01-15 04:22

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

The fed program was:

MAIN:
    0000 0001 0100     # 0 = LDA          


        
相关标签:
2条回答
  • 2021-01-15 05:15

    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.

    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.

    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.

    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.

    0 讨论(0)
  • 2021-01-15 05:25

    In computer systems an instruction's address comes from PC (Program Counter) and data's address don't come from PC. This is how they are differentiated.

    0 讨论(0)
提交回复
热议问题