Table Of ContentZ80
ASSEmBLY lAnGUAGE
PROGRAmminG
Z80
ASSEmBLY LAnGUAGE
PROGRAmminG
Lance A. Leventhal
Osborne/McGraw-Hili
Berkeley, California
Published by
OSBORNE/McGraw-Hili
630 Bancroft Way
Berkeley, California 94710
U.S.A
Forinformationontranslations and bookdistributorsoutsideoftheU. S.A.,
please contact the publisherat the above address.
5 6 78 9 DODO 8 7 6 5 4 3 2
ISBN 0-931988-21-7
Copyright © 1979 by McGraw-HilL Inc.
All rights reserved. Printed in the United States of America. No part of this
publication maybereproduced. stored in anyretrieval system, ortransmitted
inanyform orbyanymeans,electronic,mechanical, photocopying,recording
orotherwise, without the prior written permission of the publishers.
Coverdesign by K. L.T. van Genderen.
This book is dedicated to my colleagues at the Society for Computer Simula
tion- Romeo Favreau. Natalie Fowler. Alexander McKenna. John McLeod.
Stanley Rogers. and Chip Stockton.
ACKNOWLEDGMENTS
The author would like to acknowledge the following people:
Mr. Curt Ingraham, Ms. Mary Borchers, and Ms. Janice Enger of Osborne/
McGraw-Hili, who made many corrections and suggestions; Mr. Winthrop
Saville of Sorrento Valley Associates, who provided assistance and exam
ples; Mr. Tom Littlefield of Littlefield/Smith Associates, who provided
reference material; Ms. Marielle Carter of Sorrento Valley Associates, who
typed some ofthe material; Mr. Stanley Rogers ofthe Societyfor Computer
Simulation,whohascontinuedtosuggestimprovementsintheauthor'swrit
ingstyle; and hiswife Donna.forherpatience and understandingthroughout
the writing of this book.
Others who provided assistance and suggestions were Mr. Colin Walsh, Mr.
Gary Hankins, Mr. Romeo Favreau, Mr. David Bulman, Ms. Kati Bulman. Mr.
Robert Turner, Mr. Irv Stafford, Mr. John Burgar, Mr. Ferenc Montvai-Lako,
andMr.Warren McKenna. Otherstudentsandcolleagues alsohelpedto keep
the author on the right track.
Theauthor, of course, bears responsibility for any remaining errors, miscon
ceptions, and misinterpretations.
v
Contents
Chapter Page
Introduction to Assembly Language Programming 1-1
HowThis Book Has Been Printed 1-1
The Meaning of Instructions 1-1
A Computer Program 1-1
The Programming Problem 1-2
USing Octal orHexadecimal 1-3
Instruction Code Mnemonics 1-4
The Assembler Program 1-5
Additional Featuresof Assemblers 1-6
Disadvantagesof Assembly Language 1-6
High-level Languages 1-7
Advantages of High-level Languages 1-7
Disadvantages of High-level Languages 1-8
High-level Languages for Microprocessors 1-9
Which Level Should You Use7 1-10
How About the Future? 1-11
Why This Book? 1-11
References 1-12
2 Assemblers 2-1
Featuresof Assemblers 2-1
Assembler Instructions 2-1
Labels 2-2
Assembler Operation Codes (Mnemonics) 2-4
Pseudo-operations 2-4
The Data Pseudo-operation 2-5
The Equate (or Define) Pseudo-operation 2-6
The Origin Pseudo-operation 2-7
The Reserve Pseudo-operation 2-7
linking Pseudo-operations 2-8
Housekeeping Pseudo-operations 2-8
Labels with Pseudo-operations 2-9
Addresses and the Operand Field 2-9
Conditional Assembly 2-11
Macros 2-11
Comments 2-13
Types of Assemblers 2-14
Errors 2-14
Loaders 2-15
References 2-15
vii
Contents (Continued)
Chapter Page
3 The Z80 Assembly Language Instruction Set 3-1
CPU Registers and Status Flags 3-2
Z80 Memorv Addressing Modes 3-4
Implied 3-5
Implied BlockTransferwith Auto-Jncrement/Decrement 3-7
Implied Stack 3-8
Indexed 3-10
Direct 3-11
Program Relative 3-12
Base Page 3-13
Register Direct 3-14
Immediate 3-15
Abbreviations 3-18
Instruction Mnemonics 3-21
Instruction Object Codes 3-21
Instruction Execution Times 3-21
Status 3-21
Instruction Descriptions 3-43
8080A/Z80 Compatibility 3-164
Zilog Z80 Assembler Conventions 3-170
Assembler Field Structure 3-170
Labels 3-170
Reserved Names 3-170
Pseudo-operations 3-170
Examples 3-171
Labels with Pseudo-operations 3-172
Addresses 3-172
Conditional Assembly 3-174
Macros 3-174
Contents (Continued)
Chapter Page
4 Simple Programs 4-1
General Formatof Examples 4-1
GUidelines for Problems 4-2
Program Examples 4-3
Ones Complement 4-3
8-Bit AddiliOn 4-4
Shift Left One Bit 4-6
Mask Off Most Significant Four Bits 4-6
ClearaMemory Location 4-7
Word Disassembly 4-7
Find LargerofTwo Numbers 4-9
16-Bit.Addition 4-11
Table of Squares 4-12
16-Bit Ones Complement 4-14
Problems 4-15
Twos Complement 4-15
8-Bit Subtractlon 4-15
Shift Left Two Bits 4-16
Mask Off Least Significant Four Bits 4-16
Set aMemory Location to All Ones 4-16
Word Assembly 4-16
Find SmallerofTwo Numbers 4-16
24-Bit .Addition 4-16
Sum of Squares 4-17
16-BitTwos Complement 4-18
5 Simple Program Loops 5-1
Examples 5-3
Sum of Data 5-3
16-Bit Sum of Data 5-6
Numberof Negative Elements 5-9
Find Maximum 5-11
Justify aBinary Fraction 5-14
Problems 5-17
Checksum of Data 5-17
Sum of 16-Bit Data 5-17
Numberof Zero. Positive. and
Negative Numbers 5-18
Find Minimum 5-18
Count 1Bits 5-18
IX