quadrant wrote:So interrupts are only used when responding to IO devices.
They are used for a lot of things, but what those things are is dependent on the design of the CPU, and of the rest of the platform it is part of. That is to say, interrupts as used on an x86-based PC system will work and be used differently from ones on the 8080 you quoted earlier (and I am not entirely certain why you plucked that particular ISA out of the ether, unless you are into retrocomputing on CP/M systems - while the 8086 which is the basis of PCs is sort of a descendant of the 8080, it is not at all the same), or an ARM system such as most current maker-grade single-board computers (Raspberry Pi, Asus Tinker Board, Rock64, etc.), or even another type of x86 system such as an Udoo x86 (another type of single-board computer, using a system-on-chip version of the x86, meaning that it has the same sort of CPU - broadly speaking - as a PC, but the rest of the system isn't PC-compatible).
Mostly, interrupts are used for getting signals from something outside of the CPU (which includes peripherals such as keyboards, mice, USB hubs, PCIe cards, disk drives, etc. but also integrated motherboard components such as the memory controller and the programmable interval timer). However, many instruction set architectures (including x86) also use them for 'soft interrupts' or 'traps', which are used to make system calls. For example, on the PC (when running in real mode) with the default interrupt vectors found at boot time)
instruction INT 10h and
INT 13h are used to call most of the BIOS routines, and in MS-DOS,
INT 21h served for making most types of operating system calls. While the later 16-bit and 32-bit extensions to x86 ISA added things like
sysenter and call gates as a substitute the older soft interrupt system, and many other architectures have their own specialized instructions for system calls, these are basically specialized versions of the same basic mechanism (indeed, ARM ISA the instruction
svc is just a synonym for the software interrupt instruction,
swi, though
svc is the preferred mnemonic now).
Some (but not all) ISAs also use them for certain classes of CPU and/or bus exceptions.
quadrant wrote:For example on key press to store the value of the key pressed and set a 'keyIsPressed' flag as true. On the other hand programs continually poll this flag to know when a key has been pressed...?
You are really confusing different levels here. Interrupts are a hardware mechanism; 'events' are an abstraction created in some programming languages and/or libraries. At the hardware level (and often at the OS level), there are no such things as 'events'. Most interrupts do not lead to a program event, and not all program events are the result of an interrupt (and even those which are, come at a considerable remove from the interrupt that caused them).
I know you don't realize it, but trying to discuss events in JavaScript or something similar in terms of the underlying hardware mechanisms is like trying to analyze the artistic merits of the
Mona Lisa by studying its subatomic structure. Conversely, trying to understand interrupts by looking at JavaScript events is like trying to study the chemistry of photosynthesis by observing a forest from a hillside several miles away.
(Actually, it is worse than that. Portable, interpreted languages such as JavaScript aren't tied to a specific type of hardware, and generally have no way of introspecting on the hardware to anywhere near that degree, if at all. Indeed, since the interpreter itself is generally written in a portable compiled language such as C, using the language's standard libraries (which neatly wrap over the hardware details) rather than making system calls directly, it too is usually unaware of how the platform's interrupt mechanism works and how the OS uses it - or even if there is one at all, as there are some systems such as the Ceres workstation that don't use interrupts, though that's rare.
Your typical JavaScript interpreter does not even know what OS it is running under, never mind what hardware the OS is running on, though they usually have some way to ask for that information via a function call - but while that one function has to know to make a system call, it is usually in a separate DOM library which the interpreter is linked to at build time.)
It isn't any fault of yours, you simply haven't gotten that far in learning this stuff yet. It takes time, a lot of time. Don't worry, you'll get there if you keep at it.