home *** CD-ROM | disk | FTP | other *** search
/ The Grim Reaper 11A / Grim_Reaper_The_Issue_11_1993___Side_A_BASIC.atr / as1.doc < prev    next >
Text File  |  2023-02-26  |  8KB  |  1 lines

  1. Assembly Language Tutorial¢¢by Chris Crawford¢¢LESSON ONE - WHY LEARN ASSEMBLY LANGUAGE?¢¢Assembly language is the great barrier that divides the professional programmer from the amateur. It is the most powerful language available for a microcomputer.¢ There are four reasons for learning to program in assembly language. First, the speed of execution of assembly language is very high - about ten times higher than BASIC on the average, perhaps a thousand times faster on certain operations. Even ACTION!, the fastest high-level language, is only about half as fast as assembly language.¢ Second, assembly language tends to be more compact than many languages.  Again, ACTION! provides a good comparison. Code produced by ACTION! is about twice as large as equivalent assembly language.¢ The third reason to program in assembly language is that assembly gives you access to features of the machine that simply are not available in high-level languages. Interrupts are the most notable examples.¢ Finally, the most important reason for learning to program in assembly language is that it will help you to understand the machine better. And that is a very good place to begin, for you cannot learn assembly language unless you know a little bit about computers.¢*¢HOW COMPUTERS WORK¢¢I am now going to describe how computers work, in very rough terms. Computers operate on a hierarchy of concepts that spans a great range, rather like the hierarchy that starts with protons and electrons, moves through atoms, molecules, cells, people to civilizations.¢ A civilization is composed of protons and electrons, but to understand how it is so composed one must know a great deal about the intermediate steps. So too is a computer composed of transistors. There are four intermediate steps between the transistor and the computer.¢ A transistor is an electrically operated switch. We can assemble transistors into gates that will turn circuits on or off depending on the states of other circuits. There are a variety of gates reflecting the various Boolean operations: AND, OR, NOT, NAND, NOR and EOR.¢ Gates can be assembled into latches, decoders, and adders. A latch is the simplest memory element: it remembers one bit of information. A decoder translates a number encoded in binary form on a few wires into a selection of one of many wires. An adder will add two one-bit values, with a carry, and generate a carry of its own. We can next broaden each of these devices into an eight-bit device by simply slinging the devices side by side. Eight one-bit latches slung side-by-side give one byte of RAM. Eight adders make an eight-bit adder.¢ We can thus create a RAM module by building many butes of RAM. We access this RAM module with three buses: a data bus, an address bus, and a control bus. The data bus carries information between the central processing unit and the RAM module.¢ The address bus is sixteen bits wide; a decoder in the RAM module takes the numeric value on the address bus and decodes it to select the single byte of RAM that is indicated by the address. The control bus establishes the direction of the data flow on the data bus and the timing of data transfer.¢ The central processing unit (CPU) represents the highest intellectual level of the computer. It is composed of four parts: the Arithmetic and Logic Unit (ALU), the registers, the address bus controller, and the instruction decoder. The ALU is composed of adders and gate arrays that crunch numbers. The particular device to use is selected with a decoder.¢ The registers are simply on-board RAM.  The address bus controller is a device that puts the desired RAM address onto the address bus. The real heart of the CPU is the instruction decoder, a very complex decoder that takes the program instructions out of RAM and translates them into action. It does this by feeding the instructions (which are numbers) into decoder circuits that activate the desired gateways in the CPU.¢*¢PROGRAMMING A MICROPROCESSOR¢¢Machine code is nothing more than a bunch of numbers that mean something to the CPU. It's hard to work with pure numbers, so we use a little code that makes it easier for us to understand the codes that the computer uses. This programmer-friendlier code is called assembly language. It is a direct, one-to-one translation of machine code.¢Here is an example of the two side by side:¢¢ Machine Code   Assembly Language¢ A9   05        LDA #FINGERS¢ 85   9C        STA COUNT¢¢ The code on the right may not look very readable, but you must agree, it's far more readable than the code on the left. And they both mean exactly the same thing.¢ Unfortunately, the computer cannot read the assembly code, only the machine code. Therefore, we need a translator program that will translate the easier-to-understand code on the right into the impossible-to-understand code on the left. This translator program is called an assembler.¢ A program that goes in the reverse direction, translating machine code to assembly, is called disassembler. It may seem like a bother to go through all the hassle of using an assembler, but it is actually much easier.¢ Assembly language is not only more readable than machine code, but it is also assembly-time relocatable; this means you can move it around in RAM freely before you start the assembly process. A good assembler also offers a number of extra features that make it easier to keep track of your program or modify it quickly.¢¢USING AN ASSEMBLER¢¢There are three steps involved in writing an assembly language program: editing, assembling, and debugging. Editing is the process of typing in your assembly language statements. Assembling is the invocation of the assembler. Debugging is the process of running your program and analyzing why it doesn't work. Thus, the entire process of writing an assembly-language process can be described by a fictitious BASIC program:¢¢ FOR I=1 TO 1,000,000,000¢ EDIT PROGRAM¢ ASSEMBLE PROGRAM¢ DEBUG PROGRAM¢ NEXT I¢*¢THE 6502 MICROPROCESSOR¢¢The first item in the 6502 that I will describe is the accumulator. This is a single one-byte register in the 6502.  It is the central workbench of the microprocessor; almost everything happens in the accumulator. Your first three instructions on the 6502 are:¢¢ LDA address¢¢(Load the Accumulator with the contents of address)¢¢ This instruction loads the accumulator with the contents of the memory location specified by the value of address. The address can be specified by either an outright value, such as $0600, or a symbolic reference, such as FISH, where the value of FISH has been previously declared by, say, an ORG statement or an equate statement.¢¢ LDA #value¢¢(Load the Accumulator with value)¢¢ This is much like the earlier statment; it loads the accumulator with a number, only the number loaded is specified immediately rather than stored in a memory location. Thus, the command LDA #9 will put a 9 into the accumulator.¢¢ STA address¢¢(Store the Accumulator into address)¢¢ This command will store the contents of the accumulator into the RAM location whose address is specified in the command. It is just like the first command, except that the direction of data motion is reversed. The LDA command is like a read, which the STA is like a write.¢ You are now equipped to move data around inside the computer. These commands will allow you to read data from one area of memory and store it into another. LDA and STA are the two most common instructions used in any 6502 program.¢ Exercise: Write a program that will read the contents of address $FE00 and store the result into address $680. Your biggest problem here will be just getting your assembler to work. Therefore, I will give the answer away:¢¢ ROMADD ORG $FE00¢ RAMADD ORG $680¢        ORG $600¢        LDA ROMADD¢        STA RAMADD¢        BRK¢        END¢¢That's the program. Try to get it running with your assembler.¢