Assembly Language for the 68000 Family
Total Page:16
File Type:pdf, Size:1020Kb
GUAGE ^UcMttaL P. SizUuijeSi A55EMBLY LANGUAGE PROGRAMMING TOR THE 68000 FAMILY Related Titles of Interest from John Wiley & Sons PROGRAMMING WITH MACINTOSH PASCAL, Swan EXCEL: A POWER USER’S GUIDE, Hodgkins JAZZ AT WORK, Venit & Bums MACINTOSH LOGO, Haigh & Radford DESKTOP PUBLISHING WITH PAGEMAKER FOR THE MACINTOSH, Bove & Rhodes SCIENTIFIC PROGRAMMING WITH MAC PASCAL, Crandall ASSEMBLY LANGUAGE PROGRAMMING FOR TNE 68000 FAMILY Thomas P. Skinner John Wiley & Sons, Inc. New York • Chichester • Brisbane • Toronto • Singapore Intel is a trademark oflntel Corporation. Amiga is a trademark of Commodore International. Apple and Macintosh are trademarks of Apple Computer, Inc. Atari and Atari ST are trademarks of Atari Corp. IBM and IBM PC are trademarks of International Business Machines, Inc. Motorola is a trademark of Motorola, Inc. Radio Shack and Color Computer are trademarks of Radio Shack. Publisher: Stephen Kippur Editor: Therese A. Zak Managing Editor: Ruth Creif Editing, Design, and Production: Publishing Synthesis, Ltd. Composition: McFarland Graphics This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional service. If legal advice or other expert assistance is required, the services of a competent professional person should be sought. FROM A DECLARATION OF PRINCIPLES JOINTLY ADOPTED BY A COMMITTEE OF THE AMERICAN BAR ASSOCIATION AND A COMMITTEE OF PUBLISHERS. Copyright ® 1988 by John Wiley & Sons, Inc. All rights reserved. Published simultaneously in Canada. Reproduction or translation of any part of this work beyond that permitted by section 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Requests for permission or further information should be addressed to the Permission Department, John Wiley & Sons, Inc. Library of Congress Cataloging-in-Publication Data Skinner, Thomas P. Assembly language programming for the 68000 family/Thomas P. Skinner, p. cm. ISBN 0-471-85357-7 1. Motorola 68000 series microprocessors—Programming. 2.Assembler language (Computer program language) I. Title: Q A76.8. M695S58 1988 87-20293 005.265—dcl9 CIP Printed in the United States of America 88 89 10 9 8 7 6 5 4 3 2 1 * To my wife Linda > PREFACE This book deals specifically with the Motorola 68000 family of microprocessors. It is primarily about assembly language programming. Chances are that a reader interested in assembly language programming is familiar with computers and their programming; In the unlikely event that you are not, and have picked up this book expecting to learn all about computers, I want to urge you to start elsewhere. In order to gain the maximum knowledge from this book, you should already be familiar with computers in general and have written some programs in a high-level language such as BASIC or Pascal. It is not necessary to know another assembly language or be an expert in computer programming. I start at a fairly low level but get up to speed pretty quickly. Those who already know another assembly language will be able to progress rapidly through the material. In the writing of this book I attempted to strike a balance between a beginner-level tutorial and the brief format of a reference manual. This level of presentation should appeal to the majority of readers. There are 15 chapters plus a number of useful appendices. Chapter 1 covers number systems. This is mostly general information, but there is a little bit of 68000-specific information here. You should look through it even if you know number systems inside out. Chapter 2 describes microcomputer architectures in general, and the 68000 specifically. Chapters 3 through 5 provide enough information to start writing com plete programs. Chapters 6 through 8 cover more advanced topics such as addressing modes and subroutines. Once through chapter 8 you will have a substantial background in 68000 assembly language. At this point Chapter 9 presents a major program, a linked list manager. This helps to cement the techniques from Chapters 1 through 8. Chapters 10 through 12 cover advanced topics such as exception handling, shift and rotate instructions, and advanced arithmetic. By the end of Chapter 12 you will know all the instructions of the 68000. Chapters 13, 14, and 15 cover the newest members of the 68000 family—the 68010,68020, and 68030. Chapter 15 should be of special interest, since it provides an introduction to the latest and most powerful 68000 processor. You will be hearing more about the 68030 as it is introduced into systems. It is destined to have a major impact on the computer systems of the next decade. vii vi il Preface Of special note is Appendix B, which provides program shells. These shells allow you to start programming with the Atari ST, the Apple Macintosh, or the Commadore Amiga. Without these program shells it would require a good deal of effort just to learn how to interface to your operating system. A number of people provided assistance along the way. Among my students who helped out were Carol Cook and An-Ping Chi. Special thanks to Mike Mellone and John Say well, who helped prepare the appendices. Finally, I would like to thank Motorola for their cooperation and permission to reprint information from their 68000 manuals. Thomas P. Skinner CONTENTS INTRODUCTION 1 CHAPTER 1: Number Systems 5 Decimal 5 Binary 6 Conversions 6 Hexadecimal 8 Arithmetic in Binary and Hexadecimal 9 Bits, Bytes, Words and Longwords 10 Representing Negative Values 11 ASCII Character Codes 12 Exercises 13 Answers 14 CHAPTER 2: Microcomputer Architecture 17 The Motorola M68000 Family 17 The CPU 20 Memory 21 User and Supervisor Modes 23 The CPU Registers 24 Input/Output 26 Exercises 26 Answers 27 CHAPTER 3: Assembler Source Format 29 The Label Field 30 The Operation Field 31 The Operand Field 31 The Comment Field 33 On Choosing Symbols 34 Constants 34 Data-Defining Directives 36 Symbol Equates 37 The END Directive 38 Exercises 38 Answers 39 x Contents CHAPTER 4: Getting Started 41 Data Movement 41 Addition and Subtraction 44 Input and Output 47 The Program Shell 49 Looping 50 Putting It All Together 53 Exercises 54 Answers 55 CHAPTER 5: Conditional and Arithmetic Instructions 57 Arithmetic and the Condition Code Register 57 The Carry Bit 58 The Overflow Bit 61 The Zero and Negative Bits 61 The Extend Bit 62 Comparisons 62 ADDQ and SUBQ Instructions 66 Exercises 68 Answers 69 CHAPTER 6: Addressing Modes 71 Register Direct Modes 72 Immediate Data 72 Absolute Addressing 73 Address Register Indirect 74 Address Register Indirect With Postincrement 77 Address Register Indirect With Predecrement 79 Address Register Indirect With Displacement 80 Address Register Indirect with Index 82 Program Counter Relative Modes 83 Addressing Mode Summary 84 Exercises 85 Answers 86 C h a p t e r 7: The Stack 89 Stack Instructions 89 Stack Applications 93 Exercises 96 Answers 96 CHAPTER 8: Subroutines 99 JSR, BSR, and RTS Instructions 99 Passing Parameters 102 Contents xl Saving and Restoring the Registers 105 Passing Parameters on the Stack 106 Stack Frames 109 Exercises 112 Answers 114 CHAPTER 9: Linked Lists—A Programming Example 117 CHAPTER 10: Logical, Shift and Rotate Instructions 129 Truth Tables 129 Logical Operations 130 Shifts 132 Rotates 136 Bit Manipulation 137 Exercises 138 Answers 139 CHAPTER 11: Advanced Arithmetic 141 Multiple Precision Addition and Subtraction 141 Multiplication and Division 145 Decimal Arithmetic 148 Exercises 152 Answers 153 CHAPTER 12; Exception Processing, System Control Operations, and I/O 155 The Status Register and System Control 155 Exception Processing 158 Traps 161 Serial I/O 163 Miscelaneous Instructions 168 Exercises 170 Answers 171 C h a p t e r 13: The 68010 173 Virtual Memory and the Bus Error Exception 173 Virtual Machines 175 Reference Classifications 177 The Vector Base Register 178 RTD and Loop Mode 179 Summary 181 Exercises 181 Answers 182 xli Contents Chapter 14: The 68020 185 Instruction Caching 186 Additional Addressing Modes 188 Instruction Extensions 190 New Instructions 193 Bit Field Instructions 193 Breakpoint Instruction 193 CALLM/RTM 194 CAS and CAS2 196 CHK2 197 CMP2 197 Coprocessor Support Instructions 197 PACK and UNPK 198 TRAPcc 199 Exercises 199 Answers 200 Chapter 15: The 68030 203 Instruction and Data Caches 204 Pipelined Architecture 206 Paged Memory Management 206 68030 Instructions 211 Exercises 212 Answers 212 Appendix A: ASCII Character Codes 215 Appendix B: Program Shells and I/O Subroutines 217 Appendix C: 68000-68020 Instruction Summary 229 Index 235 INTRODUCTION Why learn assembly language? Most people do so out of a need to perform programming tasks that are not easy, or not possible, with other languages. The popularity of the 68000 family of microprocessors, as exemplified by the sales figures of the Apple Macintosh, Commodore Amiga, Atari ST, and others, certainly makes it worthwhile to learn more about this line of micros. The particular microprocessor chip your com puter uses will remain an abstraction unless you get down to the machine language level; but since no one really programs in machine language, as sembly language is the way to gain the most complete knowledge of the 68000 family capabilities. Programming in assembly language allows the control of every aspect of the computer hardware. Many applications require procedures that are either impossible or inefficient with computer languages such as BASIC. You may be a professional computer user who has a need for a laboratory control computer, such real time applications often require some assembly language programming. Regardless of your reason for learning assembly language, it is challenging and rewarding when your programs start to run. You will feel—and be—“in control.” This book is about programming the 68000 microprocessor, not a particular computer using this chip.