📌  相关文章
📜  相对寻址方式和直接寻址方式的区别

📅  最后修改于: 2021-09-10 03:02:53             🧑  作者: Mango

先决条件 – 寻址模式
在指令周期过程的取指令操作中,寻址方式起着至关重要的作用。寻址模式是操作数在实际执行之前在指令字中寻址的方式。

1. 相对地址模式:
在这种模式下,操作数的有效地址(EA)是通过将 CPU 寄存器的内容和指令字的地址部分相加来计算的。有效地址是通过将位移(指令中给出的立即值)和寄存器值相加来计算的。指令的地址部分通常是一个有符号数,可以是正数也可以是负数。这样计算的有效地址是相对于下一条指令的地址的。

EA = CPU Register + Displacement

图 – PC 相对寻址模式
相对寻址模式可以进一步分为三种类型 –

  • PC(程序计数器)相对寻址模式。
  • 变址寄存器相对寻址模式或变址寻址模式。
  • 基址寄存器寻址模式。

2. 直接寻址模式:
在直接寻址模式下,操作数的有效地址等于指令的地址部分,即指令的地址部分表示包含操作数的内存位置。

例子 –
ADD R1, 4000 其中 4000 是该位置的有效地址。

图 –直接寻址模式

在这个例子中,内存位置 4000 包含操作数 100,它被添加到 R1 的内容中并存储在 R1 中。

相对寻址模式和直接寻址模式的区别:

S.NO. RELATIVE ADDRESSING MODE DIRECT ADDRESSING MODE
1. Effective address is calculated by adding the contents of the CPU Register with the address part of the instruction. The address for fetching the operand is already provided in the address part of the instruction. It is not calculated.
2. Relative mode has three types : PC Relative, Index Register Relative, Base Register Relative Addressing modes. Direct addressing mode has no types.
3. It results in shorter address field in the instruction format as the relative address can be specified with a small number of bits. It results in longer address field in the instruction format as it requires more number of bits to designate the entire memory address.
4. It is often used with branch type instructions since it directly updates the program counter. It is not used in branch type of instructions.
5. It is used for writing relocatable code, position independent code, i.e. for program relocation at run time. It is used to access static data.
6. It is used to implement array addressing, records. It is used to implement variables.
7. It is used to handle recursive procedures. It cannot handle recursive procedures.