Im Magazin „BYTE" wurde in der Ausgabe Dezember 1977 der folgende Artikel abgedruckt.
Der Artikel beschreibt eine Methode, auch bei einer 8080 CPU relative Sprünge wie bei der Z80 durchzuführen.
|
|
Relative Addressing for the 8080
James P Gaskell
Griffon Industries
Austin Rd
Amherst NH 03031
|
An essential characteristic of any computer is the ability to branch as a result of a deeision.
These jumps, or branches, can be done with target addresses determined in a number of ways.
Examples include absolute (direct) jumps, indirect jumps, indexed jumps and relative jumps.
The instruction set for the 8080 processor includes explicit absolute jumps, both unconditional and conditional.
It also has one indexed jump (although it allows for no offset).
Unfortunately, the instruction set does not include any relative jump instructions, instructions which are necessary if position independent programming is to be accomplished.
However, a routine can be used that simulates the desired result, thus enbancing the 8080's usefulness to programmers.
A relative jump goes to a specified offset from the present address.
Instructions of this type allow jumps within a program to be independent of where the program is located in memory.
That is, if the whole (object) program is moved to a new area of memory, the jumps are still valid.
It is not necessary to reassemble or explicitly relocate a program every time it is moved around in memory.
In order to implement this routine, three steps have to be taken.
First, a short program must be entered into memory.
Second, a section of memory must be set to a constant.
Third, some macroinstructions must be added to the 8080 assembler.
(Macroinstructions are assembly language instructions that are expanded into a sequence of machine language instructions during assembly.)
After these steps are taken, relative jumps can be written in a straightforward fashion in assembly language.
First, the program shown in
listing 1 must be entered into memory.
It is important that this program start at hexadecimal address
0038
, because it will be entered during execution via a
Re
STart 7 instruction.
Also, since
TEMP1
is used for data storage, it is necessary that
TEMP1
be located in programmable memory and not in read only memory.
Listing 1:
A programm used to perform relative jumps on the 8080 processor.
A block of memory must also be set aside for the RST 7 instruction, and the macroinstructions listed in table 1 must be added to the assembier to allow this program to work.
[8080 Quelle] |
|
Second, a block of memory must be set to the RST 7
instruction.
The larger the block, the greater the range of the relative jump instruction.
An address near the middle of this area should be chosen as the BIAS
address.
The machine language code for the RST 7
instruction is hexadecimal FF
.
This fortuitous coding permits an easy way to meet the present requirement.
Select an area of (logical) memory where no hardware memory exists.
If the data bus has pull up resistors, doing a memory read to these locations results in reading hexadecimal FF
.
Third, macroinstructions should be written for the assembler so that it will do the computations necessary to implement the relative jump.
Since relative jumps can be done either unconditionally or based on either state of any of the four flags, this requires that nine macroinstructions be written.
Table 1 outlines what has to be done.
If your assembler cannot handle macroinstructions, or if you do not have an assembler, you must enter the proper
CALL
instruction as shown in the table.
Macro Number |
Macroinstruction |
Assembled Instruction |
1
2-9 |
JR LABEL
JR (FLAG) LABEL |
CALL (LABEL - $ + BIAS)
C (FLAG) (LABEL - $ + BIAS) |
Note 1: $ means the location of the current instruction.
Note 2: (FLAG) means any of the eight condition flags,
ie; NZ, Z, NC, C, PO, PE, P or M. |
|
Table 1: The nine macroinstructions which enable the assembler to perform relative jumps on the 8080 processor.
|
To help in understanding the theory of operation, refer to
figure 1.
Since we wish to jump from
HERE
to
THERE
, we insert
JR THERE
(=
CALL (THERE - HERE + BIAS)
) at
HERE
.
This instruction pushes (
HERE + 3
) onto the stack and jumps to the location
(THERE - HERE + BIAS)
.
At this new location we have made sure that the contents are equal to hexadecimal
FF
(=
RST 7
).
This instruction pushes
(THERE - HERE + BIAS + 1)
onto the stack and jumps to hexadecimal
0038
.
At
0038
, we find our program.
This program first saves the contents of selected registers.
It then pops the top two words off the stack and adds them together to give (THERE + BIAS + 4)
.
After (BIAS + 4)
is subtracted off, we are left with THERE
.
This address is pushed onto the stack and the registers are restored to their original conditions.
The final RETurn does a jump to THERE
.
•
•
•
Instr
HERE: JR THERE ; CALL (THERE - HERE + BIAS)
Instr |
N locations
Instr ↓
THERE: Instr ; Address = (HERE + N)
Instr
•
•
•
•
RST 7
BIAS: RST 7 ; Contents = FF
RST 7 |
N locations
RST 7 ↓
RST 7 ; Address = (THERE - HERE + BIAS)
RST 7
•
•
•
|
|
Figure 1: An illustration of relative addressing as it is performed on the 8080 processor.
The program jumps from HERE to THERE.
|
This routine uses four bytes in the stack and takes 78.5 μs (with a 2 MHz clock, ignoring memory wait times) to execute.
For comparison, the absolute jump uses no stack locations and executes in 5.0 μs.
This routine returns all registers in their original states (except, of course, PC
).
After the overhead is established, each relative jump requires three bytes, which is the same as the absolute jump.
This means that switching between absolute and relative jumps does not affect the length of the object code.
As requirements change (basically, relocatability versus execution speed) it should not be difficult to alter the coding.
In summary, the ability to do relative jumps can be added to an 8080 based system.
The working program requires only 21 bytes with perhaps another couple of hundred bytes located elsewhere, so the demand on memory space is quite low.
In a typical application of relative jumps, object programs are pulled off a mass storage unit, placed in any available memory, and immediately used.
This example shows some of the strength of this kind of jump and some of the reason for the experimenter to add this capability to his/her operating System.
Eingescanned von
Werner Cirsovius
Februar 2013
© BYTE Publications Inc.