6502 Machine Code for Humans
Total Page:16
File Type:pdf, Size:1020Kb
6502 MACHINE CODE FOR HUMANS 6502 Machine Code for Humans Other books on machine code from Granada Discovering BBC Micro Machine Code A. P. Stephenson 0 246 12160 2 Advanced Machine Code Techniques for the BBC Micro A. P. Stephenson and D. J. Stephenson 0 246 12227 7 Electron Machine Code for Beginners lan Sinclair 0 246 12152 1 Advanced Electron Machine Code Techniques A. P. Stephenson and D. J. Stephenson 0 246 12403 2 Introducing Commodore 64 Machine Code Ian Sinclair 0 246 12338 9 Advanced Commodore 64 Machine Code Programming A. P. Stephenson and D. J. Stephenson 0 246 12442 3 Z80 Machine Code for Humans Alan Tootill and David Barrow 0 246 12031 2 6502 Machine Code for Humans Alan Tootill and David Barrow GRANADA London Toronto Sydney Newark Granada Technical Books Granada Publishing Ltd 8 Grafton Street, London W1X 3LA First published in Great Britain by Granada Publishing 1984 Copyright© Alan Tootill and David Barrow 1984 British Library Cataloguing in Publication Data Tootill, Alan 6502 machine code for humans. 1. 6502 (Microprocessor)—Programming 2. Machine codes (Electronic computers) I. Title IL Barrow, David 001.64'25 QA76.8.S63 ISBN 0-246-12076-2 Typeset by V & M Graphics Ltd, Aylesbury, Bucks Printed and bound in Great Britain by Mackays of Chatham, Kent All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior permission of the publishers. Contents Preface vii 1 Getting Started I 2 Instruction Set Boosters 7 3 Organising for Character Display 22 4 Delays 32 5 Screen Filling and Scrolling 36 6 Displaying Text 45 7 Matching Input 53 8 Prompting 56 9 Conversion 63 10 Calculator Program 67 11 Relocatable Code 78 12 Points on Display 83 13 Lines and Shapes 93 14 Changing Shapes 100 15 Communicating 112 16 Wrinkles 117 Appendix A: The 6502 Instruction Set 121 Appendix B: Binary and Hexadecimal Number Systems 133 Appendix C: ASCII Character Codes 137 Index of Routines 139 Index 141 Preface Machine code is the language of the processor at the heart of your computer system. As its name suggests, it is a language designed for a machine to use. But the language and the machine are both products of human minds and machine code is just as much a language for humans. Unlike many other books on machine code, this book does not systematically explain the operation of each instruction in excessive detail. Instead, it presents you with simple, understandable tasks in computing that are carried out by groups of instructions (routines). Each task is described, the method explained and sufficient documentation given for you to work through the routines and see the part played by each instruction. Just as the words of a natural language come alive only in phrases and sentences, the instructions in a machine language have meaning only in how they relate to a complete process. We do not expect you to find this an easy or superficial book. Learning any language is hard work and, perhaps, machine code is more so than others since it demands attention to absolutely precise detail. But the effort has its rewards. On the practical side, a knowledge of machine code will enable you to understand more about the way your computer works and so put it to more effective use. On the recreational side - and we are dealing with personal computers, not the company mainframe - writing machine code that will directly control every action your computer makes can be as satisfying and pleasurable as playing chess or solving crossword puzzles. Writing better, more efficient code is a challenge to the intellect; machine code is habit-forming. We would like to thank Richard Miles and the staff of Granada Publishing for their patience and fine efforts with the manuscript, also our families who accepted our preoccupation with fortitude. Not least, we wish to thank the contributors to our machine code viii Preface series PCW SUB SET in the British magazine, Personal Computer World, whose enthusiasm for machine code programming encour aged us to write this book. Alan Tootill David Barrow Chapter One Getting Started When a personal computer is switched on, the processor automatically carries out a cycle of fetching and executing instructions that are stored in the computer’s memory. Only instructions of a limited form known as machine code are recognised by the processor. Any other form of instruction, such as that encountered in a BASIC program, has to be converted into equivalent machine code for execution, either in advance by a program called a compiler, or whilst running by a program called an interpreter. Computer codes, representing instructions and data, are binary numbers, usually expressed in the more convenient hexadecimal notation. Appendix B gives a brief explanation of these number systems as well as 2’s complement and binary coded decimal (BCD). Since even hexadecimal codes are difficult to use, machine code programming is usually done with the aid of a program called an assembler which converts easily remembered instruction mnemonics into actual machine code. Assemblers allow you to enter and edit instructions in mnemonic form and to use labels for branch and subroutine call destinations, calculating the addresses for you. If you insert or delete instructions, the source code (mnemonics) can be reassembled into machine code, the assembler taking care of all address changes. Hand-assembly is the process of carrying out the functions of an assembler by hand - tedious but necessary if you do not have an assembler. In Appendix A, and throughout the book, we give assembler mnemonics alongside the machine code. The 6502 processor As far as the software writer is concerned, the 6502 processor consists of one 16-bit register, five 8-bit registers and internal logic 2 6502 Machine Code for Humans which deals with arithmetic and data movement. The 16-bit register is used by the processor as a pointer to the memory location where the next instruction will be found. Three of the 8-bit registers are accessible to the programmer for storing values (0 to 255), carrying out arithmetic, acting as loop counters, and so on. The fourth is really a collection of 8 single bits (flags) giving certain information about processes carried out. The fifth 8- bit register is the stack pointer which indexes a reserved area of RAM called the stack. Use of the stack is explained below. The internal logic which decodes instructions need not concern us. Provided the processor is fed with the correct data and the right instructions in the right order, it will do the job we want. We do have to know how to make the processor act on data in specified memory locations using the correct addressing mode. Fuller information about the 6502 registers, flags and addressing modes is given in Appendix A where it is handy for quick reference. The 6502 stack The 6502 stack occupies page 1 of memory (addresses $0100 to $01FF) and is used primarily for storage of the current program address when a subroutine is called. It is also used for temporary storage of register contents while the registers are used for other purposes. The stack is indexed by the 8-bit stack pointer. When the computer is switched on this takes a random value and has to be initialised via the X register using the TXS instruction. The usual initialisation value is $FF - the highest stack address. Thereafter the stack pointer will keep track of the next stack location available for storage of an 8-bit value. JSR (jump to subroutine) causes a sequence of four actions affecting the stack and stack pointer (S). (1) The high order byte of the Program Counter (PC) is copied to stack at S0100+S. (2) S is decreased by 1 to point to the next free stack location. (3) The low order byte of the PC is copied to S0100+S. (4) S is again decreased by 1. The PC is then loaded with the subroutine address from the JSR instruction and execution continues at the subroutine. At the end of Getting Started 3 the subroutine, the instruction RTS (return from subroutine) causes the reverse sequence: (1) S is increased by 1. (2) Lo-byte PC is loaded from $0100+S. (3) S is increased by 1. (4) Hi-byte PC is loaded from S0100+S. PC is increased by I and program execution then continues from the instruction following the JSR. Register values can also be saved (pushed) to and restored (pulled) from stack using the PH A, PHP, PLA and PLP instructions. Great care must be taken to ensure that all restoration of register values and PC addresses is carried out in exact reverse order from that in which they were saved. If not, a stacking error will result and the computer will attempt to continue program execution at an erroneous address. In the vast majority of cases this will cause a system crash. You must also be aware of the highest number of bytes that your program will need to save on stack at any one time. The 6502 stack will accommodate only 256 bytes - that is, 128 addresses. After that the stack pointer is decreased from $00 to $FF by a ‘wraparound’ decrement and once again indexes the higher end of the stack page. Pushing any more addresses or register values will cause previously stacked values to be overwritten. In Chapter 2 we give routines which provide you with a ‘user stack’.