首页 文章

返回中断处理程序后程序计数器的位置?

提问于
浏览
3

嗨我想知道当程序从中断服务程序返回时程序计数器在哪里?我知道当中断事件发生时,PC被推入堆栈 . 但是什么地址被推入堆栈的下一个或同一个(只执行一个)?当我们有

first instruction;
interrupt event here;
go inside the ISR;
exit ISR;
second instruction;

要么

first instruction;
interrupt event here;
go inside the ISR
exit ISR;
first instruction;

我的意思是,PC是指向ISR入口之前执行的指令,还是指向下一条指令(从ISR返回后)?我希望你明白这个主意 .

4 回答

  • 4

    由于在CPU处于精确固定状态之前无法处理中断,如果在指令中间产生中断,则在执行指令后将跳转到中断向量程序 .

    因此,当从中断过程返回时,PC将指向第一个之后的指令 .

    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 
    ...
    
  • 2

    执行指令时, program counter keeps the address of the next instruction to be executed . 发生中断时,处理器执行以下操作:

    • 暂停正在执行的程序的执行并保存其上下文 . This means it saves the address of the next instruction to be executed, i.e, the value of the program counter 等相关数据 .

    • 使用该中断处理程序例程的起始地址更新程序计数器 .

    当中断处理程序例程完成时,CPU可以在中断点恢复执行程序 .

    enter image description here

    指令 i 发生中断,完成后,用户程序从 i+1 指令恢复执行 .

  • 1

    中断的确切行为是特定于硬件的,但CPU只会等到 first_instruction 完成 . 之后,它会将CPU状态推入堆栈(或以其他方式保存)并启动ISR . 这意味着您的ISR将不会立即执行 - 存在微小的延迟,这可能成为硬实时应用程序中的问题 .

  • 0

    有两种类型的中断:a)软件中断 - 由于某些严重问题引起的,例如在程序执行指令(比如第i条指令)期间除以零(比如程序是2分数) .

    CPU如何处理?

    非常像Java中的异常 . 在这种情况下,立即处理中断请求(当前第i条指令未完成) . 当前PC值(指向第i个指令的地址)保存在某个位置 . 中断被处理并且在服务中断之后,它返回执行除法程序以完成第i个指令和其余指令 .

    b)硬件中断 - 当某些输入来自键盘(例如)或某些其他硬件时,处理器正在运行程序 .

    CPU如何处理?

    在这种情况下,CPU不会立即服务中断请求 . 它首先完成当前第i个指令的执行,在某个位置保存PC当前值(指向第i个指令的地址) . 然后它会监听该中断,完成它,然后再回到旧的程序指令i 1 .

相关问题