|
Optron #29
20 июля 1999 |
|
Likbez - Assembler view from afar: What is a stack. Determination of the stack.

Assembler: a view from afar.
Continued.
Beginning at | | 20, 21, 24, 25, 28
{} Infarh, 1999
What is stack?
You've probably already heard this word. Try to find out what
lies behind them.
"Stack" refers to memory, when working with
which uses a principle called
"First in, last out (first in, last out)." This means that the
data (Words) are put on the stack in series
one by one, and read from it in reverse order. In this case,
access is possible only to the so-called "top of the stack,
ie to the last stick a word.
The stack is used for storing temporary data processor, such
as, inter alia, as the return address from subroutines.
In the Z80 processor for placing information in the stack
following registers SP, which saved address stack. Note
one important point: when writing the next word in the stack
the address of its vertices is shifted down (to the lowest
address), ie not increases and decreases. Thus, if the SP you
have written down the address # 6000, then the next word is
placed on the stack, be placed in memory at no # 6002, a
# 5FFE. Since the stack is addressed in most
microprocessor-based systems (but, of course not
rules without exceptions ...)
Determination of the stack
Of course, with the area allocated for
stack, you can work, as with any other
memory area. However, using special
command processor, designed for
work with the stack, you can greatly speed up and simplify the
program.
How to use the same stack?
First you need to take care of the records in
SP addresses the stack. Team here
look like this:
LD SP, (addr)
LD SP, IX
LD SP, IY
LD SP, HL
LD SP, nn
Impact on the flags are not provided. A
after one of the above action
SP will be the desired value. But if you
this is not enough, you can use
the following steps:
INC SP
DEC SP
Thus, you certainly can make the SP
the desired value. And if we talk about the impact on the
flags, there is nothing unusual here also occurs. All within
that types of commands that are used in the operation.
Working with stacks
In the simplest version of the stack can be
post information and, of course, to get her out.
Thus, placement. For this we use the command "PUSH" in the
following ways:
PUSH rr
PUSH ii
As we have taken, "rr" denotes a register-pair, and "ii" -
the index register IX or IY. When the processor encounters with
Severe need to execute the following, we first reduce the
address contained in the SP by two. Thereafter resulting
address will be forwarded to the contents of the register pair
specified after "PUSH". By this method we can maintain
on the stack data.
And how to remove them from there? Also very
simple. Team, the return "PUSH", written as "POP". Record it is
completely analogous to the above:
POP rr
POP ii
Run this command, the processor will
word, whose address is stored in a SP, and put it in the
register pair specified in the command, after which the
contents of SP will increased by two. For the future, let us
agree: if you find a phrase like "Put on the stack" or "remove
from the stack, it is understood and the natural correction of
the contents of SP. And another tip: when work with the stack
do not forget, in some sequence you put into it the data,
otherwise your program is at risk to become like Windows'95!
A few more teams to work with
stack:
EX (SP), HL
EX (SP), IX
EX (SP), IY
According to him there is an exchange between the contents
could be stack and the contents of HL or index register;
ADC HL, SP
ADD HL, SP
ADD IX, SP
ADD IY, SP
Applying them to SP can be used in some types of calculations.
And now - on flags. As "PUSH", "POP",
and "EX" no influence on the flags are not
have, but ... if you execute the command
"POP AF", then, of course, the flags will be already
far wrong, so do not forget about
this! As for the arithmetic
action, and there impact on the flags, too
quite common and consistent way
arithmetic operations.
Routines
To start define some terminology.
"Subprogram" refers to the main frament
program, which can be called from
any place thereof, to perform a specific task, after working a
control returns to main program
with the location where it was called subprogram. It is invoked
with the command "CALL", a record which may adopt
following forms:
CALL addr
CALL cc, addr
I hope you do not forget that the "cc" denotes the use of
terms. A conditions are allowed as follows:
C, M, NC, NZ, P, PE, PO, Z
As usual, the command "CALL"
not easy, but very simple. Met her
CPU puts the address on the stack commands
following the "CALL". After this control will be transferred to
the fragments of the program, which is located at the address
"addr". And so it happens a subroutine call. A
how to get back out of it? And that simple!
Subroutine must end with the team
"RET", which is met, the processor will remove
from the top of the stack word to use
it as a jump address. If you
do not stack nachudili, the word
will address, put a team there
"CALL".
Now do you understand the reference in one of
our first team training "RET"? The point
that when you run out programa
Assembler, he performs it as a subroutine, and, naturally,
expects on the correct exit.
And finally - on options for writing the command "RET":
RET
RET cc
That's all the complexity! Even the address is not necessary
point. The conditions are also possible.
They are similar to those that use the command "CALL".
Is that all? "?
And here and there! There is one team,
quite suitable to the theme of today's sessions. This so-called
"call to restart."
Restart - it's almost the same as
"CALL", but it allows you to make the call
Only eight procedures located in
the first 256 bytes of memory:
# 00, # 08, # 10, # 18, # 20, # 28, # 30, # 38
The advantage of the restart command (RST) is that it takes
only one bytes and works faster than the "CALL". A
drawback - that it can apply
only to refer to one of eight
above the cells. In all of the Spectrum
These addresses belong to the ROM, but if you
know what they're doing, you can use them in their programs.
Options for recording commands restart the following:
RST # 00
RST # 08
RST # 10
RST # 18
RST # 20
RST # 28
RST # 30
RST # 38
A small note
So, we figured out the stack.
Add a few words. Electronic newspaper "Polesie" also began to
publish notes, such as our "literacy classes." So,
if you are interested - contact and
this edition for more information.
That's all for today. Do not be afraid to experiment, because
"to learn how to glitch! Until See you later!
To be continued ...
Other articles:
Similar articles:
В этот день... 15 November