What happens in an interrupt service routine?

前端 未结 4 741
庸人自扰
庸人自扰 2021-01-31 10:17

Can someone please explain to me what happens inside an interrupt service routine (although it depends upon specific routine, a general explanation is enough)? This always used

相关标签:
4条回答
  • 2021-01-31 10:39

    While the 8086 is executing a program an interrupt breaks the normal sequence of execution of instruction, divert its execution to some other program called interrupt service Routine (ISR). after executing, control return the back again to the main program.

    0 讨论(0)
  • 2021-01-31 10:48

    There is a good wikipedia page on interrupt handlers.

    "An interrupt handler, also known as an interrupt service routine (ISR), is a callback subroutine in an operating system or device driver whose execution is triggered by the reception of an interrupt. Interrupt handlers have a multitude of functions, which vary based on the reason the interrupt was generated and the speed at which the Interrupt Handler completes its task."

    Basically when a piece of hardware (a hardware interrupt) or some OS task (software interrupt) needs to run it triggers an interrupt. If these interrupts aren't masked (ignored) the OS will stop what it's doing and call some special code to handle this new event.

    One good example is reading from a hard drive. The drive is slow and you don't want your OS to wait for the data to come back; you want the OS to go and do other things. So you set up the system so that when the disk has the data requested, it raises an interrupt. In the interrupt service routine for the disk the CPU will take the data that is now ready and will return it to the requester.

    ISRs often need to happen quickly as the hardware can have a limited buffer, which will be overwritten by new data if it's now pulled off quickly enough. It's also important to have your ISR complete quickly as while the CPU is servicing one ISR other interrupts will be masked, which means if the CPU can't get to them quickly enough data can be lost.

    0 讨论(0)
  • 2021-01-31 10:51

    Minimal 16-bit example

    The best way to understand is to make some minimal examples yourself.

    First learn how to create a minimal bootloader OS and run it on QEMU and real hardware as I've explained here: https://stackoverflow.com/a/32483545/895245

    Now you can run in 16-bit real mode:

        movw $handler0, 0x00
        mov %cs, 0x02
        movw $handler1, 0x04
        mov %cs, 0x06
        int $0
        int $1
        hlt
    handler0:
        /* Do 0. */
        iret
    handler1:
        /* Do 1. */
        iret
    

    This would do in order:

    • Do 0.
    • Do 1.
    • hlt: stop executing

    Note how the processor looks for the first handler at address 0, and the second one at 4: that is a table of handlers called the IVT, and each entry has 4 bytes.

    Minimal example that does some IO to make handlers visible.

    Protected mode

    Modern operating systems run in the so called protected mode.

    The handling has more options in this mode, so it is more complex, but the spirit is the same.

    Minimal example

    See also

    Related question: What does "int 0x80" mean in assembly code?

    0 讨论(0)
  • 2021-01-31 10:56

    An interrupt is used to cause a temporary halt in the execution of program. Microprocessor responds to the interrupt service routine, which is short program or subroutine that instruct the microprocessor on how to handle the interrupt.

    0 讨论(0)
提交回复
热议问题