Assembler: a view from afar
Infarh, 1998
Continued. Beginning in | 20
So, dear readers, we extend our
classes. I hope you already got Assembler and read the
instructions to him. However, today it is not so important, but
when the theory of pull ...
But let's order. Now the subject of
our consideration -
Representation of numbers
You probably already accustomed to that in binary numbers
(within a byte) are only positive: 0 to 255. But in
Actually the value of a
number depends on how you interpret it. You can use even numbers
certain range of both negative.
You can choose to what extent
is your number: 0 ... 255 or -128 ... +127.
How should look like a negative
number? Obviously, it should give zero
result in adding to their positive equivalents. Therefore, to
obtain the number of opposite sign must:
1) Invert all the bits of numbers;
2) Increase it by one.
"Are there any other kinds of representations of numbers?" -
You ask.
Yes. This - not so often used, but nevertheless sometimes
useful - binary-decimal form. There are numbers from 0 to 99.
Byte is divided into nibbles (4 bits). Each of them takes the
values 0 ... 9. Calculations using this form
more complicated, but do not despair! In his
time it will be considered.
CPU registers
Now let's talk about what can be found
inside the Z80, as if there should dig deeper. In fact, there
are many interesting but while we need only amusing items,
commonly referred to as the Register. They are intended for the
same as ordinary, familiar from the Basic variables - namely,
to store information and hideous mockery of this.
Register designated by a letter in Latin
alphabet. For general-purpose registers
Z80 CPU uses the letters:
A B C D E H L
The most important is the A register called the
"accumulator". With him proizvodyatsya all arithmetic and
logical operations, it nevertheless remains their result.
Holds register one byte of information. But
because it is sometimes necessary to work with great values!
Output is: registers can be combined in pairs, respectively
increasing its capacity to one word (2 bytes). The union is far
from not arbitrarily, but in a very concrete form. Here are the
options register pairs:
BC DE HL
Principal register pair is HL.
She, like the battery is needed almost
in all operations. By the way, you probably
surprised that the "A" couples do not find it here? On
Indeed there is, but the register pair to
"A" - not a general purpose, so
we have to study it will not. Let me just say
that his name is - "F", and it forms a pair
"AF".
Still have official registers - "I" and "R".
They are not paired, and there
themselves.
There are still couples that the individual registers can not
be divided (or rather divided, but particularly perverse
methods, their mention too early). Here are their names:
IY IX
And then there is hexadecimal, not divisible into pairs,
registers:
SP PC
I guess it all? And here and there! There is still
alternate registers:
A 'F' B 'C' D 'E' H 'L'
They are, respectively, form pairs:
AF 'BC' DE 'HL'
Working with the general purpose registers
If we want to perform any actions on the registers, then
first we must learn to give them specific values. For this
purpose, use the command assembler mnemonics with "LD", which
stands for the English word Load (Download). I must say that
this team - Not only for the register, she is also to
memory cells can be useful. Here are the possible options for
its use:
LD r, r
LD r, (HL)
LD r, n
LD r, (ii + n)
LD (HL), r
LD (ii + n), r
LD (HL), n
LD (ii + d), n
LD A, (BC)
LD A, (DE)
LD A, (nn)
LD (BC), A
LD (DE), A
LD (nn), A
LD rr, nn
LD ii, nn
LD HL, (nn)
LD rr, (nn)
LD ii, (nn)
LD (nn), HL
LD (nn), rr
LD (nn), ii
LD I, A
LD R, A
LD A, I
LD A, R
Decipher the used abbreviations:
r = A, B, C, D, E, H, L
rr = BC, DE, HL, SP
ii = IX, IY
n = B [# 00 .. # FF]
nn = word [# 0000 .. # FFFF]
Knowing that can command "LD", remains
clarify the meaning of certain designs, for example, "(nn)" and
"rr". And this is - just bytes, the address which indicates the
word "nn" or the contents of the register pair "Rr".
Or here "(ii + n)". Too simple: address
byte is determined by adding the content
register "ii" and byte "n". This is useful when
a large number of variables, summarized in
table. In this case, rather Record
start address table in the "ii", and the specific
element can receive by asking the team bias. By the way, there
is one subtlety. This method is possible in addressing the
range of not (ii )...( ii +255), and (ii-128 )...( ii +127).
When using the boot as well
as well as other processor commands, remember
one rule: forward going in the direction of the second
parameter in the command first. In other words, the command "LD
A, 5 and "LD C, A" means that in case "A" placed the number 5,
then its contents are sent to the register "B".
Register "F" is called "flag". His
bits (flags) - each individually -
serve to indicate the results of operations,
executed by the processor.
Hexadecimal case "SP" on the top of the stack machine - Region
memory for storing certain information. But it is - another
story.
Register "PC" is the program counter. Numbers stored in it is
address command, operable at the moment.
Remember! At the initial stage of training, in
avoid the nightmare of glitches, hangs, and
others do not use for experiments registers SP, PC, HL '. In
the case of IY try not to change contents of the addressable
memory and the actual content.
Work with the alternate registers
These registers are included instead of the main
with the command assembler
EXX
Her mnemonic comes from the word "exchange" (exchange). When
you run exchanged
HL <=> HL '
DE <=> DE '
BC <=> BC '
To activate the alternative pair of AF '
serves as a team with mnemonics
EX AF, AF '
Actually, there is no possibility
know which set of registers is active in
moment. The processor also do not know -
he is a "purple". So consider
alternative that set, which is currently
currently not active.
Arithmetic
Zdes a Z80 opportunities are somewhat limited. It can produce
only addition and subtraction. Here are some options these
steps:
ADD A, r
ADD A, (HL)
ADD A, n
ADD A, (ii + n)
ADD HL, rr
ADD IX, ry
ADD IY, rx
SUB r
SUB (HL)
SUB n
SUB (ii + n)
ADD means the addition, and SUB - Subtraction. The result of
calculation is placed in the register, which the team comes
first. But not particularly try to remember it. If
computations are performed on single-byte
number, then the first will always be "A" (note: in this case
the command SUB does not require the first operand). And if the
operations are performed on the word, in the early to HL, IX or
IY.
Flags
Enumerate all the possible flags register
"F":
D7 D6 D5 D4 D3 D2 D1 D0
S Z * H * P / V N CY
Flag of the S (sign) is installed, if
performing arithmetic or logical
the operation negative result.
Zero flag Z (zero) is set if
as a result of an arithmetic or
logical operation obtain a zero result.
Flags H (half-carry) and N (negative) are used very rarely -
mainly in work with binary-decimal numbers.
Flag of the parity / overflow P / V (parity /
overflow) - the only one charged with the indication of two
conditions. Flag parity is set if the result of logical
operation in a byte was even number set to "1" bit.
Overflow Flag shows that after
arithmetic steps to change the sign
operand.
Carry flag CY (carry) said that
results of arithmetic operations a carry from the highest
level. For example, in the case: 253 + 5 = 258. Of digits
not enough to save such a result. Therefore, we actually get
the number 2 and set flag CY.
Also appertaining to add that not all CPU instructions affect
the flags, and also addition, some commands change only
some bits of the flag register.
Back to the arithmetic
And now, after learning about flags, you can go
to arithmetic operations, which takes into account
Transfer:
ADC A, r
ADC A, (HL)
ADC A, n
ADC A, (ii + n)
ADC HL, rr
SBC A, r
SBC A, (HL)
SBC A, n
SBC A, (ii + n)
SBC HL, rr
As an example, consider this: we must add to the "A" a
certain number. No problem, until the result can be described
by a single bit.
However, all the more difficult (though not much) in this
example, the situation:
A = 200
n = 57
Of course, 200 + 57 = 257. But in one
byte is not appropriate. Therefore, there will remain the
number 1, and set a flag CY. Like, until all is clear, and the
team used no doubts:
ADD A, 57
But, if the following command from the
of ADD, or SUB, then the contents will be CY
lost, so do not forget about the groups
ADC and SBC!
After reading all the above and
armed with the assembler of your choice, experiment - and you
will understand that This is something there! But do not
forget: any assembler program must end the command
RET
Go for it! And until we meet again.