|
Polesse #07
25 мая 1999 |
|
Assembler - Stack, mnemonics - CALL ...

ASSEMBLER FOR BEGINNERS
(C) THINKER
STACK This is a very important concept for the programmer, so
that the numbers thaw thoroughly, and I'll try to
explain how can understand. Suppose you need to perform some
procedure, with reg. 'A' there is used, then it will appear
after a certain number, but we need to after using the reg. 'A'
and reg. 'B' contained the same- values as before. What
should I do? You can save the battery (so-called reg. 'A',
remember?) In any cell memory, then our programm will look like
this:
LD A, 10, in 'A' enters the number 10
LD B, 25, a 'B' enters top 25
LD (65000), A; to a memory address 65,000 puts
; Value Reg. 'A'
LD A, B; in Reg. 'A' fills the reg. 'B'.
; (After all, in memory, we can only
speculate
C reg. A!)
LD (65001), A; in the cell's capacity to address 65,001
Suppose 'A'
CALL PROGR; go to the sub (equivalent to
; RANDOMIZE USR PROGR, where PROGR -
address)
LD A, (65001) record in the 'A' value from the memory cell
with
; Address 65001
LD B, A; record in the 'B' content, Reg. 'A'
LD A, (65000), now in reg. 'B' we have again the number 25,
and
, In ref. 'A' - 10
RET; Returns
But you could also use a stack. To do this, there are two
mnemonics: PUSH ss and POP ss. PUSH ss stored in the stack Reg.
pair ss, and POP ss - pulls. To imagine how everything
this works imagine yourself a glass, where the numbers are
falling. Glass - this is our stack, a number - this value Reg.
pairs. In the stack You can enter more than one reg. pair, in
addition, if we retain the three reg. pair, in order to get the
first we have to pull out the last two. That is, the principle
of "first in, last out "- the basic principle of the machine
stack.
Now we rewrite our program from using the stack:
Legend:
Stored in the stack Regis-LD A, 10 LD A, 10
spectra of a pair of 'AF', because we LD B, 25 LD B, 25
can save it only PUSH AF PUSH AF
pair. Here's what happens in the PUSH BC
stack. CALL PROGR
POP BC
AF POP AF
1 RET
1
Now stored in the stack 'BC': BC
PUSH BC 2
Execute a subroutine CALL PROGR, 2
take out of the stack 'BC': Jan. 1
POP BC
Take out of the stack 'AF': AF
POP AF 2
AF 2
January 1
1
Notice how it works
A principle of "LIFO" - "Last Input - First
Output ("last in - first
out ").
RET
CALL ss These mnemonics we've used before, but not
RET delved into the essence of the work. That's what happens
in
prodesse run these commands:
CALL - the stack is stored the address following the CALL
command, and then jumps to address ss. CALL command can be
conditional, and the condition is defined as the same as in the
JP (JR) (CALL Z, ss; CALL NZ, ss).
RET - extracted from the stack number and the transition to
this address. RET command can be conditional, with the condition
defined as the same as in the JP (JR) and CALL (RET Z; RET NZ).
From this it follows: in order to properly return from
routines, you must do before leaving the stack the way
it was when the CALL. For example, we need to put on
Reg. 'B' number "3", and using routines TEST, in Reg. 'C'
number, which is contained in the memory cell 23560, and the
reg. 'A' should remain unchanged:
TRUE: FALSE ----------------- :--------------
LD B, 3 | LD B, 3 | LD B, 3 |
CALL TEST | CALL TEST | CALL TEST |
RET | RET | RET |
TEST PUSH AF | TEST PUSH AF | TEST LD A, (23560) |
LD A, (23560) | LD A, (23560) | LD C, A |
LD C, A | LD C, A | POP AF |
POP AF | RET | RET |
RET ---------------------------------------
This example clearly shows: that the operation of the stack
make sense, put on a stack of values should be subsequently
from there to pick up. Ie PUSH each team must have a
corresponding mnemonic POP and vice versa. Notice carefully to
both examples, where wrong - not a PUSH or POP. Therefore,
these teams are often called paired.
INC s increment operator. Increases Reg. s or ref. pair
s on edinitsu.1 (sA, B, C, D, E, HL, BC, DE, and
still some
about which you know so far is optional).
DEC s Operation decrement. Reduces Reg. s or ref. pair s
per unit.
LDI is very useful mnemonics processor. After receiving
this command, the computer operates the following
algorithm rhythm:
1) recorded the number of memory address 'HL' in the storage
unit
address 'DE'
2) Increased 'HL' 1 [(HL) -> (DE)]
3) Increasing 'DE' 1 [HL +1 -> HL]
4) Reduced 'BC' on 1 [DE +1 -> DE]
5) If BC = 0, laying down-[BC-1 -> BC]
is a flag P / V, otherwise it
reset YES NO
[(F) P / V = 0] [(f) P / V = 1]
LDD Same as LDI, only in paragraphs 2) and 3) Reg.
couples do not uvelichieayutsya and decrease.
LDIR even more useful command - send a block of memory with
increasing addresses. Here is its algorithm:
1) recorded the number of memory address 'HL' in the storage
unit
address 'DE'.
2) Increased 'HL' on 1
3) Increasing 'DE' 1 [(HL) -> (DE)]
4) Reduced 'BC' on 1 [HL +1 -> HL]
5) If BC <> 0, follows the transition [DE +1 -> DE]
paragraph 1 [BC-1 -> BC]
NO
DA
In essence ----------------------------| LDIR matches | END
| Next programmke: |
| | We illustrate the practical
| LABEL LDI | application LDIR. For example, we
| JR PE, LABEL | want to display area
| RET | memory length of 6144 bytes,
----------------------------- Starting with 0:
LD DE, 0; LOCATION
LD HL, 16384; WHERE
LD BC, 6144, how many
LDIR
RET
Now we bring to your attention a little simple program from
the action of which you see on the screen after pressing the
keys <6> or <7>. Try to understand what she was doing, explain
each mnemonic, then rewrite it themselves, from memory, making
again.
IM 0; including polls the keyboard, which establishes
EI; Lebanese regime, enable interrupts.
LD HL, 5000
PUSH HL
OPROS LD A, (23560)
CP "6"
JR Z, LEFT
CP "7"
JR Z, RIGHT
CP ""
JR Z, EXIT
JR OPROS
EXIT POP HL
RET
LEFT POP HL
INC HL
PUSH HL
CALL PRINT
JR OPROS
RIGHT POP HL
DEC HL
PUSH HL
CALL PRINT
JR OPROS
PRINT LD DE, # 5800
LDIR
RET
________________________________________________________________
Well, Well, I think today is enough. See you next
room. If upon reviewing the material you have any questions,
please contact the Editor "Polesie" or find another way to
contact me. Until we meet again.
Other articles:
Similar articles:
В этот день... 16 November