5

While reading about Program counter i came to know to that The Program Counter is special in that there is no way to directly modify its value.

Is there any indirect way to access/modify the content of Program Counter?

Brooks Moses
  • 9,267
  • 2
  • 33
  • 57
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199

2 Answers2

12

You have to understand that if you modify the PC, the next instruction executed will be the one at the new PC address. That is simply an unconditional jump, and all processors have such an instruction.

Typically there is no LD PC,addr instruction, but that is exactly what JMP addr does, so it is not true that you cannot directly modify its value. You cannot however modify its value without modifying the execution path of the code - execution continues from the address specified.

In most cases it is possible to do it indirectly also, by for example setting the stack pointer to a location containing the new address and calling a RET return instruction.

Different processors and architectures may behave differently in a number of ways, and the instruction mnemonics I have suggested above are "generic" and not intended to refer to any specific instruction set.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • +1. You might add that the `call` instruction modifies it, as do conditional jumps. And you can do things like `jmp eax` (jump to the address pointed to by EAX) and `jmp [eax]` to jump to the address stored in memory pointed to by EAX. And the `int` instruction on the x86 is rather like a `call`. Lots of ways to modify the program counter. – Jim Mischel Dec 26 '11 at 17:07
  • 1
    @Jim: That is true, but an unconditional jump modifies the PC and does *nothing else*. Ultimately every instruction can modify the PC even if it were only to increment it. I was trying to be non architecture/instruction set specific, and not get into detail of addressing modes; my *addr* parameter was intended to represent any valid address representation or addressing mode including register indirection. Amit would have to specify an architecture of he wanted a more specific answer. – Clifford Dec 26 '11 at 22:37
4

Unconditional jump instruction directly modifies the value of the PC.

zvrba
  • 24,186
  • 3
  • 55
  • 65