|
Micro #21
08 июля 1999 |
|
Tutorial - Programming in machine code and assembly language (part one).

>
PERSONAL COMPUTER
"ZX-SPECTRUM"
Programming in machine code
and Assembler
Moscow 1993
The book is the most accessible edition of Programming in
machine code for widespread personal
computers "ZX-Spectrum" (system "Sinclair"). In an accessible
form for beginners consider a system command processor,
describes the built-in calculator, dismantled
numerous examples. The book contains interesting
recommendations for viewing native proprietary programs.
Designed for independent study.
ACKNOWLEDGEMENTS
The book you hold in your hands already
gained fame as the most
accessible tutorial for those who want to
overcome the psychological barrier and move from programming in
BASIC programming in machine code, or simply wants to
understand the machine code branded programs. The first edition
of this book we published two years ago, in 1990, and now in
the country already has thousands of fans household personal
computers such as ZX_Spectrum (system, "Sinclair"),
self-mastered the subtleties of computer programming on this
book. The first edition was published in three volumes. Now we
have combined all three volumes (tutorial, workshop and
directory) within a single book, and corrected the number of
significantly (Approximately 20%) completed the contents.
Major revisions have sections devoted to the description of the
system interrupt laptop, parsing the concept of streams and
canals, the concept of assembler directives and issues related
to standardization of Russification computers. As shown by the
first experience, these questions need to be strong
illumination.
"Inforkom" continues to work on a book about computers
Spectrum and compatible with them. At the moment, launched a
multi-volume publication on working with graphics Spectrum.
Despite the fact that each of these volumes is an independent
academic unit and can be used independently from others,
however, they can be considered a logical extension of this
edition is least because "the first steps in the native Z-80"
from natural causes become a basic reference book for our future
developments. "Inforkom" thanks all the readers of the first
edition, sent their feedback, comments and recommendations, and
particularly expressed personal gratitude to our correspondents
Bayanova KN for preparing sections I.5.18, II.4.4.7, III.5 and
Pashorinu VI for preparing the section II.5.5.
"Inforkom.
Moscow, August 1992.
INTRODUCTION
Conducted in early 1990 survey of our customers showed their
keen interest in the development of programming in machine code
for the Sinclair-compatible computers that have received a our
country is the most widely among PC-class household.
Z-80 processor, which assembled on the basis of
computers in this system has gained wide
popularity in the world due to its versatility, the
availability of a vast system of production and technology
teams to provide him with large volumes at relatively low price.
This processor is used not only in
multiple computers that are part
system, "Sinclair Spectrum, but in other computer systems.
Among them, computers family MSX ("YAMAHA", "SPEKTROVIDEO"
Toshiba, Panasonic, etc.), computers
Systems "Enterprise", "SHARP, many computers the company"
AMSTRAD ", etc. In principle, material in this book can be used
by 90% and those who work with computers these systems.
Currently, CPU Z-80 is very well documented in world
literature. The most fundamental works for programmers of any
level are the monograph Lance Leventhal (LANCE A. LEWENTHAL)
"Programming Assembler Z-80 "(" Z-1980 ASSEMBLY PROGRAMMING "),
as well as the" Procedures in Assembly Processor Z-80 "(" Z-80
ASSEMBLY LANGUAGE SUBROUTINES).
These are very good books, unfortunately,
very large (on p. 500-600) and translated into Russian, which
makes doubtful the possibility of their wide distribution here
in the coming years.
Numerous other books overseas
authors devoted to programming for
Sinclair-compatible computers in the machine
code are as weaknesses or
lack of systematic exposition,
or improved guidance of the material. In the first case they are
good assistant guide, and the second - a good tutorial, but for
those who been prepared in principle. Textbook is
for those who are self begins to understand from the outset,
they can be very limited.
In his book, bring to your attention, we have tried, as have
managed to combine pop, systematic and informative
presentation. Those who do not need to learn elementary
programming in machine code can go directly to the second part
of the "Workshop ...". Not to duplicate the background material,
which is necessary as those who work with
the first part, and those who work with
second, we took it apart, a third
part.
We highly recommend reading accompany
this book is an independent code review
proprietary programs with a
disassembliruyuschey programs such as MONITOR 16/48, MONS 3,
ULTIMON etc.
We must also advise you on our
periodical "ZX-REVIEW." On his
pages you can find almost anything
You may be required from the software and
Computer Information Management
system "Sinclair" and something that is associated with
hardware issues.
We may purchase complete sets of annual "ZX-Review" for 1991
and for 1992 year, carried out in book form.
Go out and releases of 1993. For the record
indicate that the volume of a complete annual
Kit "ZX-Revue" about 5 times greater than the volume of this
book.
Your suggestions, comments and suggestions
You can send us the address 121019,
Moscow, G-19, PO Box 16. You can always
choose from a list of literature issued by us that you would
like.
I. FIRST STEPS IN native
1. MOST - THE FIRST STEP ...
Many fans do not suffer serious
difficulties in learning BASIC. For this
rather relatively little practice.
But sooner or later they will come to the barrier
"Native". It is sad, but
some way in front of him and stopped.
This does not in any way connected with lack of desire or
ability, just a lot do not know where to start. If in BASIC
You can start with anything (for error
the computer itself to correct you), here you
Processor find yourself with one on one, and
such a trial and error method does not work.
In short, there is a psychological
barrier, which can be difficult to overcome in
alone. It is known that in order to
learn to program, you must take and
start programming. "Inforkom offers you the following
compromise approach - first under this chapter, we will, taking
"the bull the horns, "simply start programming, and
then devote the remainder of the book is a systematic
presentation of the material. Thus, let's write the first
program in machine code. First of all, we select for it memory
area. If you've read our book "Great opportunities for your
Spectrum, you know that for BASIC in-
computer memory reserved memory area
starting from the address pointed to by the system variable
PROG and ends with the address pointed to by the system
variable RAMTOP. Suppose you want to write the program into
machine code starting at address 30000. Give the command CLEAR
29999. This will install RAMTOP in 29999 and your program will
be protected from possible damage from BASIC. Even if you
give the command NEW, memory areas that are above RAMTOP, will
not be affected.
Now, let two straight team one by
other:
POKE 30000,0
POKE 30001,201
We have now recorded two numbers in the correct
us to address. They form a simple program. Run it with the
command RANDOMIZE USR 30000. Try it yourself ... You think that
nothing happened, but it not so. First, the processor addressed
at 30000 and found a number of 0, which denotes the machine
operation code NOP. Operation NOP (NO OPERATION - no surgery)
tells the processor that do nothing not necessary. During
0.0000014 sec. he really does not do anything, and then moves
on to the following address, where he finds
islo 201.
This command RET (RETURN - return). She
allows the processor to cease this
place the program in machine code and get back to where it is
called, ie in our case - in BASIC. This is the processor and
did about what you got the message BASIC "OK."
If all you read here, you
clear, then you already understand how programs are compiled
into machine code. You can certainly argue that the benefits of
such programs are not very many, but not now in
This is the essence. It is important that you understand that a
certain sequence of numbers can be sequence of commands to the
processor Z-80.
Unfortunately for us there is little evidence
simple sequence of numbers such as such as 0 and 201. Keep in
mind Codes all commands of the processor (and there are about
seven hundred) is not easy, but it is simplified by the fact
that is an intermediate language between the processor
and a man - Assembly language. Each
code has a mnemonic assembler. Mnemonic - a collection of
letters stands for the English words. For our example program
in Assembly as follows:
NOP
RET
Translating these mnemonics into machine code
You can also instruct your computer. For this
There are special programs that
also called assemblers. There are also opposing the program -
the disassembly. They conversely convert machine code to
mnemonic assembler.
And those programs and others quite a lot. Often they are
combined in packages. Widely distributed packages of firm
GENS3/MONS3 HISOFT and EDITAS / MONITOR 16/48 firm PICTURESQUE.
Here GENS3 and EDITAS - assemblers and MONS3, MONITOR 16,
MONITOR 48 - disassembler.
Now let us return to our first
program and try it a few develop to it still doing something.
CPU Z-80 has several registers, the which have names - "A",
"B", "C", etc. Each can contain one kakoelibo integer from 0 to
255 (ie one bytes).
There are dozens of commands of the processor,
that allow you to copy the contents of
registers from one to another, and perform communications with
the outside world, including and RAM.
For example, the assembly instructions LD B, A
(Machine code - 71) means "to download
And the contents of the register in the register ". LD -
is an abbreviation for LOAD (LOAD - LOAD).
Likewise, LD C, B (the machine code 72)
means "to load the register with the contents
register. You can load the registers and
integers. For example, LD A, N - means
"Get to the register and the integer N, where
N may be a number from 0 to 255. "Prior to this
all the teams were single byte. The same team - two-byte. First
comes the machine code - 62, followed by the number itself - N.
Thus, For example, LD A, 77 (get to the register A
number 77) will be: 62.77.
Here 62 - operation code - he said
the processor that needs to be done, and 77 - is
operand. We note here that there are operations and trehbaytnye
and four-byte. The first byte, as a rule - the operation code,
and following it - the operands. We're talking
"Normally" because there are some
operations, which code is written by two
Bytes / 1.
So, we are ready to make writing a program that will be to
throw any number from one register processor to another.
/ 1 BACKGROUND: CPU Z-80
developed on the basis of its predecessor, the processor 8080.
Most of the teams they are the same, but for the Z-80 was added
dozens of new commands. To distinguish them from commands 8080,
they begin with the prefix 203, 221, 237 or 253 (In hexadecimal
code, respectively: CB, DD, ED or FD). Therefore there
operation code which consists of two bytes followed by operands.
ASSEMBLER Machine Code
NOP 30000 0
LD A, 77 30001 62
30002 77
LD B, A 30003 1971
LD C, B 30004 72
RET 30005 201
Much more interesting would be to organize
exchange between the processor and the computer's memory.
Commands that allow you to do it, too much. For example, a team
of LD (NN), A. Its native Code - 50. It means: "Load a memory
location whose address is given double-byte number NN, the
contents of register A ". This team - trehbaytnaya. The first is
opcode (50), and after - the two-byte
operand NN, which specifies a desired address. For example, if
you need an address of 30008, then the operand NN will be: 56,
117, because 117 * 256 + 56 = 30008.
This two-byte operand is of an older
part and younger. Remember that double-byte numbers (usually
the address) is stored in the memory is always in reverse
order, ie significant byte first, and then Senior
/ 1.
Thus, the assembly instructions LD
(30,008), A, native is written as:
50, 56, 117.
There is also the opposing team, LD
A, (NN) - "Worth the register and the contents
memory, given the address of NN ". Her
code - 58, followed by two bytes,
pointing to the address: first, junior, and
then - Senior. Then LD A, (30007) can be written as follows:
58, 55, 117.
Now we can move the contents of memory cells from one to
another, copying it through the register and the processor.
/ 1 BACKGROUND: Our everyday
Experience teaches that the first are senior level, and then
junior. For example, among 567 First there are the hundreds
(5), then - dozens (6) and then the identity (7). In a computer
- on the contrary. This is due to the fact that first computers
were so small memory, that of the senior ranks it then
and did not go. They were a later addition, and they had to
take at first.
Now we can move the contents of memory cells from one to
another, copying it through the register and the processor.
Before surgery
30007 30008
N A. . . . . . . . -
P R O C E S S O R
After surgery
30007 30008
N N
ASSEMBLER Machine Code
LD A, (30007) 30000 1958
30001 55
30002 117
LD (30008), A 30003 1950
30004 56
30005 117
RET 30006 201
You can still somewhat complicate the task and
try to perform the addition of content
any two memory locations and send
the result for storage in any cell.
To perform arithmetic operations
such as the addition, the processor Z-80 well
has several teams. Consider the command
ADD A, B (its native code - 128). ADD = ADDITION = ADD.
ADD A, B means the following: "Add
contents of register B to the CPU register content A and the
result is to leave register a ".
As you can see, this team single-byte,
as all indicate that where take
what to do with it and where to send the result, it has been by
themselves.
Suppose you want to add the contents of cells 30013 and
30014, and the result put a cell 30015. Then the program
in assembly and in the native will be
have the form:
ASSEMBLER MACHINE CODE COMMENTARY
LD A, (30013) 30000 1958 Worth as a result
Giustra A number
30,001 in 1961 contained in
30002 117 30013 address.
LD B, A 30 003 71 Upload a solution
Giustra B contains
My Registry A
LD A, (30014) 30 004 1958 Download to re
Giustra A number
30,005 in 1962 contained in
30006 117 30014 address.
ADD A, B 30007 128 added to with
derzhimomu A with
derzhimoe registration
RA VI
LD (30015), A 30 008 50 Upload containing
30 009 63 zhimoe register A
30010 117 against 30,015.
NOP 30,011 0 No operation.
RET 30012 201 go back to
where this space
gram causing
las.
Try this program to work. Let
Do you want to add two numbers, say 50 and
70. First, allocate memory for the program in machine code.
10 CLEAR 29999
Now we introduce the program into memory,
starting at address 30000.
20 FOR i = 30000 TO 30012: READ q: POKE i, q:
NEXT i
1930 DATA 58, 61, 117, 71, 58, 62, 117, 128,
50, 63, 117, 0, 201
Write in a cell 30013 and 30014 are the two
numbers that we wish to add:
40 POKE 30013, 50: POKE 30014, 70
Enter the command to execute our program in machine code.
50 RANDOMIZE USR 30000
And finally, we start our BASIC program - RUN.
After a second run for it and
message appears, DC It seems to be
Nothing has changed, but if you check what is in the 30015
cell, then make sure that there is a result of the addition, ie
number 120.
PRINT PEEK 30015 will give you: 120 OK
Thus, we have already met with several
processor commands the Z-80. Now we have to stop this
superpopular exposition, since the system of commands that the
processor is quite extensive and in such an approach would take
her about a thousand pages. We hope that the psychological
You have already overcome the barrier and then we proceed to a
systematic and gradual Development of the instruction processor.
You seem to understand that every team in the native has its
own mnemonic assembler, in which several letters are encoded
operation. K Unfortunately, the literature is usually written
professionals for professionals and they do not bother with
translation into Russian mnemonic language, especially that of
the Z-80 books in Russian are almost no (there are several
departmental transfers). Therefore, "Inforkom" in his "Handbook
.." (Part 3) paid place a dictionary to translate the mnemonic
assembler normal English, but through He and the Russian
language.
In concluding this chapter, several general
comments. Different processors have different instruction sets.
Knowledge of the instruction processor Z-80 does not mean that
You will be able to understand the native other computers
collected at other processors, but exactly what their
development will you dozens of times faster.
Even for only one CPU Z-80
entity has not yet been standardized.
Many of the assembled program, for example,
EDITAS, ZEUS, GENS1 ... GENS3 and others have
deviations in the form of a record of mnemonics, but they
insignificant and always negotiated in
the accompanying instructions to the program.
Lyrics provided by Michael (MIHEICH)
10.07.1999
Other articles:
AD & D - The value of ability. |
Prohodilka - the passing game Magician land. |
Tutorial - Programming in machine code and assembly language (part one). |
Similar articles:
В этот день... 27 November