ADDITIONAL ADDRESSING MODES
CHAPTER 2 IMPLEMENTING ADDITIONAL INSTRUCTIONSANDADDRESSING MODES 131
• Transfer control to the address stored at the address in memory locations NXTPGM and NXTPGM+l. Then increment those locations by 2.
LD HL, (NXTPGM)
,FETCH STARTING ADDRESS ,COMPLETE AUTOPOSTINCREMENT
,TRANSFER CONTROL TO START ADDRESS Here NXTPGM and NXTPGM+ 1 point to the starting address ofthe next routine the processor is to execute. Initially, NXTPGM and NXTPGM+ 1 would contain BASE, the starting address of a table of routines. A typical table would be
BASE: DW Autopredecrementing. In autopredecrementing, the address register is decre-mented automatically before it is used. Autopredecrementing can be provided on the Z80 processor by decrementing a register pair before using it as an address. Note that the processor autopredecrements the stack pointer when it executes PUSH and CALL.
Examples
Load the accumulator using autopredecrementing on register pair HL.
DEC HL ;AUTOPREDECREMENT HL
LD A, (HL) ;FETCH DATA
Store the accumulator using autopredecrementing on register pair DE.
DEC DE
LD (DE),A ;AUTOPREDECREMENT DE ,STORE DATA
Load register pair DE starting at the address two smaller than the contents of HL.
DEC HL
Autodecrementing by 2 is essential in handling arrays of addresses or 16-bit data items. Note that predecrementing is generally simpler and more natural than postdecrementing .
. Store the accumulator using autopredecrementing on memory locations ADDR and ADDR+l.
132
Z80 ASSEMBLY LANGUAGE SUBROUTINES LD HL. (ADDR)DEC HL LD (HU,A
LD (ADDR),HL
,AUTOPREINCREMENT INDIRECT ADDRESS
;STORE DATA
;UPDATE INDIRECT ADDRESS
Autodecrementing can be combined with indirection. Here memory locations ADDR and ADDR+ I could point to the last occupied location in a stack.
. Transfer control to the address stored at an address two smaller than the contents of memory locations NXTPGM and NXTPGM+ 1.
LD HL.(NXTPGM) DEC HL
LD 0, (HU
DEC HL LD E. (HU
LD (NXTPGM),HL EX DE,HL
JP (HU
;FETCH STARTING ADDRESS
;STORE AUTOPREDECREMENTED POINTER
;TRANSFER CONTROL TO START ADDRESS Here NXTPGM and NXTPGM+ 1 point to the starting address of the most recently executed routine in a list. Initially, NXTPGM and NXTPGM+ 1 would contain FINAL+2, where FINAL is the address of the last entry in a table of routines. A typical table would be
FINAL:
OW ROUTO OW ROUT!
OW ROUTL
;STARTINO ADDRESS FOR ROUTINE 0
;STARTINO ADDRESS FOR ROUTINE 1
;STARTINO ADDRESS FOR LAST ROUTINE Here we work through the table backward. This approach is useful in evaluating mathematical formulas entered from a keyboard. If, for example, the computer must evaluate the expression
Z
=
LN (A x SIN (B x EXP(C x V»~)it must work backward. That is, the order of operations is Calculate C x Y
Calculate EXP (C x Y) Calculate B x EXP(C x Y) Calculate SIN (B x EXP(C x Y)) Calculate A x SIN (B x EXP(C x Y)) Calculate LN(A x SIN(B x EXP(C x Y))) .
Working backward is convenient when the computer cannot start a task until it has received an entire line or command. It must then work back to the beginning.
CHAPTER 2. IMPLEMENTING ADDITIONAL INSTI<UCTIONSAND ADDRESSING MODES
133
· Autopostdecrementing. In autopostdecrementing, the address register is decre-mented automatically after it is used. Autopostdecrementing can be impledecre-mented on the Z80 by decrementing a register pair after using it as an address.
Examples
Load the accumulator using autopostdecrementing on register pair HL.
LD A, (HU DEC HL
;FETCH DATA
; AUTOF'OSTDECREMENT HL
Store the accumulator using autopostdecrementing on register pair DE.
LD (DE), A DEC DE
;STORE DATA
;AUTOPOSTDECREMENT DE
Load register pair DE starting at the address in HL. Afterward, decrement HL by 2.
INC HL ; FETCH MSB
LD D, (HU
DEC HL ; FETCH LSB
LD E, (HU
DEC HL ; AUTOPOSTDECREMENT HL BY 2 DEC HL
Autodecrementing by 2 is essential in handling arrays of addresses or 16-bit data items.
· Store the accumulator using autopostdecrementing on memory locations ADDR and ADDR+1.
LD HL,(ADDR) LD (HU,A DEC HL
LD (ADDR.l,HL
;FETCH INDIRECT ADDRESS
;STORE DATA
;AUTOPOSTDECREMENT INDIRECT ADDRESS Autopostdecrementing can be combined with indirection. Here memory locations ADDR and ADDR+ I could point to the next empty location in a buffer.
· Transfer control to the address stored at the address in memory locations NXTPGM and NXTPGM + 1. Then decrement those locations by 2.
LD HL,(NXTPGM) ;FETCH POINTER
INC HL ;FETCH STARTING ADDRESS LD D, (HU
DEC HL LD E, (HU
DEC HL ;AUTOPOSTDECREMENT POINTER DEC HL
LD (NXTPGM),HL
EX DE, HL ; ,JUMP TO START! NG ADDRESS ,JP (HU
Here NXTPGM and NXTPGM+ I point to the starting address of the next routine
134
Z80 ASSEMBLY LANGUAGE SUBROUTINESthe processor is to execute. Initially, NXTPGM and NXTPGM+ 1 contain FINAL, the address of the last entry in a table of routines. A typical table would be
FINAL:
DW DW
DW
ROUTO ROUT!
ROUTL
;STARTING ADDRESS OF ROUTINE 0
;STARTING ADDRESS OF ROUTINE!
;STARTING ADDRESS OF LAST ROUTINE Here the computer works through the table backward. This approach is useful in interpreting commands entered in the normalleft-to-right manner from a keyboard.
For example, assume that the operator of a process controller enters the command SETTEMP(POSITION 2)= MEAN(TEMP(POSITION 1), TEMP(POSITION 3)).
The controller program must execute the command working right-to-Ieft and starting from inside the inner parentheses as follows:
1. Determine the index corresponding to POSITION 1.
2. Obtain TEMP(POSITION 1) from a table of temperature readings.
3. Determine the index corresponding to POSITION 3.
4. Obtain TEMP(POSITION 3) from a table of temperature readings.
5. Evaluate MEAN(TEMP(POSITION 1), TEMP(POSITION 3)) by executing the MEAN program with the two entries as data.
6. Determine the index corresponding to POSITION 2.
7. Execute the SET function, which presumably involves setting controls and parameters to achieve the desired value of TEMP (POSITION 2).
The operator enters the command working left to right and from outer parentheses to inner parentheses. The computer, on the other hand, must execute it inside out (starting from the inner parentheses) and right to left. Autodecrementing is obviously a handy way to implement this reversal.
. Indirect preindexed addressing (preindexing). In preindexing, the processor must first calculate an indexed address and then use that address indirectly. Since the indexed table must consist of 2-byte indirect addresses, the indexing must involve a multiplication by 2.
Examples
. Load the accumulator using preindexing. The base address is in an index register and the index is a constant INDEX.
LD LD LD
L, (xy+2lonNDEX) H,(xy+2*INDEX+!) A, (HU
OBTAIN LSB OF ADDRESS OBTAIN MSB OF ADDRESS OBTAIN DATA INDIRECTLY