Users browsing this forum: Google [Bot] and 2 guests. Subroutines vs. Main Program. Posted: Sun May 28, pm.
|Published (Last):||1 November 2007|
|PDF File Size:||17.29 Mb|
|ePub File Size:||12.42 Mb|
|Price:||Free* [*Free Regsitration Required]|
This banner text can have markup. Search the history of over billion web pages on the Internet. The authors and the publishers make no expressed or implied warranty of any kind with regard to these programs nor the sup- plementary documentation in this book. In no event shall the authors or the publishers be liable for incidental or consequential damages in con- nection with or arising out of the furnishing, performance, or use of any of these programs.
Berkeley, California U. For information on translations and book distributors outside of the U. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of , no part of this publication may be reproduced or distributed in any form or by any means, or stored in a data base or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.
Text design by Paul Butzler. It contains an overview of assembly language program- ming for a particular microprocessor and a collection of useful routines. In writing the routines, we have used a standard format, documentation package, and parameter passing techniques. We have followed the rules of the original manufacturer's assembler and have described the purpose, procedure, param- eters, results, execution time, and memory usage of each routine. This overview of assembly language programming provides a summary for those who do not have the time or need for a complete textbook such as is pro- vided already in the Assembly Language Programming series.
Chapter 1 contains an introduction to assembly language programming for the particular processor and a brief summary of the major features that differentiate this processor from other microprocessors and minicomputers. Chapter 2 describes how to imple- ment instructions and addressing modes that are not explicitly available. Chapter 3 discusses common errors that the programmer is likely to encounter. The collection of routines emphasizes common tasks that occur in many applications such as code conversion, array manipulation, arithmetic, bit manipulation, shifting functions, string manipulation, summation, sorting, and searching.
You should be able to use these routines as subroutines in actual applications and as guidelines for more complex pro- grams. We have aimed this book at the person who wants to use assembly language immediately, rather than just learn about it. This book can also serve as supplementary material for students of the Assem- bly Language Programming series.
This book should save the reader time and effort. There is no need to write, debug, test, or optimize standard routines, nor should the reader have to search through material with which he or she is thoroughly familiar. We have organized and indexed this book for rapid use and reference. Obviously, a book with such an aim demands response from its readers. We have, of course, tested all the programs thoroughly and documented them carefully.
If you find any errors, please inform the publisher. If you have sugges- tions for additional topics, routines, programming hints, index entries, and so forth, please tell us about them.
We have drawn on our programming experience to develop this book, but we need your help to improve it. We would greatly appreciate your comments, criticisms, and suggestions. The stack pointer always contains the address of the next available stack location on page 1 of memory addresses l6 through 01FF, 6. The lower address holds the less significant byte and the higher address holds the more significant byte. Since the pro- vides automatic wraparound, addresses 00FF l6 and ]6 form a rarely used pair.
BLOCK Reserve bytes of memory; reserve the specified number of bytes of memory for temporary storage. BYTE Form byte-length data; place the specified 8-bit data in the next available memory locations.
DBYTE Form double-byte word length data with more significant byte first; place the specified bit data in the next available memory locations with more significant byte first. END End of program. EQU Equate; define the attached label. This chapter provides a brief guide to assembly language pro- gramming for those who have an immediate application in mind. Being aware of these unusual features can save you a great deal of time and trouble. The Carry flag acts as an inverted borrow in subtraction.
The SBC instruction accounts for this inversion by subtracting 1 -Carry from the usual difference. Thus, the Carry has the opposite meaning after subtraction or comparison on the than it has on most other computers. If you wish to exclude the Carry flag, you must clear it before addition or set it before subtraction. There are no bit registers and no operations that act on bit quantities.
The lack of bit registers is commonly overcome by using pointers stored on page and the indirect indexed postindexed addressing mode. However, both initializing and changing those pointers require sequences of 8-bit operations. For many other instructions, however, you can simulate indirect addressing by clearing index register Y and using indirect indexed addressing, or by clearing index register X and using indexed indirect addressing.
Both of these modes are limited to indirect addresses stored on page 0. The stack pointer contains the less significant byte of the next empty address. Thus, the stack is limited to bytes of memory. RTS Return from Subroutine loads the program counter from the top of the stack and then adds 1 to it.
You must remember this offset of 1 in debugging and using JSR or RTS for purposes other than ordinary calls and returns. The Decimal Mode D flag is used to perform decimal arithmetic. When this flag is set, all additions and subtractions produce decimal results. Increments and decrements, however, produce binary results regardless of the mode.
The problem with this approach is that you may not be sure of the initial or current state of the D flag the processor does not initialize it on Reset. A simple way to avoid problems in programs that use Addition or Subtraction instructions is to save the original D flag in the stack, assign D the appropriate value, and restore the original value before exiting. Interrupt service routines, in particular, should always either set or clear D before executing any addition or subtraction instruc- tions.
The overall system startup routine must initialize D usually to 0, indicating bin- ary mode, with CLD. Most based operating systems assume the binary mode as a default and always return to that mode as soon as possible. A minor quirk of the 's decimal mode is that the Zero and Negative flags are no longer universally valid. These flags reflect only the binary result, not the decimal result; only the Carry flag always reflects the decimal result.
Thus, for example, subtracting 80 l6 from 50 16 in the decimal mode sets the Negative flag since the binary result is D0 16 , even though the decimal result 70 16 has a most significant bit of 0.
Similarly, adding 50 16 and 50 16 in the decimal mode clears the Zero flag since the binary result is A0 I6 , even though the decimal result is zero. Note that adding 50 16 and 50 16 in the decimal mode does set the Carry. Thus when working in the decimal mode, the programmer should use only branches that depend on the Carry flag or operations that do not depend on the mode at all such as subtractions or comparisons followed by branches on the Zero flag.
This is not the case with the , , or Z microprocessors. Storing data in memory does not affect any flags! The index registers are only 8 bits long. This creates obvious problems in handling arrays or areas of memory that are longer than bytes.
To overcome this, use the indirect indexed postindexed addressing mode. This mode allows you to store the starting address of the array in two memory locations on page 0. Whenever the program completes a byte section, it must add 1 to the more significant byte of the indirect address before proceeding to the next section. The processor knows that it has completed a section when index register Y returns to 0.
Note that we check the Zero flag rather than the Carry flag since, as on most computers, Increment and Decrement instructions do not affect Carry.
In the first place, it allows only direct addressing absolute and zero page. Thus, you can perform the following operations without loading the accumulator at all. The processor lacks some common instructions that are available on the , , and similar processors. Most of the missing instructions are easy to simulate, although the documentation can become awkward. There is also no direct way to load or store the stack pointer this can be done through index register X , load or store the status register this can be done through the stack , or perform operations between registers one must be stored in memory.
Other missing instructions include Unconditional Relative Branch use jump or assign a value to a flag and branch on it having that value , Increment and Decrement Accumulator use the Addition and Subtraction instructions , Arithmetic Shift copy bit 7 into Carry and rotate , and Test zero or minus use a comparison with or an increment, decrement sequence.
Weller 1 describes the definition of macros to replace the missing instructions. The order of the bytes is the same as in the , Z, and microprocessors, but opposite the order used in and This convention is also used in the , but the obvious alternative last occupied location is used in the , , Z, and microprocessors.
Instructions store data in the stack using postdecrementing they subtract 1 from the stack pointer after storing each byte and load data from the stack using preincrementing they add 1 to the stack pointer before loading each byte.
Setting the flag with SEI disables the maskable interrupt and clearing the flag with CLI enables the maskable inter- rupt. This convention is the same as in the and but the opposite of that used in the , , and Z In particular, there are no address-length bit user registers. Thus, variable addresses must normally be stored in pairs of memory locations on page and accessed indirectly using either preindexing indexed indirect addressing or postindexing indirect indexed addressing.
The lack of bit registers also complicates the handling of arrays or blocks that occupy more than bytes of memory. It is the only register that normally has a zero page indexed mode except for the LDX STX instructions , and it is the only register that can be used for indexing with single-operand instructions such as shifts, increment, and decrement. It is also the only register that can be used for preindexing, although that mode is not com- mon.
Finally, it is the only register that can be used to load or store the stack pointer. Tables through contain lists of instructions having particular features. Table lists instructions that apply only to particular registers and Table lists instructions that can be applied directly to memory locations. Tables through list instructions that allow particular addressing modes: zero page Table , absolute Table , zero page indexed Table , absolute indexed Table , and preindexing and postindexing Table Only register that can be shifted with a single instruction.
Only register that can be loaded or stored using preindexed or postindexed addressing.
6502 Assembly Language Subroutines
Things are tough right now They're tough for everyone : But when coronavirus hit here in the UK, the museum had to close its doors to the public, and we lost practically all our income overnight. No visitors, no workshops, no events, no school visits We know that things are tough for everyone right now, but if you can afford to help us through these tough times please donate what you can. Stay safe, stay at home! There's over 36, exhibits here! That should keep you occupied for a bit - get searching!
donate to us!