Atari Assembler Course ----------------------------------- 1.1 Why Assembly Language To better understand the need for Assembly Programming, let's compare the different types of programming languages: interpreted, compiled, and assembled. The language we are most familiar with, Atari BASIC, falls in the first category. When an interpreted language runs, each command is examined and converted to its machine code equivalent and then executed. Because of this "one-at-a-time" method, these languages tend to be slower but require less memory. Also, the language itself requires memory (8K in the case of Atari BASIC) even with no program loaded into the computer! Some languages, like Forth, have a very fast yet small interpreter. Others like Pascal are larger and slower. The closest a language can come to the speed of machine code is a truly compiled language, such as ACTION!, that generates the "native code" of the computer itself (the 6502 microprocessor in our case). Compiled languages differ from interpreted languages in the way they are translated into machine code. Unlike an interpreter, a compiler generates the equivalent machine code for the complete program before it is run. Although this step takes extra time, it needs only to be done once and the resulting machine-code equivalent program will run much faster every time. Lastly, assembled language is a means of writing programs directly in machine code but we have the use of words to represent the lowest level operations of the microcomputer. The operational codes or "Op-codes are represented as three-letter symbolic words or "mnemonics". Unlike the other types, assembly language allows the programmer to optimize the speed, size, and timing of a program by talking in the computers own words. These advantages will easily justify the longer developement time in many applications, such as games, where timing is intricate, the size must be minimized and, naturally, it has to be fast! ----------------------------------------------------------------------- 1.2 Atari System Architecture In order to design good software with your Atari Computer, you need a good understanding of the hardware. Because Assembly Language deals directly with the machine itself, you will become good friends with "ANTIC", "GTIA", "POKEY", and the "PIA". These chips are the work-horses of the Atari and help out the 6502 with the various duties of the computer system. We will get into more detail with them through out the course. For now: Read: "DE RE ATARI", Chapter 1, (note figure on page 1-3). ------------------------------------------------------------------------ 1.3 6502 CPU Description BIT BIT BIT BIT BIT BIT BIT BIT 7 6 5 4 3 2 1 0 ,---+---+---+---+---+---+---+---. | | | | | | | | | '---+---+---+---+---+---+---+---' ACCUMULATOR ,---+---+---+---+---+---+---+---. | | | | | | | | | '---+---+---+---+---+---+---+---' X INDEX REGISTER ,---+---+---+---+---+---+---+---. | | | | | | | | | '---+---+---+---+---+---+---+---' Y INDEX REGISTER ,---+---+---+---+---+---+---+---. | N | V | X | B | D | I | Z | C | '---+---+---+---+---+---+---+---' PROCESSOR STATUS REGISTER ,---+---+---+---+---+---+---+---. | | | | | | | | | '---+---+---+---+---+---+---+---' PROGRAM COUNTER HIGH ,---+---+---+---+---+---+---+---. | | | | | | | | | '---+---+---+---+---+---+---+---' PROGRAM COUNTER LOW ,---+---+---+---+---+---+---+---. 1| | | | | | | | | '---+---+---+---+---+---+---+---' STACK POINTER 6502 Microprocessor Internal Registers ----------------------------------------------------------------- 1.3.1 Registers - A,X,Y A "register" is a memory cell, internal to the microprocessor, in which eight "bits" or binary digits are stored. The accumulator is the register where data must be stored to be operated upon. The "X" and "Y" registers are normally used to keep data for indexing into memory or as a counter in a loop. ------------------------------------------------------------------------ .3.2 Processor Status Register The next most important register in the 6502 is the process status register. As its name implies, the "P" register holds the status of many conditions in the CPU. Each bit in this register is a flag, either a "1" or a "0", that may be used as a test for branching or looping in a program. The symbolic letter for each status flag is shown in the figure above within corresponding bit of "P" register. The "C" or "carry" flag is set (equal to one) when an arithmetic operation results in a value greater than 255 (the highest value for 8 bit numbers). The "Z" flag is set when an operation results in a value equal to zero. The "I" flag is used during an "interrupting" or time-sharing portion of the program. The "D" flag is set when "decimal" arithmetic is used and reset to "0" for "binary" math. The "B" flag is set when the 6502 Break command has occurred. The "X" flag is not used. The "V" flag is set when an operation results in a value greater than +127 or less than -128 and is used for signed arithmetic. The "N" flag, or negative flag, is equal to the seventh bit of a result; this is also used for signed arithmetic. Each of the status bits may be changed in a program except for the break flag. 1.3.3 Program Counter - PC The program counter holds the value of the memory location where the next machine code instruction is located. It has a "high" and "low" byte so that 64K bytes of memory can be "addressed" with the 16 bits. Also, the "PC" is changed by the CPU as a result of a branch, jump, or subroutine in the program so that the next operation is found. We will see some other uses of the program counter later on. 1.3.4 Stack Pointer - S The Stack is another very useful aspect of the 6502 CPU. It is located in memory on "Page-One" (hex 0100 to 01FF) and is reserved for the the 6502 and the programmer for limited usage. The stack begins at the end of page-one (hex 01FF) and is filled on a last-in, first-out basis. The Stack Pointer holds the location of the "top" of the stack. 1.4 Review of Binary and Hex Math Because we are dealing directly with the microprocessor itself, it is very important to understand the operations of binary numbers and their hexidecimal equivalents. We can review this briefly during the session if there are questions. But, get a good understanding before we begin. A very good tutorial appears in the Sept/Oct. issue of "ANALOG" magazine in the column "Boot Camp" by Tom Hudson. Read it and the subsequent columns!