Page 41 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 41
Chapter 3
Interrupts and stacks
Interrupts are a hardware mechanism designed to let the computer respond effi-
ciently and promptly to asynchronous external events. The most common sources of
interrupts are events such as a byte being received by a serial port, a disk drive
finishing a sector write, a timer timing out, or some other event being completed.
An interrupt is like a computer’s doorbell. If we did not have a doorbell in our
house, we might find ourselves standing around the door for hours whenever we
were expecting company. In a computer this is done by looping, and if it is done
without releasing the processor it will absorb every cycle available. Essentially the
program is asking “Is it here yet, is it here yet, is it here yet?”
Looping in a program without allowing other tasks to run is a crime against nature! Even if
you expect the exit event to occur quickly, to put the program into a loop is to squander re-
sources. If the exit event does not occur, the computer will appear to be locked-up and will
not respond to the keyboard or other input.
Most modern processors have between 2 and 32 interrupt inputs. Most interrupt sys-
tems allow for expandability to larger numbers of inputs if needed. These inputs can
be masked (disabled) under software control, and they can be either edge or level
sensitive.
When an interrupt input is triggered, current program execution is stopped, and the
address at which the program was executing is automatically pushed onto the stack.
The stack is simply a block of reserved memory that is combined with a Stack Pointer
to produce a simple LIFO (last-in first-out) buffer (see Figure 3.1). An interrupt is a
hardware version of a subroutine call in software. It is just that simple.
When the return address has been pushed onto the stack, execution is restarted at
the interrupt’s preprogrammed interrupt vector address. When the interrupt routine is
finished, it executes an RTI (Return from Interrupt). An RTI is like a Return (from
subroutine), in that it pops the return address from the stack and resumes execution
of the interrupted program. The RTI usually has the additional effect of resetting any
interrupt latch.
In practice, the interrupt routine will save all of the CPU registers on the stack as
well as the return address saved by the hardware reaction to the interrupt. This is
necessary because the interrupt routine may need to use the registers for its own
calculations, and the interrupted program will need the data it was working on when
24

