ATARI 130XE MACHINE LANGUAGE FOR THE ABSOLUTE BEGINNER Kevin Bergin _ _l_j_ .. .. ,,- ~~ MELBOURNE HOUSE PUBLISHERS © 1985 Beam Software All rights reserved. This book is copyright and no part may be copied or stored by electromagnetic, electronic, photographic, mechanical or any other means whatsoever except as provided by national law. All enquiries should be addressed to the publishers: IN THE UNITED KINGDOM - Melbourne House (Publishers) Ltd Castle Yard House Castle Yard Richmond, TW10 6TF IN AUSTRALIA - Melbourne House (Australia) Pty Ltd 2nd Floor, 70 Park Street South Melbourne, Victoria 3205 ISBN 0 86161 200 0 Edited by Richard Woolcock Printed and bound in Great Britain by Short Run Press Ltd, Exeter Ed,tlon: 7 6 5 4 3 2 I Prinling: FED C B A 9 8 7 6 5 4 3 2 I Year: 9089 88 87 86 85 Contents Chapter 1 - Introduction to Machine Language 1 Using a machine language program. 2 Memory addressing. 2 Using memory directly from BASIC 3 Machine language as a subroutine 3 Summary 5 Chapter 2 - Basics of Machine Language Programming. 7 Using memory from machine language 7 The registers . 7 The accumulator 7 Addressing modes 8 Simple program input 10 Assembly language 11 Screen memory 12 Printing a message 14 Summary 16 Chapter 3 - An Introduction to Hexadecimal 19 Uses of Hexadecimal. 19 Binary . 20 Why hexadecimal? 22 Hex and Binary maihematically 23 Absolute addressing 24 Converting hexadecimal to decimal. 25 Summary 26 Chapter 4 - Introduction to ALPA + Disassembler. 29 To get ALPA running . 31 Using ALPA 31 ALPA commands 32 Memory Usage in ALPA 33 Summary 35 Chapter 5 - Microprocessor Equipment 37 Storing numbers 37 The carry flag 37 Adding numbers 39 Two byte addition 41 Subtracting numbers . 45 An exercise . 45 Summary 46 Chapter 6 - Program Control . 47 Player Missile Graphics . 47 Moving Player-Missile Graphics. 48 Looping using JMP 48 ALP A label name addressing 49 Infinite loops 50 Comparing numbers 51 Branch instructions 52 Zero Flag. 52 Relative addressing 55 Summary 56 Chapter 7 - Looping and Pointing . 59 Counting to control a loop .. ..... .. . 59 Counting using the accumulator 59 Counting using memory 60 The X and Y registers 61 Using the X register as a counter . 62 Moving blocks of memory . 63 Implied addressing 64 Relative addressing . 64 Absolute addressing 64 Indexed addressing . 64 Using the X register as an index. 64 Non-symmetry of commands 65 Searching through memory . 65 Using more than one Index 68 Zero page indexed addressing 68 Summary 69 Chapter 8 - Using Information Stored in Tables .... .. . . . 71 Displaying characters as graphics . 71 Graphics memory 72 Copying the character sets from ROM . 73 Indirect indexed addressing. . . . . . . . . . 74 Register transfer instructions . 76 Indirect addressing 77 Summary 78 Chapter 9 - Processor Status Codes . . . . . . . . . 81 BCD representation . 82 Summary 85 Chapter 10 - Logical Operators and Bit Manipulators 87 Changing bits within memory 87 The logical AND 88 The logical OR . 89 The logical Exclusive OR . 90 The bit instruction 91 Rotating Bits within a byte 91 Rotation with carry 93 Rotating to the right 94 Clever multiplication 95 Summary 98 Chapter 11 - Details of Program Control . 99 The Program Counter 99 Storing into the Program counter 100 The Program counter and subroutines 100 The stack control structure 101 Subroutines and the Stack . 102 The Stack and interrupts 106 Summary 107 Chapter 12 - Dealing with the Operating System 104 The Kernal 104 CLI 110 RTI 110 Summary 112 Appendices Appendix 1 - 6502 Instruction Codes . 113 Appendix 2 - Hexadecimal to Decimal Conversion Table 121 Appendix 3-Relative Branch and Two's Complement Numbering Tables 123 Appendix 4 - Atari 130XE Memory Map 124 Appendix 5 - The Screen Chip 125 Appendix 6 - The Sound Chip 130 Appendix 7 - Memory Usage Directory 133 Appendix 8 - Table of Screen Codes 136 Appendix 9 - Current Key Pressed . 138 Appendix 10 - ALPA and Disassembler 139 Index 153 Forevvord So, you feel you've had enough of BASIC and want to learn more about your machine. Maybe you use your computer to run some professionally written software, like word processing, accounting systems, educational software or games. You may have wondered what it is that makes these programs so different from the ones you have written in BASIC. These professional programs seem to be able to do many tasks at the same time, including functions which you may have not realised that your computer can do. Apart from the size of the programs and the amount of time spent in writing them, the one major difference between your programs and most of the programs that you will buy in a store, is that most professional programs are written wholly or partly in machine language. Machine language is a must for the really serious programmer. Most games, useful utilities and interfaces are written in machine language. This book attempts to give you an introduction to the world of machine language, the other side of your 130xE. You will be led through the microprocessor's instruction set slowly at first, practising each instuction learned using the monitor/program entry program ALPA (Assembly Language Programming Aid). As we work through the instruction set you will meet the new concepts and features of your computer, some of which you may not have known it possessed. You are encouraged throughout the book to check that the computer's output is what you would logically expect it to be. Keep a pen and paper close at hand LO copy on paper what the microprocessor is doing, to get its answers, and to see if your answers agree. Chapter 1 Introduction to Machine Language One advantage of machine language (M.L.) is that it allows the programmer to perform several functions not suited to BASIC. The most remarkable advantage of machine language, however, is its speed. On the 130XE you can carry out approximately one hundred thousand M.L instructions per second. BASIC commands are several hundred times slower. This is due to the fact that BASIC is written in machine language and one single BASIC command may be a machine language program of hundreds of instructions. This is reflected in the capabilities of each of the languages. Machine language instructions, as you will see as you work your way through this book, are extremely limited in what they can do. They perform only minute tasks and it takes many of them to achieve any 'useful' function. They perform tasks related to the actual machinery of the computer. They tell the computer to remember some numbers and forget others, to see if a key on the keyboard is pressed, to read and write data to the cassette tape and to print a character on the screen. Machine language programs can be thought of as subroutines like a subroutine in BASIC - a program within another program that can be used anywhere in the program and returns to where it was called from when finished. You use the commands COSUB and RETURN to execute and then return from a subroutine. 10 COSUB 8000 8000 RETURN 1 This wouldn't be a very useful subroutine because it doesn't do anything but it does show how a subroutine works! Using a machine language program To call a machine language subroutine from a BASIC program you use the command 'A=USR (address)' where A is a dummy variable. Just as with the COSUB command you must tell the computer where your routine starts. 'COSUB 8000' calls the subroutine at line number 8000. Similarly A=USR (8000) calls the machine language subroutine at memory address 8000. NOTE here that memory address 8000 is very different to line number 8000. A memory address is not a program line number, it is the 'address ' of an actual piece of memory in the computer. Memory addressing Each piece of memory in the computer can be visualised as a box which can contains one character, one piece of information. With over 65,000 separate boxes, the computer must have a filing system to keep track of them, so that it can find each separate piece of information when it needs it. The filing system it uses gives each box an 'address', which is like the address of your house. You use addresses to find the dne particular house you are looking for anywhere within a busy city. You use this address to visit a house, send it mail or to pick up a parcel from it. The computer, like us, sends information and moves from one place (subroutine) to another using its system of addresses. The computer's system of addressing is simpler than ours in its terms, anyway - as it starts at one end of memory and calls this address zero. It then counts through the memory 'boxes ~, giving each of them a number as it goes - from zero at one end to 65535 right at the other end of memory. For us this would be very difficult to remember, but for the computer it is the logical way to do things. These numbered boxes can be thought of as post office boxes. If you put something in the box at address number one, it will stay there until you replace it with something else. Each box can hold only one thing at a time. When you put something in a box, what was originally there wi 11 be lost forever. The command 'A=USR (8000) ' tells the BASIC to execute a machine language subroutine whose first instruction is stored in the box at address 8000. 2
Description: