Online Book Reader

Home Category

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

By Root 1513 0
is 30h and the address has been latched.

The Jump instruction is certainly useful. But it's not nearly as useful as an instruction that jumps sometimes but not all the time. Such an instruction is known as a conditional jump, and perhaps the best way to show how useful such an instruction can be is to pose a question: How can we persuade our automated adder to multiply two 8-bit numbers? For example, how do we get the result for something as simple as A7h times 1Ch?

Easy, right? The result of multiplying two 8-bit values is a 16-bit product. For convenience, all three numbers involved in the multiplication are expressed as 16-bit values. The first job is to decide where you want to put the numbers and the product:

Everyone knows that multiplying A7h and 1Ch (which is 28 in decimal) is the same as 28 additions of A7h. So the 16-bit location at addresses 1004h and 1005h will actually be an accumulated summation. Here's the code for adding A7h to that location once:

At the completion of these six instructions, the 16-bit value at memory locations 1004h and 1005h will equal A7h times 1. Therefore, these six instructions have to be repeated 27 more times in order for that 16-bit value to equal A7h times 1Ch. You can achieve this by typing in these six instructions 27 more times beginning at address 0012h. Or you can put a Halt instruction at 0012h and press the Reset button 28 times to get the final answer.

Of course, neither of these two options is ideal. They both require that you do something—type in a bunch of instructions or press the Reset button—a number of times that's proportional to one of the numbers being multiplied. Surely you wouldn't want to generalize this process for 16-bit values that you want to multiply.

But what if you put a Jump instruction at 0012h? This instruction causes the counter to start from 0000h again:

This certainly does the trick (sort of). The first time through, the 16-bit value at memory locations 1004h and 1005h will equal A7h times 1. Then the Jump instruction will go back up to the top. At the end of the second time through, the 16-bit result will equal A7h times 2. Eventually, it will equal A7h times 1Ch, but there's no stopping it. It just keeps going and going and going.

What we want is a Jump instruction that starts the process over again only as many times as are needed. That's the conditional jump. And it's really not that hard to implement. The first thing we'll want to add is a 1-bit latch similar to the Carry latch. This will be called the Zero latch because it will latch a value of 1 only if the output of the 8-Bit Adder is all zeros:

The output of that 8-bit NOR gate is 1 only if all the inputs are 0. Like the Clock input of the Carry latch, the Clock input of the Zero latch latches a value only when an Add, Subtract, Add with Carry, or Subtract with Borrow instruction is being performed. This latched value is known as the Zero flag. Watch out because it could seem as if it's working backward: The Zero flag is 1 if the output of the adder is all zeros, and the Zero flag is 0 if output of the adder is not all zeros.

With the Carry latch and the Zero latch, we can expand our repertoire of instructions by four:

Operation

Code

Load

10h

Store

11h

Add

20h

Subtract

21h

Add with Carry

22h

Subtract with Borrow

23h

Jump

30h

Jump If Zero

31h

Jump If Carry

32h

Jump If Not Zero

33h

Jump If Not Carry

34h

Halt

FFh

For example, the Jump If Not Zero instruction jumps to the specified address only if the output of the Zero latch is 0. In other words, there will be no jump if the last Add, Subtract, Add with Carry, or Subtract with Borrow instruction resulted in 0. Implementing this design is just an add-on to the control signals that implement the regular Jump command: If the instruction is Jump If Not Zero, the Set It signal on the 16-bit counter is triggered only if the Zero flag is 0.

Now all that's necessary to make the code shown above multiply two numbers are the following instructions starting at address 0012h:

The

Return Main Page Previous Page Next Page

®Online Book Reader