This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how pc is updated during execution of SWI and any simple instruction like mov R1,R15?

when swi instruction is executed at that time it is said that pc is not updated yet so it is pointing to instruction next to swi instruction for example

addr         code

0x0000     swi 100
0x0004    mov r1,r2
0x0008    mov r2,r3


so in this code when swi instruction is being executed at that time value of PC is 0x0004 so that value will be copied to R14_SVC while going to swi handler.means value in PC in not address of instruction being executed +8 but +4 as can be seen.

but when following code is run i.e

addr        code

0x0000   mov R1,R15
0x0004   mov r2,r1
0x0008   mov r4,r5

so when first instruction is being executed means value of R15 goes to R1 that is 0x0008 so how pc is updated in this case means pc pointing to instruction being executed + 8 instead of +4.

why so?

Parents
  • When you read the PC (R15) by way of a "MOV" instruction, you're right the program counter is 8 bytes ahead of the current instruction. That's simply an artefact from early Arm implementations with a 3-stage pipeline (Fetch, Decode, Execute). At each Fetch, the PC is incremented by 4 bytes. So, by the time you execute, you've passed +0 (the one you're executing), +4 (the one currently in Decode) and +8 (the one currently in Fetch).

    When you take an exception, as the 'SVC' instruction causes, R14_mode is updated with the preferred return address plus an offset, which is the implementation adjusting the address based on that legacy 3-stage pipeline - for SVC it subtracts 4 from the PC+8 value you'd normally see as a result of moving the PC into another GPR.

    Table G1-10 in the Arm v8 Architecture Reference Manual (issue B.a, which is the current at the time of writing) shows the offsets and the obvious implication that if you design an architecture around a very specific implementation behavior, just to save a few gates, that you'll be stuck with it for 25 years or more.. every 32-bit Arm processor and every 64-bit Arm processor executing in 32-bit state absolutely must maintain the illusion, even though it has many more stages, out of order and 4 or 5 issue per cycle behavior, or older software built around that illusion will just not work.

    Ta,

    Matt 

Reply
  • When you read the PC (R15) by way of a "MOV" instruction, you're right the program counter is 8 bytes ahead of the current instruction. That's simply an artefact from early Arm implementations with a 3-stage pipeline (Fetch, Decode, Execute). At each Fetch, the PC is incremented by 4 bytes. So, by the time you execute, you've passed +0 (the one you're executing), +4 (the one currently in Decode) and +8 (the one currently in Fetch).

    When you take an exception, as the 'SVC' instruction causes, R14_mode is updated with the preferred return address plus an offset, which is the implementation adjusting the address based on that legacy 3-stage pipeline - for SVC it subtracts 4 from the PC+8 value you'd normally see as a result of moving the PC into another GPR.

    Table G1-10 in the Arm v8 Architecture Reference Manual (issue B.a, which is the current at the time of writing) shows the offsets and the obvious implication that if you design an architecture around a very specific implementation behavior, just to save a few gates, that you'll be stuck with it for 25 years or more.. every 32-bit Arm processor and every 64-bit Arm processor executing in 32-bit state absolutely must maintain the illusion, even though it has many more stages, out of order and 4 or 5 issue per cycle behavior, or older software built around that illusion will just not work.

    Ta,

    Matt 

Children