6. Relative Addressing
Quite often a program only needs to jump a little bit in order to jump to the next instruction. Maybe just a few memory locations away from the current instruction.
A very efficient way of doing this is to just add a small offset to the current address in the program counter. (Remember that the program counter always points to the next instruction to be executed). This is called 'relative addressing'
Relative addressing means that the next instruction to be carried out is an offset number of locations away, relative to the address of the current instruction.
Consider this bit of pseudo-code
jump +3 if accumulator == 2 code executed if accumulator is NOT = 2 jmp +5 (unconditional relative jump to avoid the next line of code) acc: (code executed if accumulator is = 2) carryon:
In the code snippet above, the first line of code is checking to see if the accumulator has the value of 2 in it. If it is has, then the next instruction is 3 lines away. This is called a conditional jump and it is making use of relative addressing.
Another example of relative addressing can be seen in the jmp +5 instruction. This is telling the CPU to effectively avoid the next instruction and go straight to the 'carryon' point.
Challenge see if you can find out one extra fact on this topic that we haven't already told you
Click on this link: using relative addressing in assembly language
Copyright © www.teach-ict.com