in CO and Architecture retagged by
4,419 views
12 votes
12 votes
The most relevant addressing mode to write position-independent code

what is the meaning of postion - independent code
in CO and Architecture retagged by
4.4k views

2 Comments

yes, the answer is surely PC relative. Because in PC relative we just need the offset to reach any branch instruction and in all other addressing modes we need to fix the position of segments of code even when the code is relocatable, i.e. we need to fix the address so that our displacement stays correct. Whereas in PC-relative mode systems updates everything with respect to current relocation that took place, so even if in case the branch is relocated again then the offset will automatically get updated.

You can see here that relocatable code is eventually converted to absolute code https://stackoverflow.com/questions/46039781/what-is-the-use-of-relocatable-machine-code-which-is-generated-by-assembler-and

1
1

2 Answers

22 votes
22 votes
Best answer

Hope it helps!

selected by

4 Comments

@Manu Thakur

 Can u mention the source of this info? It will be really helpful.
0
0

@

Doesn't the PC's value updates automatically when relocated. And because of pre-calculated correct offset, it will eventually point to the intended address?

0
0
edited by

Based on Transfer of control, addressing modes are:

  • PC relative addressing mode: PC relative addressing mode is used to implement intra segment transfer of control, In this mode effective address is obtained by adding displacement to PC.
    EA= PC + Address field value
    PC= PC + Relative value.
  • Base register addressing mode: Base register addressing mode is used to implement the inter-segment transfer of control. In this mode effective address is obtained by adding base register value to address field value.
    EA= Base register + Address field value.
    PC= Base register + Relative value.
    

    Note:

    1. PC relative and based register both addressing modes are suitable for program relocation at runtime.
    2. Based register addressing mode is best suitable to write position-independent codes.
1
1
16 votes
16 votes
Position-independent code simply means that placing such a piece of code anywhere in the memory does not require additional address modifications. As suggested, PC-relative is the most relevant one. For example, consider base-register based mode wherein the address stored by base register depends on the memory it is loaded to, even though the offset mentioned in the code is position-independent.

1 comment

But contents of the program counter also depend on the memory location it is loaded to?! Then why prefer relative over base?
1
1

Related questions