Sunday 16 December 2012

Topic 3 subtopic 4: PC relative addressing



PC-relative addressing


In order to load a register from a "constant" stored in program memory which is short distance away from the current instruction, PC-relative addressing mode is used. It can be seen as a special case of the "base plus offset" addressing mode, which selects the program counter (PC) as the "base register". 

It usually used in conditional branches. PC in PC-relative addressing mode bring a meaning of "special purpose register, Program Counter", which address of next instruction to be fetched are stores inside. 
The offset is the number of instructions forward or backward that must be skipped to get to the instruction labeled Label. The offset value can be an interpreted label value or immediate value. 

Thus, 4*offset + PC+4 is the address of the instruction labeled with Label.
The effective address is the sum of the Program Counter and offset value in the instruction. The branch target is determine by effective address.

PC-relative addressing occurs in branch instructions, such as beq and bne. These instructions are I-type instructions, with the following format.




The immediate value is only 16 bits, which means we can't address a large range of memory.
 To implement loops or if-else statements, Branch instructions are used primarily.
When jump to a statement in an if-else or loop, we will  typically jump to a nearby instruction from the one we jumped from. It makes sense to jump relative to the PC, because the instruction we jumped from must have been the one at PC
For example, if you had a branch instruction at address 1000, then normally you'll jump somewhere near the branch instruction.


PC relative addressing in action


Below diagram shows how an effective address is generated when using PC relative addressing mode.






SOON BOON JIAN
B031210199

2 comments: