1984 Bookmatter ProgrammingInZ80AssemblyLangua

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Programming in Z80 Assembly Language

Also from Macmillan

Advanced Graphics with the BBC Model B Microcomputer


Ian O. Angell and Brian J. Jones
Advanced Graphics with the Sinclair ZX Spectrum
Ian O. Angell and Brian J . Jones
Assembly Language Programming for the Acorn Electron
Ian Birnbaum
Assembly Language Programming for the BBC Microcomputer,
second edition
Ian Birnbaum
Advanced Programming for the 16 K ZX8l
Mike Costello
Using Your Home Computer
Garth W.P . Davies
Beginning BASIC
Peter Gosling
Continuing BASIC
Peter Gosling
Practical BASIC Programming
Peter Gosling
Program Your Microcomputer in BASIC
Peter Gosling
Codes for Computers and Microprocessors
P. Gosling and Q. Laarhoven
Microprocessors and Microcomputers - their use and programming
Eric Huggins
The Sinclair ZX81 - Programming for Real Applications
Randle Hurley
More Real Applications for the ZX8l and ZX Spectrum
Randle Hurley
Digital Techniques
Noel Morris
Microprocessor and Microcomputer Technology
Noel Morris
The Alien, Numbereater, and Other Programs for Personal
Computers - with notes on how they were written
John Race
Understanding Microprocessors
B.S. Walker
Assembly Language Assembled - for the Sinclair ZX81
Anthony Woods
Programming in
Z80 Assembly Language

Roger Hutty

M
MACMILLAN
© Roger Hutty 1984

All rights reserved. No part of this


publication may be reproduced or
transmitted, in any form or by any
means, without permission.

First published 1984 by


Higher and Further Education Division
MACMILLAN PUBLISHERS LTD
London and Basingstoke
Companies and representatives
throughout the world

ISBN 978-1-349-06959-0 ISBN 978-1-349-06957-6 (eBook)


DOI 10.1007/978-1-349-06957-6
Contents

Pre f a c e ix

Cha pt er The Z80 a rch i te c tu re

1.1 Mic ro p r o ce s sor sy s t e ms 1


1. 2 Th e Z80 c e ntra l p roc ess i ng uni t 2
1.3 Memo ry 3
1.4 Ins tructions 4
1.5 As se mbly l a ngua ge 4

Chapt er 2 Ac cumulat or and r egis t e r instructions 6

2.1 Load A r e gist er with a v a l ue 6


2. 2 Add a n d subtra ct a va l u e 6
2. 3 Ad d an d subtract a re gist er 7
2 .4 Load one r e g ister wi th a n o t he r 7
2. 5 Incremen t a nd de c rement a re g i ster 7
2 .6 Nega t e the accumu la tor 8
2. 7 Add res si ng mode s - immed i at e and e xt e nd ed 8
2 .8 Labels 9
2.9 Program 10

Cha pt er 3 Subr ou t ines a n d di spla y output 11

3.1 Subr o utine conc e p ts 11


3. 2 Th e CALL a nd RET i ns t r uc tion s 12
3.3 Disp la y ou t p ut 13
3. 4 Ps e ud o o pe rations 14
3.5 Progra m 15

Chap te r 4 Uncondi tiona l jumps a nd k ey board i n pu t 16

4. 1 Unco n d i t i ona l jumps 16


4.2 Keyboard input 17
4.3 Cha r a c t e r code s and values 18
4 .4 The EQU ps eudo o pe r a t o r 19
4 .5 Program 20

Cha pt er 5 Fla gs, con d i t iona l jump s and the CP instruct i on 21

5. 1 Th e f l ag r e g i st er 21
5 .2 Con di ti on al j ump i nstructions 22
5.3 The c o mpare in s t r u c t ion 23
5 .4 Con di t i onal loo p te r mi na tion 24
5. 5 Pro gram 25

v
vi Contents

Chapter 6 Counting loops and the stack 26

6.1 Counting loops 26


6.2 Number input 28
6.3 The stack 29
6.4 The stack instructions 30
6.5 Saving and restoring registers 30
6.6 Program 31

Chapter 7 Nested loops and addressing modes 33

7.1 Nested loops 33


7.2 Immediate extended and register indirect
addressing modes 33
7.3 The DEFM pseudo operator 36
7.4 Text output 37
7.5 The subroutine mechanism 37
7.6 Program 38

Chapter 8 Carry and overflow 40

8.1 Carry 40
8.2 The carry flag 40
8. 3 Over flow 41
8.4 The overflow flag 42
8 .5 Conditional CALLs and RETs 42
8.6 Program 43

Chapter 9 Bit operations and the index registers 45

9.1 The BIT test instruction 45


9.2 The SET and RES instructions 46
9.3 The DEFS pseudo operator 46
9.4 The index registers 47
9.5 Expressions 47
9.6 Jump tables 48
9.7 Program 50

Chapter 10 Shift instructions, multiply and divide 51

10.1 The SRL instruction 51


10.2 The SRA instruction 52
10.3 The SLA instruction 52
10.4 8-bit multiplication and division 53
10.5 Program 55

Chapter 11 Logical operations and macros 56

11.1 Logic operators 56


11.2 Logical i ns t r uc t i ons 56
11.3 Masking 57
11.4 Macros 57
11.5 Conditional pseudo operations 60
11.6 Program 62
Contents VII

Chapter 12 Rotate instructions and parity 63

12.1 Accumulator rotate instructions 63


12.2 Register and memory byte rotate instructions 65
12.3 Packing and unpacking 65
12.4 Parity 67
12.5 The parity flag 67
12.6 Program 68

Chapter 13 16-bit and multiple byte arithmetic 69

13.1 The DEFW pseudo operator 69


13.2 The 16-bit ADD, ADC and SBC instructions 70
13.3 Extended loops 72
13.4 Multiple byte arithmetic 74
13.5 Program 75

Chapter 14 Block transfer and search instructions 76

14.1 Block transfer instructions 76


14.2 Block search instructions 79
14.3 Program 81

Chapter 15 Decimal arithmetic 83

15.1 BCD representation 83


15.2 BCD arithmetic 84
15.3 The DAA instruction 86
15.4 The digit rotate instructions 87
15.5 Program 89

Chapter 16 Miscellaneous instructions 90

16.1 The NOP instruction 90


16.2 The auxiliary registers 90
16.3 Input and output instructions 91
16.4 Interrupt instructions 91

Appendix A Binary and hexadecimal number systems 93

A.l Binary and hexadecimal numbers 93


A.2 Binary and hexadecimal arithmetic 95
A.3 Decimal to hexadecimal conversion 95
A.4 Hexadecimal to decimal conversion 95
A.5 Binary-hexadecimal conversion 96
A.6 Decimal-binary conversions 97
A.7 Bytes 97
A.8 Signed (2's complement) numbers 97
Exercise answers 99

Appendix B Hexadecimal-decimal conversion tables 101


viii Contents

Appendix C Summary of 280 instructions 103

Table C.lSummary of flag operations 104


Table C.28-bit load group 105
Table C.316-bit load group 106
Table C.4Exchange group and block transfer 107
and search group
Table C.S 8-bit arithmetic and logical group 108
Table C.6 General purpose arithmetic and
CPU control groups 109
Table C.7 16-bit arithmetic group 110
Table C.8 Rotate and shift group 111
Table C.9 Bit set, reset and test group 112
Table C.IO Jump group 113
Table C.11 Call and return group 114
Table C.12 Input and output group 115

Appendix D Display and keyboard character codes 116

Appendix E Expression operators 117

Index 118
Preface

Many microcompute r systems, home and business o ne s , are based on


Zilog's Z80 microprocessor, one of the mos t commonly used
microprocessors.

All microcomputer systems have the facility to run programs


written i n one or more high-level languages such as BASIC,
PASCAL, COBOL, FORTRAN and FORTH. However, s ome t i me s it is
necessary (because of speed or memory restrictions) or des irable
to run programs written in the assembly language of a
microcomputer system. If the microcomputer s y s t e m is based on a
Z80 microprocessor then the Z80 assembly language must be used.

This book covers all of the facilities provided by the Z80


assembly language, starting with the simple fa cilities and
building on them step-by-step to gradually include the more
complex facilities.

Learning a programming language is like learning a foreign


language - you need plenty of practical e xperience. At the end of
each chapter there i s a practical program for you to do. Programs
from some of the chapters are used in later practicals so that
you can develop quite substantia l programs, rela tively easily.
The practicals have been carefully chosen to be i n t e r e s t i n g and
visually rewarding - all practicals except the first produce some
form of output on the displa y.

ACKNOWLEDGEMENTS

My thanks to Leicester Polytechnic for allowing me to use their


computer facilities for the development of programs and
production of the book, and to Mark Folker for his thorough
checking of the text and many suggested improvements. Also, I
wish to thank my wife, Susan, for her support in many ways,
particularly in the typing of the text using a word processor.

Roger Hutty

ix

You might also like