Online Book Reader

Home Category

Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [77]

By Root 1545 0
together and then subtract an 8-bit number from that sum and store the result. That would require a Load instruction, two Add instructions, a Subtract, and a Store. But what if we also wanted to subtract other numbers from that original sum? That sum isn't accessible. We'd have to recalculate it every time we needed it.

The problem is that we've built an automated adder that addresses the Code memory and the Data memory simultaneously and sequentially beginning at address 0000h. Each instruction in the Code memory corresponds to a location in the Data memory at the same address. Once a Store instruction causes something to be stored in the Data memory, that value can't later be loaded back into the accumulator.

To fix this problem, I'm going to make a fundamental and excruciating change to the automated adder that will at first seem insanely complicated. But in time, you'll see (I hope) that it opens a wide door of flexibility.

Here we go. We currently have seven opcodes:

Operation

Code

Load

10h

Store

11h

Add

20h

Subtract

21h

Add with Carry

22h

Subtract with Borrow

23h

Halt

FFh

Each of these codes occupies 1 byte in memory. With the exception of the Halt code, I now want each of these instructions to require 3 bytes of memory. The first byte will be the code itself, and the next 2 bytes will be a 16-bit memory location. For the Load instruction, that address indicates a location in the Data RAM array that contains the byte to be loaded into the accumulator. For the Add, Subtract, Add with Carry, and Subtract with Borrow instructions, that address indicates the location of the byte that's to be added to or subtracted from the accumulator. For the Store instruction, the address indicates where the contents of the accumulator are to be stored.

For example, just about the simplest chore that the current automated adder can do is add two numbers together. To do this, you set up the Code and Data RAM arrays this way:

In the revised automated adder, each instruction (except Halt) requires 3 bytes:

Each of the instruction codes (except Halt) is followed by 2 bytes that indicate a 16-bit address in the Data RAM array. These three addresses happen to be 0000h, 0001h, and 0002h, but they could be anything.

Earlier I showed how to add a pair of 16-bit numbers—specifically 76ABh and 232 Ch—using the Add and Add with Carry instructions. But we had to store the 2 low-order bytes of these numbers at memory locations 0000h and 0001h, and the 2 high-order bytes at 0003h and 0004h. The result of the addition was stored at 0002h and 0005h.

With this change, we can store the two numbers and the result in a more rational manner, and perhaps in an area of memory that we've never used before:

These six locations don't have to be all together like this. They can be scattered anywhere throughout the whole 64-KB Data RAM array. To add these values at these memory locations, you must set up the instructions in the Code RAM array, like this:

Notice that the 2 low-order bytes located at addresses 4001h and 4003h are added first, with the result stored at address 4005h. The 2 high-order bytes (at addresses 4000h and 4002h) are added with the Add with Carry instruction, and the result is stored at address 4004h. And if we were to remove the Halt instruction and add more instructions to the Code memory, a sub-sequent calculation could later make use of the original numbers and the sum of them simply by referring to these memory addresses.

The key to implementing this design is to have the data output of the Code RAM array go into three 8-bit latches. Each of these latches stores one of the bytes of the 3-byte instruction. The first latch stores the instruction code, the second latch stores the high-order byte of the address, and the third latch stores the low-order address byte. The output of the second and third latches becomes the 16-bit address of the Data RAM array:

The process of retrieving an instruction from memory is known as the instruction fetch. In our machine, each instruction is 3 bytes

Return Main Page Previous Page Next Page

®Online Book Reader