csc159 ch4 interrupt_stack

20
Topic 4 INTERRUPT AND STACK Introduction Peripheral devices demand the attention of microprocessor at various and predictable times during normal program execution. The best example of the random need for attention is the use of the keyboard on a computer. Every time a key is pressed, the microprocessor must deal with an activity. Other peripheral devices such as disk drives, CRT’s and printers also need to interact with the microprocessor. Just how the microprocessor accomplishes the task of working these devices is the subject of this device. 4.1 INTERRUPT Polling Technique/approach to taking care of peripheral devices. Microprocessor checking each device in rotation at frequent intervals to see if it need service. The computer time spent in polling is largely wasted. Not efficient when the processor needs to perform other tasks. Need better system that allow processor to be free to continue normal sequential execution and only stop to deal with a peripheral when it specially needed attention. So that, interrupt system has been design to satisfy the requirement for external input control and freeing the CPU from waiting for events to occur. 4.1.1 Concept hardware interrupt 1. What is interrupt? Interrupt is a call for the microprocessor to interact or service the interrupting unit. The interrupt will cause the computer to suspend the program being executed and jump into a special interrupt processing program. There are many circumstances under which it would be desirable to interrupt the normal flow of a program in the computer to react to special event. Example: User command from keyboard 1

Upload: mohd-faizrul

Post on 28-Nov-2014

155 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CSC159 Ch4 Interrupt_Stack

Topic 4

INTERRUPT AND STACK

Introduction

Peripheral devices demand the attention of microprocessor at various and predictable times during normal program execution. The best example of the random need for attention is the use of the keyboard on a computer. Every time a key is pressed, the microprocessor must deal with an activity. Other peripheral devices such as disk drives, CRT’s and printers also need to interact with the microprocessor. Just how the microprocessor accomplishes the task of working these devices is the subject of this device.

4.1 INTERRUPT

Polling

Technique/approach to taking care of peripheral devices. Microprocessor checking each device in rotation at frequent intervals to see if it need service. The computer time spent in polling is largely wasted. Not efficient when the processor needs to perform other tasks. Need better system that allow processor to be free to continue normal sequential execution and

only stop to deal with a peripheral when it specially needed attention. So that, interrupt system has been design to satisfy the requirement for external input control and

freeing the CPU from waiting for events to occur.

4.1.1 Concept hardware interrupt

1. What is interrupt?

Interrupt is a call for the microprocessor to interact or service the interrupting unit. The interrupt will cause the computer to suspend the program being executed and jump into a

special interrupt processing program. There are many circumstances under which it would be desirable to interrupt the normal flow of a

program in the computer to react to special event. Example:

User command from keyboard Command from other external input Abnormal situation – power failure Execution of an illegal instruction Completion signaling of an I/O task

2. Interrupt capability

Allow computer to take special actions when required. Used to time-share the CPU between several different programs at once. Satisfy the requirement for external input control. Provides the desirable feature of freeing the CPU from waiting for events to occur. Provides one or more special control lines to the central processor know as interrupt lines.

1

CPU

Printer

Disk DriveKeyboard

Mouse

IRQ

IRQ IRQ

IRQ

IRQ – Interrupt ReQuest

Page 2: CSC159 Ch4 Interrupt_Stack

- The messages sent to the computer on these IRQ lines also known as interrupt.

4.1.2 Interrupt service

The interrupt will cause the computer to suspend the program being executed and jump to a special interrupt processing program.

Interrupt handler program – action taken by the processor when an interrupt occurs. Also known as interrupt routine.

When interrupt occurs, the processor will then execute the interrupt routine called for. The process of determining the appropriate course of action by the interrupt handler program

(interrupt routine) is known as servicing the interrupt. There are 4 distinct steps that microprocessor takes after an interrupt.

1) The microprocessor finished the current instruction, until the end of an instruction cycle.

- The interrupt signal will not be acknowledge until the current instruction is carried out.

2) Normal operation is suspended.

i. All the pertinent information about the program being suspended is saved/preserved in a known part of memory. Either in a special area associated with the program (process control block) or in a part of memory known as stack area.

ii. Pertinent information including: Location of the last instruction executed. Values of data in various registers that contain pieces of information relate to

the algorithm being carried out when the interrupt occurred.

iii. The contents of the registers and the status of microprocessor in general must be preserved so that it can again resume operation when the interrupt has been serviced.

3) The microprocessor jumps to the location in memory where the interrupt service routine has been stored and executes the routine. The address of the routine may be fixed in the microprocessor design.

4) When interrupt routine complete its task, it would return control to the interrupted program.i. The processor returns from an interrupt. The return includes restoring the microprocessor

to its exact condition before the interrupt occurred.

ii. All registers were restored to their original values. The information they contained must be retrieved from memory and placed back in their respective registers.

iii. Finally, the program counter (PC) is loaded with retrieval address of instruction that would have been executed if an interrupt had not occurred.

iv. The original program would resume execution exactly where it left off.

2

Page 3: CSC159 Ch4 Interrupt_Stack

Figure 4.1: Servicing an interrupt

Figure 4.2: Sequence of event during interrupt

4.1.3 Usage of interrupt

The way in which an interrupt is used depends on the nature of the device. There are several different ways in which interrupt are used:

i) As an external event notifier.ii) As a completion signal.

iii) As a means of allocating CPU time.iv) As an abnormal event indicator.

i) The interrupts as an external event notifier

3

Normal Execution

Normal Execution Continues

Finish Current Instruction

Suspend OperationStore registers

Jump to Subroutine and Execute

Return to Normal OperationRestore Microprocessor Status

Contents of registers stored in stack

Contents of registers returned from stack

Interrupt

Page 4: CSC159 Ch4 Interrupt_Stack

Interrupt are useful as notifiers to the CPU of external events that require action. Frees the CPU from necessity of performing polling. Example: Keyboard input.

Figure 4.3 shows the steps in processing a keyboard input interrupt.

Figure 4.3: Using a keyboard handler interrupt

Keyboard input can be processed using a combination of programmed I/O and interrupt. Suppose a key is struck on the keyboard. This causes an interrupt to occur. The current program is suspended, and control is transferred to the keyboard interrupt handler program. The keyboard interrupt handler first inputs the character, using programmed I/O, and determines what character has been received. It would next determine if the input is one that requires special action. If so, it would perform the required action, for example, suspending the program or freezing the data on the screen. Otherwise, it would pass data to the program expecting input from that keyboard. Normally, the input character would be stored in a known memory location; ready for the program to use when it is reactivated.

When the action is complete, that is, when the interrupt has been serviced, the computer normally restores the register values and returns control to the suspended program, unless the interrupt specifies a different course of action. This program would be case, for example, if the user typed a command to suspend the program being run.

ii) The interrupt as a completion signal

Controlling the flow of data to an output device. Interrupt serves to notify the computer of completion of a particular course of action. Example: Printer Printer is a slow output device. The computer capable of outputting characters to the printer

much faster than the printer can handle them. Interrupt can be used to control the flow of characters to the printer in an efficient way. Interrupt capability prevents the loss of output and allows the printer to control the flow of

characters to a rate that printer can accept. Allows the CPU to perform other tasks while it waits for the printer to complete its printing.

Figure 4.4 shows this application.

4

Page 5: CSC159 Ch4 Interrupt_Stack

Figure 4.4: Using a print handler interrupt

The computer sends one or more characters at a time to the printer, depending on the type of printer. When the printer is ready to accept more characters, it sends an interrupt to the computer. This interrupt indicates that the printer has completed printing the characters previously received and is ready for more characters.

In this case, the interrupt capability prevents the loss of output, since it allows the printer to control the flow of characters to a rate that the printer can accept. Without the interrupt capability, it would be necessary to output characters at a very slow rate to assure that the computer did not exceed the ability of the printer to accept output. The use of an interrupt also allows the CPU to perform other tasks while it wait for the printer to complete its printing.

iii) The interrupt as a means of allocating CPU time

Interrupt is used as a method of allocating CPU time to different programs that are sharing the CPU.

The CPU can only execute one program at a time. Time share multiple programs implies that the computer system must share the CPU by allocating

small segments of time to each program, in rapid rotation among them. Each program is allowed to execute some instructions. After a certain period of time, that program is interrupted and relinquishes control to a dispatcher

program within the operating system (OS) that allocates the next block of time to another program.Figure 4.5 shows this application.

Figure 4.5: Using an interrupt for time sharing The computer system provides an internal clock that sends an interrupt periodically to the CPU. The time between interrupt pulses is known as a quantum.

5

Page 6: CSC159 Ch4 Interrupt_Stack

When the interrupt clock occurs, the interrupt routine returns control to the OS. OS determines which program will receive CPU time next.

This is effective method for allowing the OS to share CPU resources among several programs at once.

iv) The interrupt as an abnormal event indicator

Interrupt used to handle abnormal events that effect operation of the computer system. The events are directed at the problems within the computer system itself.

Example: Execution of an illegal instruction.i) Divide by zeroii) Nonexistent op codeiii) Hardware error detected

One obvious example of an external event requiring special computer action is power failure.

- Most computers provide enough internal power storage to save the work that being performed and to shut down gracefully, provided that the computer has quick notification of the power failure. A power line monitor that connects to the interrupt facility provides this capability.

- The interrupt routine will save the status of programs that are in memory, close open files, and perform other housekeeping operations that will allow the computer to restart without loss any data. It will then halt the computer.

Another important application is when a program attempt to execute an illegal instruction such as a divided by 0 or a nonexistent op code or when a hardware error is detected, such as parity error.

- When the error occurs it is not possible to complete the executing program.- System will attempt to recover from the error and that the appropriate personnel be notified.- Interrupt routine can notify the user of the error and return control of the CPU to the operating

system program. You should notice that these interrupt are actually generated from inside the CPU, whereas the other interrupt that we have discussed so far are generated externally.

- Internal interrupts are sometimes called traps or exceptions.

4.1.4 Multiple interrupt and prioritization

There are many different input and output devices and event indicators connected to interrupt lines.

Multiple interrupts will occur from time to time. Two different processing method used for determining which device initiated interrupt:

i) Vectored interrupt ii) Polling

Multiple interrupt can be handled by assigning priorities to each interrupt – handle top priority first.

A higher priority interrupt will be allowed to interrupt an interrupt of lower priority. Lower priority interrupt will have to wait until a higher priority interrupt is completed. This leads to hierarchy of interrupts, in which higher-priority interrupts can interrupt other

interrupts of lower priority, back and forth, eventually returning control to the original program that was running.

Figure 4.6 shows a simple example of this situation. In this figure, interrupt routine C is the highest priority, followed by B and A.

6

Page 7: CSC159 Ch4 Interrupt_Stack

Figure 4.6: Multiple interrupts Most computer system allows the system manager to establish priorities for various interrupt. There are three methods used to establish priorities:

i) Logical wayii) Hardware or softwareiii) Assigning a priority number to each interrupts.

i) Logical way Highest priority are reserved for time sensitive situation.

- Power failure, external event. Lower priority usually for task completion interrupts.

ii) Hardware or software

Highest priority devices are placed closest to the CPU. Connecting most critical devices to the interrupt pin with the highest priority. Used chips that perform a prioritizing function.

- All peripheral devices are connected to the interrupt controller chip.- Sends the interrupt on to the microprocessor along with the address of the interrupt

routine to execute.

Vectored Interrupt

Address of the interrupting device is included as a part of the interrupt. When the interrupt request occurs, the microprocessor will automatically load the program

counter with the assigned address. Assigned address contains the address of the routine to be executed. The interrupt will then check the assigned address to get the interrupt routine.

Example:Assume that assigned interrupt address is FFFAH and FFFBH

100010011002

7

Program Counter

1003

Hex Address

20 (Hi)

00 (Lo)

Interrupt 1 occurs

Page 8: CSC159 Ch4 Interrupt_Stack

10031004

200020012002

2030

FFFA 0 0 1 0 0 0 0 0FFFB 0 0 0 0 0 0 0 0FFFCFFFDFFFEFFFF

Figure 4.7(a): Program is interrupted while working at location 1003h

The microprocessor is executing the normal program sequence. On the instruction located at 1003, interrupt 1 is received. Interrupt caused the microprocessor to check assigned address. Vectored address FFFA and FFFB contains the address of interrupt routine. Address routine, 2000h loaded into program counter address register and routine is executed.

10001001100210031004

200020012002

2030

FFFA 0 0 1 0 0 0 0 0FFFB 0 0 0 0 0 0 0 0FFFCFFFDFFFEFFFF

Figure 4.7(b): Vectored interrupt has sent program to execute at new location Interrupt caused the microprocessor to check the assigned address for vector. Vectored address FFFA and FFFB contains the address of interrupt routine. Address routine, 2000h loaded into program counter address register and routine is executed.

8

Program Counter

2000

Hex Address

20 (Hi)

00 (Lo)

Vectored to new location

Program Counter

1004

Hex Address

20 (Hi)

00 (Lo)

Page 9: CSC159 Ch4 Interrupt_Stack

10001001100210031004

200020012002

2030 RETURN

FFFA 0 0 1 0 0 0 0 0FFFB 0 0 0 0 0 0 0 0FFFCFFFDFFFEFFFF

Figure 4.7(c): After the routine is finished at location 2030h, the program is returned from interrupt

At the end of subroutine, processor returns to the next sequential address in the normal routine.

Types of interrupt

Interrupt can be divided into TWO categories:i) Maskableii) Non-maskable

Maskable – interrupt that can be selectively disabled.- interrupt that not be accepted.

Non-maskable - interrupt that never disabled.- interrupt that will always be acknowledged and accepted.- example: power failure.

Most interrupt can be temporary disable by program instruction when a program is performing a critical task that would be negatively affected if an interrupt were to occur.

These interrupt can then be disabled or masked, so the microprocessor does not accept them. The various interrupts can be masked by assigning each interrupt possible a bit position in an

interrupt mask register.

4.1.5 Software interrupt

Behave much like hardware interrupt, except that software interrupt are written into the main program and executed in the course of sequential operation.

Similar to a known fixed location. Often used to jump to other programs in memory.

9

Page 10: CSC159 Ch4 Interrupt_Stack

Software interrupt are vectored interrupt if the processor has such instruction. The instruction will send the processor to create the assigned address for vectored location of the

interrupt service routine. Many interrupt use fixed locations for vector address. These locations in memory are specified by

manufacturer of the microprocessor chip. Software interrupt make the interrupt routine available for use by other programs. Program can access these routine simply be executing the INT instruction with appropriate

parameter. Application of software interrupt is to centralize I/O operations.

- Used by each program to request I/O from the operating system software.

4.2 STACK

During an interrupt routine, some or all of the microprocessor registers may be used. This would destroy data previously in the registers if their contents were not saved in some way. Obviously, to return the microprocessor to its pre-interrupt status requires a procedure to accomplish this.

What is stack?

The location where the contents of the registers in the microprocessor are temporary stored.

Hardware stack

A number of registers set aside within the processor to serve as the stack location. The advantage of hardware stack is rapid access and therefore, speed. But the size of the stack is limited by the number of registers that can be provided. Restrict the flexibility of a microprocessor.

Software stack

An area in RAM for temporary storage of data and registers contents. Building this stack is an inherent part of the interrupt signal. However, stack can also be built

independently of an interrupt request. In fact, stacks are used just about any time a subroutine is called.

The software stack is almost unlimited in size and can reside anywhere in memory. This implementation causes the need for a special register in the microprocessor called the stack

pointer register.

Stack pointer

Hold the address of the stack. Whenever a stack is required; the stack pointer register will track its location by holding the 16-bit

stack address.

4.2.1 Function of stacks – interrupt, subroutine

There are a lot of situations that need the used of interrupt:

i) When a subroutine is called.- Stack is an excellent method for storing the return address and arguments for subroutine calls.

10

Page 11: CSC159 Ch4 Interrupt_Stack

ii) When interrupt occurs.iii) When stack instruction occurs.iv) To store data when the most recently used data will also be the first need.v) Efficient way of storing intermediate data values during complex calculations.

4.2.2 Stack operation – push, pop

The stack address is built, one byte at a time, each entered or stacked on the last entry. It is analogous to the way plates are stack at a salad bar. Figure 4.8.

Figure 4.8 (A): Stack of plate illustrating First In Concept

Figure 4.8 (B): Stack of Plate Last Out Concept

The first plate put on the stack is at the bottom. The last plate off the stack is the last plate placed of the stack.

Stack operate by LIFO (Last In First Out) The last byte put on the stack will be the first byte retrieved from the stack, when an interrupt is

completed, for instance. Stack instructions use assembly language mnemonics like PUSH, to push byte onto the stack and

PULL or POP to take a byte off the stack. The stack builds downward.

- Directions of occupied address are moving toward lower address.

4.2.3 Stack building

Most stack operations are however automatic. That is, the entire process of saving the contents of registers is done automatically whenever a subroutine call is encountered.

The individual stack instructions, however do sometimes allow a choice as to which register to save.

For some operations, only those register whose content is important are saving in the stack – save valuable processor time.

Naming the registers to be saved is accomplished within the byte following a PUSH instruction.

Each bit position can be assigned to a register.

11

Plate stacked

First plate in

First plate in will be last plate out

Plate taken off stacked

Page 12: CSC159 Ch4 Interrupt_Stack

- If a bit is set, the register is saved.- If a bit position is clear, the register is not saved.

For example, if the byte XXX10011 follow the PUSH instruction.

BYTE

X X X 1 0 0 1 1

The D register, accumulator B and the flag register would be push or saved to the stack. This is because the bit corresponding to those registers is set. The others are clear, indicating that the registers they are represent will not be saved to stack.

4.2.4 Examples – multiple interrupts & subroutine

Subroutine Calls

A subroutine is an out-sequence part of the program that can be accessed by the jump to subroutine (JSR) or a call instruction.

The call or JSR instruction always saves the contents of the program counter.

Figure 4.10 shows the main program loaded in memory from location 0200H to 0350H.

As the program executes, it encounters a JSR instruction at location 0210H. The instruction occupies three bytes of memory, one for the instruction and two for the address of the subroutine to be executed. The next instruction that would have been executed is at location 0213H. The micro program knows that the JSR instruction requires 3 bytes and, therefore, places address 0213H on the stack.

The JSR instruction then cause the program counter address register to be loaded with the address of the subroutine. The top address of the stack is F00AH and will contain the high byte 02H and F009H will contain the low byte, 13H.

The microprocessor then begins executing instruction at location 1000H. At location 1015H, another subroutine is required, this time at location 2000H. The JSR instruction occupies 3 bytes as before. Again the, microprocessor places the address of the next instruction to be execute on the stack. Note that the top of the stack is now addressing F009h, where the last in address is stored.

Now the microprocessor begins executing the second subroutine. If another subroutine is required, it is also possible to call it. When subroutines call on other subroutine, the programs are said to be nested. One routine is nested in another.

The second routine finishes at location 2010H when the RETURN instruction is reached. The RETURN instruction does not need an address because the address to return to has stored on the stack. The return instruction POPs the address off the top of the stack and loads it into the register of program counter.

The address on the top of the stack, location F007h, is 1018H. This where the program was executing the first subroutine when the second call instruction was encountered.

12

Flag Register

Accumulator B

Accumulator APC Register

D Register

Page 13: CSC159 Ch4 Interrupt_Stack

The first routine then continues until the RETURN instruction is found. Now the top address in the stack is 0213H. This address is loaded into the program counter register. 0213H is the address of the next instruction in the main program.

Subroutines can be called at any time and as often as needed. One subroutine can call another and this routine can call a third, etc.

0000

0200

0210 Jump Instruction0211 10 Hi Byte0212 00 Lo Byte0213

0350Continue

Main

1000

1015 Jump Instruction1016 20 Hi Byte1017 00 Lo Byte10181019102A Return

2000 2nd Subroutine

2010 Return

F007 18 LoF008 10 HiF009 13 LoF00A 02 Hi

Figure 4.10: Sequence of Events and Memory Locations

13

Main Program

1st Subroutine

2nd Subroutine

Stack Storing Address to Return to

Return to Main

Return to 1st

JSR @ 1000

JSR @ 2000