Where the program counter goes after returning the interrupt handler?

前端 未结 4 982
名媛妹妹
名媛妹妹 2020-12-18 11:59

Hi I am wondering where the program counter goes when the program returns from interrupt service routine? I know when the interrupt event occurs the PC is pushed into the st

相关标签:
4条回答
  • 2020-12-18 12:03

    Exact behaviour of interrupts are hardware-specific, but the CPU will simply wait until first_instruction is finished. After that, it will push CPU state onto stack (or save it in other way) and start ISR. That means that your ISR will not be executed immediately - there is a tiny delay, which may become an issue in hard realtime applications.

    0 讨论(0)
  • 2020-12-18 12:13

    When an instruction is executing then the program counter keeps the address of the next instruction to be executed. When an interrupt occurs, then processor do the following:

    • Suspends the execution of the program being executed and save its context. This means it saves the address of the next instruction to be executed, i.e, the value of the program counter and other relevant data.

    • Update the program counter with the starting address of that interrupt handler routine.

    When the interrupt handler routine is completed, CPU can resume execution of the program at the point of interruption.

    enter image description here

    Interrupt is occurred at instruction i and after completion user program resumes its execution from i+1 instruction.

    0 讨论(0)
  • 2020-12-18 12:19

    There are 2 types of interrupts : a) Software interrupt- caused due to some critical problems like division by zero during execution of an instruction (say i th instruction) in a program(say program is Divide 2 number).

    How is this handled by CPU?

    Very much like an exception in Java. In this case the interrupt request is immediately handled(current i th instruction is not finished). current PC value( pointing to address of i+1 th instruction) is saved in some location. Interrupt is processed and after serving interrupt,it returns for of execution division program to finish i+1 th instruction and rest of instructions.

    b) Hardware interrupt - processor is running a program when some input arrived from a keyboard (for example) or some other hardware.

    How is this handled by CPU?

    In this case CPU doesn't immediately serve interrupt request .It first completes the execution of current i th instruction, saves PC current value (pointing to address of i+1 th instruction) in some location . Then it listens to that interrupt, finish it and later comes back to old program instruction i+1 .

    0 讨论(0)
  • 2020-12-18 12:25

    Since an interrupt can't be handled until the CPU is in a precise fixed state, if the interrupt is raised in the middle of an instruction then the jump to the interrupt vector procedure will occur after the execution of the instruction.

    So when coming back from the interrupt procedure the PC will be pointing to the instruction after the first.

    first instruction fetch (PC is updated meanwhile)
    first instruction decode
    interrupt is raised
    first instruction execution
    ** now and only now the CPU checks for a possible interrupt **
    interrupt is present, pushing PC on stack and other things
    jump to interrupt address
    execution of interrupt
    return from interrupt (pop of PC and other things)
    second instruction fetch 
    ...
    
    0 讨论(0)
提交回复
热议问题