0
votes

I'm working with the HCS08 CPU (used in Freescale MCUs) and I can't understand a Branch instruction. Here is the manual. The Program Counter in this architecture points to the next instruction to be fetched. The BSR instruction (page 237) will update the Program Counter register in the following way (before saving it into the stack and making the branch):

PC ← (PC) + $0002 Advance PC to return address

They say "The program counter is incremented by 2 from the opcode address (so it points to the opcode of the next instruction which will be the return address)". But why by 2 and not just by 1??? Suppose I have

  • line 1 instruction1
  • line 2 BSR
  • line 3 instruction2
  • line 4 instruction3

While the CPU fetches "instruction1" doesn't know that there will be a branch in the next line, so after the "instruction1" fetch the Program Counter points to the line 2. When the CPU fetches the BSR, it doesn't increment the Program Counter; it saves in the Stack the actual value of the Program Counter plus 2, which will be used by the return instruction at the end of the branch. But Program Counter + 2 points to line 4, not to line 3!!! Isn't it an error? Thank you for having read!

Bob

1

1 Answers

2
votes

You may be getting consused between the assembler location counter and the CPU program counter. BSR is a 2 byte instruction (AD + addr), so the PC needs to be advanced by 2.