Читать книгу Microprocessor 4 - Philippe Darche - Страница 23
1.2.3.2. Relative addressing
ОглавлениеRelative addressing, implied in PC (Program Counter-relative addressing), makes it possible to access a memory location relating to the current position of the program counter that, we recall, contains the address of the next instruction to be executed (Figure 1.12) after the decoding stage. This mode is in fact an indexed mode using the PC (cf. § 1.2.3.4 on indexing). With the following formula, we see that the effective address of the data or instruction relates to the PC by a value of d:
[1.2]
This is the favored mode for jump instructions, whether conditional or not (PC-relative branch). The relative displacement d is expressed in a signed integer representation, which is always the complement to 2n (two's complement, cf. § II.2.5 from Darche (2000)). Depending on the size of the displacement, the extent of the jump will be limited to (-2n-1, 2n-1 -1), with n being the format of the address field. Depending on the value of n of the relative address, we will call it a short or long jump. When the processor uses segmentation (this will be covered in a future book by the author on memories), jumps can be made within a single segment (intrasegment jump) or between two segments (extra-segment jump).
Figure 1.12. Execution of an instruction in relative addressing
The example below (x86) is a negative jump. The hexadecimal value F9 represents -7 in base 10. This means that the processor will connect 5 bytes higher than the instruction address, the difference of two bytes arising from the fact that the PC has changed while the instruction was executed (incrementation of the size of this instruction, here, two bytes):
73 F9 jnc loop; PC ← PC + F916
Two particular cases should be cited: jmp 00
, which jumps to the following instruction since the program counter has been incremented during the decoding phase of the execution cycle (cf. § V1-3.2 and V1-3.3.2) to direct the following instruction and jump –n
, where n is the instruction format (in words) underway, which implements an infinite loop. This mode is linked to PC (PC with displacement or Program Counter with Displacement for MC68000). It can be seen as an indexed mode, the indexation register being the PC (cf. § 1.2.3.4).
This mode is useful for generating the independent code of implantation in memory (position-independent code). We also speak of a translatable code (relocatable code), a topic discussed in § 3.1.4. It is also at the root of implantation of classic control structures of high-level languages (if_<condition>_then_else, iterative structures (i.e. loops) such as while_<condition>_do, repeat_until_<condition>, for_<condition>_do, etc.) in assembly language.
This mode can even be used to address an operand (Figure 1.13). We cite x86 64-bit architectures with addressing called RIP (Instruction Pointer Register)- relative, ARMv8 with literal mode and MPU MC6809 with the program counterrelative mode.
Figure 1.13. Seeking an operand in relative addressing
This mode can be seen as an indirect mode auto-incrementation using the PC (Program Counter) as an indirection register (cf. § 1.2.3.3).