A short assembler lesson for lamers
Alone Coder
Preface.
Until recently, I believed that
there are no longer people in the world who do not
they just can’t write a computer game
ru, but also - scary to say! - can't from-
Separate the register from the port. My naive pre-
the situation was dispelled by a recent visit to Ryazan
Russian radio market in the Central Park of Culture and Culture.
- Do you have any programs for
Sinclair?” asked the young man, approaching
to the table in the center of the site.
The word already alarmed me:
maybe someone else doesn’t know, but the computer
The yuter on which you are reading this text is
called ZX Spectrum, from the Latin word,
meaning, it seems, . I came up
closer.
- Sorry, I'm a local coder. My name is
Dima Bystrov... - I began embarrassedly.
After talking, we exchanged addresses and
They said goodbye, promising to visit each other.
A couple of weeks later we met again;
each of my programs clearly surprised me
a new friend who you probably haven't seen
there has been new software for two years now. I sent him
basic utilities and wished soon
write something good.
Another two weeks passed... He returned my
disks and brought an old magazine in a glossy
cover.
The magazine was called "Home Computer";
it was, to be honest, a piss magazine
( - adjective from , ka-
which term we coders use instead
IBM PC). Heading most
interested me. This is a column of questions
journal readers and responses to them, data
by a certain Dr. Help. When I came across
I couldn't help but laugh at these questions:
.
I specifically note the next question.
Now you will understand what I was getting at
this conversation. Get ready...
.
The following is a description of how the computer
ter should be vacuumed and wiped with a cloth
coy. But the authors of the magazine did not
were able to understand: the reader BELIEVES THAT AP-
THE COMPUTER HARDWARE CAN BE DAMAGED
PRESSING THE WRONG BUTTON!
Dispel such mythsand help the weirdo
to many readers of computer magazines
feel like masters of your computer
Tera and this page should.
Chapter 1
about how to overcome fear and write
the simplest program in assembler
First, I will introduce a few basic
concepts we use and applications of which
You cannot avoid problems in the future.
CODER - a person who writes (does)
programs in machine codes.
MACHINE CODES - instructions for the Z80 processor
in a computer, reduced to the simplest calculations
fusion operations. Computer programs
codes are ALWAYS faster than those written in some
or other languages (BASIC, Pascal, C,...).
ASSEMBLY (ASM) - recording method M.K. on
paper (screen, disk,..) in the form of letters and numbers.
Also A. is a program that translates asm
in M.K.
LAMER is not a CODER. Also L. is the one who loves
bit spoil other people's programs.
HACKER is a good CODER who cheats
someone else's program for the better (for example,
inserts eternal life, a beautiful screensaver,
packs the game, rarely fixes bugs
in the program or adds new features to it
possibilities).
MEMORY - computer memory. In a more limited
in any sense - RAM (RAM, into it
You can still write it down).
BYTE - character/number from 0 to 255/number
from -128 to 127, with -128 corresponding
128, -127 corresponds to 129, etc. Of these
consists of memory.
BIT - one eighth of a byte. Contains mini-
amount of information:0 or1.(Despite the endless
exact repetitions of this meaningless phrase
in the literature, I hasten to inform you that the minimum
information is zero bits, and if you say
those that are not considered so, I will give more
example of less information: 0 with probability
1/3 or 1 with probability 2/3.) Numbered
in a byte from right to left from zero, and the sum
bits multiplied by twos to their powers
numbers will give the byte value.
The best assembler is ALASM.
It has 2 modes - command and editor
testing. The first one is installed after
downloads. Enter the second one - type EDIT
(button E) name. For example: EDIT AAA.
Immediately you will enter the newly minted technology
basic file AAA.H and you can change it.
Now you are sitting in front of the screen, and your head
For some reason, yours began to think poorly. B
in such cases I type the first and last
lines of the future program. Here's how they usually-
look exactly like this:
ORG 25000
RET
The first indicates the location in memory where the
children work program (from 24576 to 65535)
and is not a command to the processor (that is,
does not take up space). The last one is the exit from
program (or from a subroutine, which is essentially
do the same thing) to where they dared her
run(call).
ORG type commands are called directives
Assembler. Another example of a directive:
ENT
It is needed in order to know from what
places to launch your abundant ORGs
program, since only assemblers(MASM)
prefer the latter ORG, others (ALASM) -
first, and some refuse altogether
translate (assemble) a program without
ENT or ENT $.
Lines for inserting commands in the middle
well, the text is moved apart with the SS+W (<>) buttons,
and are shifted using the SS+Q (<=) buttons. First
it may seem inconvenient, but then you
you will find it a very good way.
Useful directive
DEFB "text",#3F,5,"a"
She puts it in that place of memory where
the following co-
program manda, any set of bytes (symbolic
fishing). The DEFW command is similar, but it is fussy
two bytes each and does not digest texts.More
more mysterious is the command DEFS 40,1,2,3 - which
It pushes bytes 1,2,3 into memory 40 times in a row.
The hash # indicates a hexadecimal number
#3F=3*16+F, where A,B,C,D,E,F respectively
are equal to 10,11,12,13,14,15.
But I digress. In order for our program to de-
Lala at least something, although we haven’t done anything yet
we know how to brazenly use someone else’s
program from ROM (there is a lot of interesting stuff there):
ORG 25000
CALL 3582
RET
Now we call the shift program
entire screen up to the familiar place (SCROLL UP
CHR$) in the same way as our program
called by the system (BASIC, ALASM, TR-DOS,...).
To start this plagiarism (hazing?)
and see how it works, let's get out of the re-
press edit mode to command mode. For
To do this, press EXTEND; a live line will appear
giving commandseditor. You can enter once
nal commands: B - beginning of the text, E - end,
S - search for something, X - replacement, and one more thing
I don’t remember what, but you need to press Q - Quit.
Now the program should be written down: SAVE.
After this we assemble: ASSEMBLE and launch
say: RUN. The screen moved up, hurray.
You can also call it from BASIC: QUIT
(output in BASIC), RANDOMIZE USR 25000.
Return to ALASM - RANDOMIZE USR 23600.
From the built-in debugger StS: DEBUG
(debugging),JUMP 25000. RANDOMIZE USR 23600.
Go to editing - EDIT.
To fully master the system, you need to
it read HELP's to ALASM and StS.Dalneya-
For the sake of compactness, this text assumes
that you already know how to handle them.
Chapter 2
a little about everything
The register is a small variable,
which is stored not in memory, but in the process
re. The register contains 1 byte. Register names:
A
B C
D E
H L
I have only listed the general registers.
values, a total of 24.6 registers last
collected in pairs, which are simply called
(reg pair,rp) and can
used as a two-byte number/address
(out of 160K memory, 64 are simultaneously addressed
Therefore, the byte address is transmitted in 2 bytes)
A register can be assigned a value with the command
LD E,100
or LD BC,40000
or LD A,H
but not LD HL,BC
Label is similar to the line number in
BASIC except that it is not neededplaced on each line. The label can be used
use in expressions instead of the corresponding
date. The label contains 7 characters and is
starts with a letter. Example:
ORG 25000
LD DE,TEXT
LD BC,TEND-TEXT
CALL #203C ;again program from ROM
RET
TEXT DEFB "TEXT is a label",13
TEND
Text is printed at address TEXT=25010
length TEND-TEXT=17 bytes. All these calculations
niya produces ALASM when translated.
If you do not want the program you-
was filled only in a row, can be used
go to label:
ORG 25000
LD BC,#50F0;B=#50=80,C=#F0=240
JP 8933
A dot appears on the screen. JP 8933 pe-
transfers control to the program in ROM, which,
naturally, it already contains RET, so you can
don't worry about returns. Transition
no more than 127 bytes forward/backward
called short or relative and called-
JR address is written.
When you need to do more than one thing
times in a row (perform a cycle), in particular, using
used by the DJNZ team address:
ORG 25000
LD B,80
CYCLE LD (BUF),BC;write;8933 spoils B
LD C,B
CALL 8933
LD BC,(BUF);in BC the old value
DJNZ CYCLE
RET
BUF DEFW 0
A diagonal is drawn.DJNZ subtracts from B
one and, if zero is not obtained, goes
at the specified address. It's easy to calculate that
so the loop will be executed exactly as many times as
how much was in B atentering it (i.e. 80).
The parentheses indicate what is meant
not a number, but a memory cell with this number.
LD (BUF),BC writes BC to address BUF.
Use cells with labels for storing
registers are inconvenient, so they were invented
commands: PUSH rp (push) and POP rp (push out)
no). The first one remembers rp in the memory area
called the stack, the second takes from there
current value in rp. Many meanings for
put on the stack, removed from there in reverse
ok. The address where
the RET command passes. Therefore the number PUSH
and POP must match. Example:
ORG 25000
LD B,80
CYCLE PUSH BC
LD A,80
SUB B
LD C,A
CALL 8933
POP B.C.
DJNZ CYCLE
RET
A reverse diagonal will be drawn. Team
SUB r belongs to the category of arithmetic
logical and subtracts register r from A. Other
These a-l commands:
ADD A,r;add r to A
ADD HL,rp;add rp to HL
ADC A,r;A=A+r+CY
ADC HL,rp;HL=HL+rp+CY
SBC A,r;A=A-r-CY
SBC HL,rp;HL=HL-rp-CY
DEC r/rp;decrease rp by 1
INC r/rp;increase rp by 1
AND r; bitwise A AND r is written to A.Ob-
all bits of A that correspond to
zero bits r.
OR r;bitwise A OR r is written to A. B
all bits of A are turned to one, corresponding to
bits of r were equal to 1.
XOR r; the same, but bitwise EXCLUSIVE OR.
B each bit of A is written 1 if the bits are with
this number in A and r weredifferent; otherwise 0.
CP r;comparing A with r
CPL;A=255-A.All bits are inverted(0 1)
NEG;A=-A.
DAA; BCD correction; production
add it immediately after addition/subtraction, if
Whether the numbers entered were binary decimal.
It is unlikely that it will be useful to you, because d-d numbers -
This is a method of writing two digits in a byte, and
d-d #34 is considered equal to 34.
A few words about what CY is.
called a carry flag (sign, bit)
and is set to 1 if the result is
exceeded 255 (65535 in the case of 2-byte
addition) or when subtracting more
numbers from a smaller number or in other special
military cases. The Z flag is also used,
which is equal to 1 if the result is a-l opera-
tion is zero, and zero in other cases. For
This is called the zero flag. All flags
are located in the flag register F, which includes
ste with register A is another (even-
I turn) r.p.AF, which, unfortunately, cannot
used in double-byte calculations.
Comparison is the same as subtraction, but the result is
it is not remembered, but only the corresponding
The flags change accordingly:
Z=1,if A=r;Z=0,if A<>r;
CY=1, if A,where cc is the condition:
Z - Z=1 Since there are also flags, that is
NZ - Z=0 and additional ones rarelyusing
C - CY=1 terms used in
NC - CY=0 long transition mand JP.
That is, if the condition is met, then
it is possible to make the transition, otherwise it won’t.
Chapter 3
some important practical tips
The program becomes yours only then
when you worked on its creation. By the way-
ty, you shouldn’t rejoice after writing either
niya of any program, if the last one is before you
was written by at least one person.
There are two ways to write a program:
ramm on acma: on paper with pen/pencil
followed by typing on a computer and not
mediocre sitting behind/in front of the screen.First
The good thing about this method is that you can write software
we are away from the computer and have bad low productivity
productivity and an increase in the number of errors;
the second circumvents the shortcomings of the first and loses
its advantages. It is advisable to own both.
Despite the fact that the Pentagon is 128th, po-
Please note that not every computer is a Pentagon.
Therefore, you should not use two
screens when one is enough, and also
eat all 71680 processor cycles or stan-
Issue the first interrupt command EI.
NEVER operate the on/off
by removing the drive to check for disk
ka or playing music through a drive!
In the domestic drive 5313 at the same time
the operation of the lowering mechanism is disrupted
disk drive and the drive CANNOT WRITE
INFORMATION!
Have copies of your important programson
your own and/or other people's disks. The latter sometimes
even more reliable. Don't have copies of junk.
Save the system track.
Save your time, e.g. don't look
the same TV shows twice, don't go
to discos and drinking parties. Remember: life is
person is one and short, but they will remember you
only if you manage to do a lot for it.
Learn to speak, type, walk and breathe
mother quickly.
Have patience; errors in programs
happen to the masters from CodeBusters, but they
know how to correct them => and you will learn. First
the structure of a complex program you will not be able to
retain in memory, but after a short time
regular computer work size
program typed at once will become unlimited
humble.
Don't make a program out of pieces!
Labels in programs are recommended to be
wash like this: name of the subroutine + letter /
number, for example: MOVTXT0 in p/p MOVTEXT;
READKYQ to READKEY; you can SYMN13 to SYM.
If someone tells you that a certain method
is always the best, don't believe it. For
Let's take memory movement as an example. Slow-
LDIR is indispensable in a text editor;
LDI is very useful for vertical shear
screen and lack of memory for shifting;
POP HL:LD (...),HL is used in shifts,
multicolors and mappings;POP rp...PUSH rp
good for speeding up mapping (see article in
next issue); LD rp,...PUSH rp hitherto
was used only for drawing mesh
check thoughincreases the width in one fell swoop
multicolor up to 24 CHR$ +allows withdrawal
screen in one interruption (see article).
Despite the fact that switching two
images provides a variety of graphic
modes, remember that this leads to flickering
screen, which may be suitable for a demo,
but not for the program that the user
actively uses. You might even remember:
Quod licet Iovi, non licet bovi, which means
“What Jupiter can do, the bull is not allowed to do.”
Do not use three screen switching
never, since this entails not
flickering, but the screen blinks.
Don't limit the complexity of your compositions
you programs, justifying yourself by saying that someone
is very smart or has more
free time than you have. Not the gods of the mountains
your skin is burning! I don't have any free time
time, but I wrote this journal!
To be continued...
Share your thoughts about the article