Figure 9

We will also describe the "Hex Dump" and "Ins Dump" (Instruction Dump) keys, which make it easier to verify that a program has been stored correctly in memory.

Any program, from the simplest to the most complex, must go through the same stages. First, the problem must be clearly stated. Next, the problem should be broken down into a series of small pieces. Finally, each of these pieces is translated into a series of computer instructions in computer machine language which can then be stored in the computer's memory. The program is then ready to run.

A3 a first simple example, we present a program to show all the different colors the video display generator can make on the TV. We go through the stages in writing this program in some detail to illustrate how a program is written. Later, examples will be more abbreviated, so you should spend some time on this first example to insure you thoroughly understand it.

First comes the statement of the problem. In our example, we want to display all colors on the TV. However, thi3 is not really a precise enough statement of the problem. In general, it will be more useful to state the problem in a way more related to the computer. For this example, we need to recall Just how to display a color on the TV: it is done by storing a number into memory location 0009. Thus, stated more precisely, the programming problem is to store all possible numbers in succession into location 0009.

Next, we break the problem up into plece3, at the same time deciding what registers or memory locations to use in the program for storage of data. In the example, there is only one piece of data that we are concerned with: the color we are about to display. We will use the X register of the computer to store this number. (The registers as well as the Internal organization of the 6502 microprocessor are described in Chapter 3») Thus the steps to solve this simple programming problem are:

a) Put 0 into the X register.

B) Store the X register into location 0009 (to start with color 0).

C) Add 1 to the X register (to go on to the next color).

D) Jump back to step (B) (so that 3teps B and C will be repeated to go through all the colors. Refer back to Chapter 1 if you have forgotten what a Jump is.)

Note carefully how the program works. The first time Step B is executed there will be a value of zero in the X register, and thus color zero will be stored in location 0009 and displayed. The next time Step B la executed the X register will contain 1 (because 1 was added to the X register in Stop C), and thus color 1 will be displayed. The next time around the X register will contain 2 (because another 1 wa3 added to the X register the second time Step C was executed), and so color 2 will be displayed. The program will step through all the numbers, and therefore ail the colors. (In case you are wondering what happens when the X register counts up to FF (or 255 in decimal), the largest number that it can contain, it will return to zero and repeat the sequence of colors over again.)

This programming technique of executing the same instructions over and over again for different values of data (in this case for different numbers in the X register) is called a loop. Such loops are an extremely important programming tool. You will find them in virtually all the example programs and will find them to be very useful In writing your own programs. Three essential ingredients that are present in any loop are shown in their simplest forms in Steps B, C, and D of the example:

1. Do something (here Step B which displays a color).

2. Change the value of the data (here Step C which changes the value of the X register).

3. Go back to the start of the loop to do it again (here Step D which Jumps back to Step B to repeat the loop).

Most loops also have a fourth element which Is not present in this simple example: a check to see whether the program has gone through the loop enough times and 3hould go on to something else. You will see this element of loops in future examples.

Finally, you are ready to translate each of the steps in the program into specific machine language Instructions to the computer. In the 3imple example each step will correspond to one computer Instruction. In more complicated examples, as you become more proficient in programming, each step will normally correspond to several computer instructions. The complete set of instructions that the 6502 computer can execute is given in Chapter 3« For now, you will learn the instructions a few at a time a3 you go through the sample programs. In each case the introduction of an instruction will include the abbreviation for the instruction, the two digit hex code which is the actual machine language version of the instruction, and a brief description of what the instruction does. Again, for more details sec Chapter 3.

LDX I A2 Load X register immediate: this instruction puts the two-digit hex number following A2 in the program into the X register.

STX BE Store X register: this instruction puts the contents of the X register into the memory location whose ^-digit hex address follows 8E in the program.

INX E8 Increment X register: this instruction adds 1 to the X register.

JMP 4C Jump: this instruction Jumps to the memory location whose address follows 4C in the program and continues executing instructions at the new memory location.

Using these instructions, the sample program is as follows (store the program in the computer starting at memory location F100, in the RAM, a3 indicated in the first column):

address Instruction machine code comment

F100 LDX I 00 A2 00 Put 00 into X register

F102 STX 0009 8E 09 00 Put X register into 0009

F105 INX E8 Add 1 to X register

F106 JMP F102 4C 02 F1 Jump back to F102

A few peints should be noted about this program. The first instruction is stored at location F100. The address of each subsequent instruction is determined by adding the length of the previous Instruction to the address of the previous instruction. For example, the first Instruction consists of the two bytes A2 and 00, which occupy memory locations F100 and F101. The second instruction goc3 in location F102. It is three bytes long (8£, 09, and 00) and so the third instruction will go in location F105, etc. You don't need to know all of these addresses to store the program in memory, since that is done Just by repeated pre33ing of the "Store" key which automatically stores into successive memory locations. You do need to know the addresses of any Instructions to which the program will Jump so that you can code the Jump instructions. In the example, you need to know that the SIX instruction is at location F102 so you can have the program Jump to that instruction. Finally, note that when memory addresses (0009 and F102 in the example) are translated into machine code, the order of the bytes is reversed. For example, address F102 in the JMP instruction is entered as 02 first, and then PI. This is the way the 6502 expects to find its addresses^

You are now ready to store the program into memory and run it. As a reminder, the sequence of 3teps to store the program is:

1. Type in F100

2. Press "Store Address"

3- Type In A2, press "Store"

4. Type In succession 00,8E,09,00,E8,4C,02, and Fl, pressing "Store" after each two-digit hex number. If you have done this correctly, when you are done the TV display should read FT 08 02F1 .

Before running the program, it is a good idea to check the contents of memory to verify that the program was stored correctly. You could do this by fetching bytes from memory one at a time a3 described above, but there are two easier ways to do this by using the "Hex Dump" and "Ins Dump" keys. To use the Hex Dump feature, use the following sequence:

1. Type in F100

2. Pres3 "Fetch Addre33"

3. Pre33 the "Shift" key (0 on the right controller)

4. Press the "Hex Dump" key ( on the left controller). You will see an address (F100) together with two bytes of data (A2 00) on the bottom line of the TV. The two bytes are the contents of FT00 and F10U

5. Press "Hex Dump" again. You will see a new line with an addres3 (FT02) and the contents of the next two memory locations (8E 09).

6. Continue pressing "Hex Dump" to examine the remainder of the program. When you have gone through the entire program, be 3ure to press "Unshift" (the 0 key on the left controller) to return to normal mode. You will know you are in normal mode because the cursor, the little square in the top center of the TV, will begin blinking again.

To use the Instruction Dump feature, use the same sequence but press the "Ir.s Dump" key (key 8 on the left controller) instead of the "Hex Dump" key. Now you will see the abbreviation for the actual instruction, Just as you coded the program above. (Again make sure to pres3 "Unshift" when you are finished looking at the whole program.)

Finally, you are ready to run the program! The sequence of steps is as follows:

1. Type in F100

2. Press "Store Address" key

3- Press "Shift" key

Press "Run" key (1 key on left controller). At this point the MagiCard gives you a chance to stop the run sequence to prevent you from inadvertently running a program before you are ready. The screen will go blank and wait for you to pres3 either the "Game Re3et" or "Game Select" switches on the game console. Pressing the "Game Reset" switch will return you to the monitor, in case you were not really ready to run the program. However, here you are ready to run, 30

5- Press the "Game Select" switch to begin execution of the program.

You 3hould now see a brilliant display of colors on your TV. Congratulations, you have run your first program!

What next? You'll probably get tired of watching the colors fai.rly soon and will want to go on to bigger and better programs, but there are still a Tew things you can learn from this simple example. For one thing, how can you stop the program and get back to the monitor? The answer is, you can't! The program does not contain any instructions to stop, and so there is no way for it to stop. It will Just keep running, showing the colors over and over again, until you turn your computer off. In the future, It will be useful to put Instructions into your programs to allow them to stop, so thi3 i3 the first modification to make to the sample program. As part of the loop, check the "Game Reset" switch, and if it is pressed leave the color display program and go back to the monitor program. The modified program looks like this:

Was this article helpful?

0 0

Post a comment