Online Book Reader

Home Category

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

By Root 1616 0
colon, such as

0000h: LOD A,[1005h]

And here's how we can indicate some data stored at a particular address:

1000h: 00h, A7h

1002h: 00h, 1Ch

1004h: 00h, 00h

The 2 bytes separated by commas indicate that the first byte is stored at the address on the left and the second byte is stored at the next address. These three lines are equivalent to

1000h: 00h, A7h, 00h, 1Ch, 00h, 00h

So the entire multiplication program can be written as a series of statements like this:

0000h: LOD A,[1005h]

ADD A,[1001h]

STO [1005h],A

LOD A,[1004h]

ADC A,[1000h]

STO [1004h],A

LOD A,[1003h]

ADD A,[001Eh]

STO [1003h],A

JNZ 0000h

001Eh: HLT

1000h: 00h, A7h

1002h: 00h, 1Ch

1004h: 00h, 00h

The judicious use of blank lines and other white space is simply to make the whole program more readable for human beings like you and me.

It's better not to use actual numeric addresses when writing code because they can change. For example, if you decided to store the numbers at memory locations 2000h through 20005h, you'd need to rewrite many of the statements as well. It's better to use labels to refer to locations in memory. These labels are simply words, or they look almost like words, like this:

BEGIN: LOD A,[RESULT + 1]

ADD A,[NUM1 + 1]

STO [RESULT + 1],A

LOD A,[RESULT]

ADC A,[NUM1]

STO [RESULT],A

LOD A,[NUM2 + 1]

ADD A,[NEG1]

STO [NUM2 + 1],A

JNZ BEGIN

NEG1: HLT

NUM1: 00h, A7h

NUM2: 00h, 1Ch

RESULT: 00h, 00h

Notice that the labels NUM1, NUM2, and RESULT all refer to memory locations where 2 bytes are stored. In these statements, the labels NUM1 + 1, NUM2 + 1, and RESULT + 1 refer to the second byte after the particular label. Notice the NEG1 (negative one) label on the HLT instruction.

Finally, if there's a chance that you'll forget what these statements do, you can add little comments, which are in English and are separated from the actual statements by a semicolon:

BEGIN: LOD A,[RESULT + 1]

ADD A,[NUM1 + 1] ; Add low-order byte

STO [RESULT + 1],A

LOD A,[RESULT]

ADC A,[NUM1] ; Add high-order byte

STO [RESULT],A

LOD A,[NUM2 + 1]

ADD A,[NEG1] ; Decrement second number

STO [NUM2 + 1],A

JNZ BEGIN

NEG1: HLT

NUM1: 00h, A7h

NUM2: 00h, 1Ch

RESULT: 00h, 00h

I'm showing you here a type of computer programming language known as assembly language. It's something of a compromise between the naked numbers of machine code and the wordiness of our English descriptions of the instructions, coupled with symbolic representations of memory addresses. People are sometimes confused about the difference between machine code and assembly language because they're really just two different ways of looking at the same thing. Every statement in assembly language corresponds to certain specific bytes of machine code.

If you were to write a program for the computer that we've built in this chapter, you'd probably want to write it first (on paper) in assembly language. Then, once you were satisfied that it was mostly correct and ready to be tested, you would hand assemble it: This means that you would manually convert each assembly-language statement to machine code, still on paper. At that point, you can use the switches to enter the machine code into the RAM array and run the program, which means to let the machine execute the instructions.

When you're learning the concepts of computer programming, it's never too early to get acquainted with bugs. When you're coding—particularly in machine code—it's very easy to make mistakes. It's bad enough to enter a number incorrectly, but what happens when you enter an instruction code incorrectly? If you enter a 11h (the Store instruction) when you really meant to enter a 10h (the Load instruction), not only will the machine not load in the number it's supposed to, but that number will be overwritten by whatever happens to be in the accumulator.

Some bugs can have unpredictable results. Suppose you use the Jump instruction to jump to a location that doesn't contain a valid instruction code. Or suppose you accidentally use the Store instruction to

Return Main Page Previous Page Next Page

®Online Book Reader