More on Interrupts. Interrupt service routines oDOS facilities to install ISRs oRestrictions on ISRs oCurrently running program should have no idea that.

Slides:



Advertisements
Similar presentations
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Advertisements

10-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL Subroutine and Interrupt.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
University of Tehran 1 Microprocessor System Design Interrupt Omid Fatemi
Azir ALIU 1 What is an assembly language?. Azir ALIU 2 Inside the CPU.
Chaining Interrupts. Short contents What does chaining interrupts mean? TSR programs Chaining an interrupt example program Reentrancy problems with DOS.
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
1 Hardware and Software Architecture Chapter 2 n The Intel Processor Architecture n History of PC Memory Usage (Real Mode)
CS2422 Assembly Language & System Programming November 2, 2006.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
8.7 Memory management Program E Program D System memory DOS INT 21, function 48H: Allocate Memory Specification: allocates a number of memory paragraphs.
Interrupt Processing Haibo Wang ECE Department
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
8.6 Multitasking User 1 PP User 2 User 3 User 4 User 1 User 2 User 3 User 4 User 1 User 2 User 3 User 4 User 1 User 2 User 3 User 4... time the time.
Introduction to Computer Engineering by Richard E. Haskell Interrupts Module M17.3 Sections 11.3, 14.1.
8.4 Instruction Execution Times TOBIN PROC FAR SUB AX,AX MOV DX,AX MOV CX,4 NEXTD: PUSH CX SUB BP,BP MOV CX,4 GETNUM: RCL BX,1 RCL BP,1 LOOP GETNUM.
Lecture 10 Interrupts, Part 2, putting it all together Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Lecture 11 Last notes on interrupts and exam review Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
ECE291 Computer Engineering II Lecture 13 Dr. Zbigniew Kalbarczyk University of Illinois at Urbana- Champaign.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
BIOS and DOS Programming in DOS INT 10 and 21H. Interrupts There are some extremely useful subroutines within BIOS or DOS that are available to the user.
Khaled A. Al-Utaibi  Interrupt-Driven I/O  Hardware Interrupts  Responding to Hardware Interrupts  INTR and NMI  Computing the.
Chapter 3 Examining Computer Memory and Executing Instructions.
ECE291 Lecture 9 Interrupts I. ECE 291 Lecture 9Slide 2 of 28 Lecture Outline Printed lab manual?Printed lab manual? Interrupt I/OInterrupt I/O Interrupt.
1 Microprocessor-based Systems Course 8 Design of input/output interfaces.
ECE291 Computer Engineering II Lecture 12 Josh Potts University of Illinois at Urbana- Champaign.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
ECE291 Lecture 9 Interrupt implementation (part of the magic explained!)
ECE291 Computer Engineering II Lecture 11 Josh Potts University of Illinois at Urbana- Champaign.
Dec Hex Bin 14 E ORG ; FOURTEEN Interrupts In x86 PC.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
Interrupts.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
ECE291 Computer Engineering II Lecture 14 Dr. Zbigniew Kalbarczyk University of Illinois at Urbana- Champaign.
Microprocessors Monday, Apr. 16 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
ECE291 Lecture 10 Interrupts II. ECE 291 Lecture 9Slide 2 of 22 Lecture outline Installing/Removing ISRsInstalling/Removing ISRs Interrupt SchedulingInterrupt.
Lecture 3 CSE 341 – Microprocessors Lecture 3 Md. Omar Faruqe UB 1228
BIOS and DOS Interrupts Basic Input /Outpu System Disk Operating System.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ECE291 Computer Engineering II Lecture 8 Josh Potts University of Illinois at Urbana- Champaign.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Internal Programming Architecture or Model
BITS Pilani Pilani Campus Pawan Sharma Lecture /12/ EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
ECE291 Computer Engineering II Lecture 14 Josh Potts University of Illinois at Urbana- Champaign.
ECE291 Computer Engineering II Lecture 12 Josh Potts University of Illinois at Urbana- Champaign.
Stack Operations Dr. Hadi AL Saadi.
Microprocessor and Assembly Language
Timer and Interrupts.
Interrupts In 8085 and 8086.
Instruksi Set Prosesor 8088
Chapter 10 The Stack.
Arithmetic using a stack
Stack and Subroutines Module M17.1 Section 11.2.
Programming 8086 – Part IV Stacks, Macros
Computer System Overview
8086 Registers Module M14.2 Sections 9.2, 10.1.
Symbolic Instruction and Addressing
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Microprocessor and Assembly Language
Presentation transcript:

More on Interrupts

Interrupt service routines oDOS facilities to install ISRs oRestrictions on ISRs oCurrently running program should have no idea that it was interrupted. oISRs should be as short as possible because lower priority interrupts are blocked from executing until the higher priority ISR completes FunctionAction INT 21h Function 25hSet Interrupt vector INT 21h Function 35hGet Interrupt vector INT 21h Function 31hTerminate and stay resident

Installing ISRs Let N be the interrupt to service oRead current function pointer in vector table oUse DOS function 35h oSet AL = N oCall DOS Function AH = 35h, INT 21h oReturns: ES:BX = Address stored at vector N oSet new function pointer in vector table oUse DOS function 25h oSet DS:DX = New Routine oSet AL = N oDOS Function AH = 25h, INT 21h

Installing ISR oInterrupts can be installed, chained, or called oInstall New interrupt replace old interrupt oChain into interrupt Service myCode first MyIntVector Save Registers Service Hardware Reset PIC Restore Registers IRET MyIntVector Save Registers MyCode Restore Registers JMP CS:Old_Vector MyIntVector PUSHF CALL CS:Old_Vector Save Registers MyCode Restore Registers IRET oCall Original Interrupt Service MyCode last

Timer interrupt example oIn this example we will patch the ISR for the Timer Interrupt oOur ISR will count the number of timer interrupts received oOur main program will use this count to display elapsed time in minutes and seconds

About the PC timer oWe’ll examine it in detail when we look at the 8253 oYou can view it as a clock that ticks and sends a signal to your processor approximately every 1/18.2 seconds oIt is also called the “eighteenth of a second” oWe’ll find out where this 18.2 comes from later oYou can approximately count minutes and seconds as follows oFor every 18 ticks of the timer we add 1 second oFor every 60 seconds we add one minute and reset the counters for seconds

ISR overview oInstall the ISR for vector 08h oCall original timer interrupt oIncrement a counter on every interrupt from the timer (count) oIncrement a second counter (scount) when count=18 and reset count oIncrement a minute counter (mcount) when scount=60 and reset the second counter oThe program will print the scount, mcount and count values in red, green and blue colors oIt will look as a (somewhat low-resolution) timer printed in your screen

Timer interrupt – main proc skeleton ;====== Variables =================== ; Old Vector (far pointer to old interrupt function) oldvRESW2 countDW 0;Interrupt counter (1/18 sec) scountDW 0;Second counter mcountDW 0;Minute counter pbufDB 8;Minute counter ;====== Main procedure =====..start … ;----Install Interrupt Routine----- call Install ;Main program (print count values).showc Mov ax, [mcount];Minute Count … call pxy mov ax, [scount];Second Count … call pxy mov ax,[count];Interrupt Count (1/18 sec) … call pxy mov ah,1 int 16h;Check for key press jz.showc;Quit on any key ;---- Uninstall Interrupt Routine----- call UnInst;Restore original INT8 … call mpxit

Timer interrupt – complete main proc..start movax, cs ;Initialize DS=CS movds, ax movax, 0B800h ;ES=VideoTextSegme nt moves, ax callinstall;Insert my ISR showc: movdi,12 ;Column 6 (DI=12/2) mov ah, b ;Intense Green callpxy movax,[count];Int Count (1/18th sec) movbx,pbuf callbinasc movbx, pbuf Contd…

Timer interrupt – complete main proc.. movax, [mcount] ;Minute Count movbx, pbuf callbinasc movbx, pbuf movdi,0;Column 0 movah, b ;Intense Red callpxy movax,[scount] ;Second Count movbx,pbuf callbinasc movbx, pbuf movah, b ;Cyan movdi,24 ;Column 12 (DI=24/2) callpxy movah,1 int16h;Key Pressed ? jzshowc CallUnInst;Restore original INT8 movax,4c00h ;Normal DOS Exit int21h

Timer interrupt – PXY and Install interrupt ;pxy (bx = *str, ah = color, di = column) pxy moval, [bx] cmpal, ‘$' je.pxydone moves:[di+2000], ax incbx adddi,2 jmppxy.pxydone ret moval, 8;INT = 8 movah, 35h;Read Vector Subfunction int21h;DOS Service movword [oldv+0], bx movword [oldv+2], es moval, 8;INT = 8 Contd…

Timer interrupt – PXY and Install interrupt ;====== Install Interrupt ===== install;Install new INT 8 vector pushes pushdx pushax pushbx movah, 25h;Set Vector Subfunction movdx, myint ;DS:DX point to function int21h;DOS Service pop bx pop ax pop dx pop es ret

Timer interrupt – uninstall interrupt ;====== Uninstall Interrupt =========== UnInst; Uninstall Routine (Reinstall old vector) pushds pushdx pushax movdx, word [oldv+0] movds, word [oldv+2] moval, 8; INT = 8 movah, 25h; Subfunction = Set Vector int21h; DOS Service popax popdx popds ret

Timer interrupt – ISR code ;====== ISR Code ========= myint pushds;Save all registers pushax movax, cs;Load default segment movds, ax pushf;Call Orig Function w/flags callfar [oldv];Far Call to existing routine incword [count] ;Increment Interrupt count cmpword [count],18 jne.myintdone incword [scount];Next second movword [count], 0 cmpword [scount], 60 jne.myintdone incword [mcount]; Next minute movword [scount], 0.myintdone popax;Restore all Registers popds iret;Return from Interrupt

Replacing An Interrupt Handler ;install new interrupt vector %macro setInt 3 ;Num, OffsetInt, SegmentInt pushax pushdx pushds ;store old interrupt vector %macro getInt 3 ;Num, OffsetInt, SegmentInt pushbx push es moval, %1 Contd…

Replacing An Interrupt Handler movdx, %2 movax, %3 movds, ax moval, %1 movah, 25h;set interrupt vector int21h popds popdx popax %endmacro movah, 35h ;get interrupt vector int21h mov %2, bx mov %3, es popes popbx %endmacro

Replacing An Interrupt Handler CREQU0dh LFEQU0ah SEGMENT stkseg STACK resb 8*64 stacktop: SEGMENT code WarningDB “Overflow - Result Set to ZERO!!!!”,CR,LF,0 msgOKDB “Normal termination”, CR, LF, 0 old04hOffsetRESW old04hSegmentRESW New04h;our new ISR for int 04 ;occurs on overflow sti;re-enable interrupts movax, Warning pushax callputStr ;display message xorax, ax;set result to zero cwd;AX to DX:AX iret

Replacing An Interrupt Handler mov ax, msgOK pushax callputStr Error: ;restore original int handler setInt 04h, [old04hOffset], [old04hSegment] movax, 4c00h int21h..start movax, cs movds, ax ;store old vector getInt04h, [old04hOffset], [old04hSegment] ;replace with address of new int handler setInt04h, New04h, cs moval, 100 addal, al into;calls int 04 if an overflow occurred testax, 0FFh jzError Contd…

Replacing An Interrupt Handler NOTES INTO is a conditional instruction that acts only when the overflow flag is set With INTO after a numerical calculation the control can be automatically routed to a handler routine if the calculation results in a numerical overflow. By default Interrupt 04h consists of an IRET, so it returns without doing anything.

Reentrancy oWhat happens if oAn ISR for some devices is executing and it has enabled interrupts oAnother interrupt from the same device comes along…The program may not behave correctly oAssume that the ISRs modify some register and store its value in a memory location oThe previous ISR might have already modified the register but didn’t have the time to store the value in the memory locations oThe new ISR will save this register but it will try to update the memory location, which is in an inconsistent state

Reentrancy AnISR ; assume scount=3, MSEC=950 push ds push ax mov ax, cs mov ds, ax mov ax, [MSEC] add ax, 55 ; ax=1005 cmp ax, 1000 jb SetMsec Contd…

Reentrancy ; Assume that another interrupt occurs at this point inc [scount] ; the second interrupt will set scount=4 sub ax, 1000 ; mov [MSEC], ax; the second interrupt will set MSEC=5… ; but the interrupted ISR will reexecute the inc so scount will be ; set to 5 although the timer has not ticked 55 times!!!! pop ax pop ds

Reentrancy oThe code between the mov ax, [MSEC] and the mov [MSEC], ax must be executed atomically, without any interruptions oThis is called a critical region oYou can protect a critical region from being interrupted by using: pushf;preserve the current I flag state cli; turn off interrupts …; critical region popf; restore the I flag state

Reentrancy in practice oFirst thing to remember: don’t call DOS from your ISRs, DOS is not reentrant oDOS subroutines assume to be entered by a single point at any time oIf you write an ISR and attempt to call DOS you will most likely hang the machine forever oSecond thing to remember: BIOS is not reentrant Contd…

Reentrancy in practice oThere are ways to check if you’re executing inside DOS by testing a flag (function code 34h) oIf the flag is 0 it is safe to call DOS oIf the flag is 1 it might not be safe to call DOS oYou can also check if DOS is “idling” (function code 28h), in that case it is safe to call DOS

DOS Memory Usage 00000h Interrupt vector 003FFh Various DOS/BIOS vars Free memory area for use by programs 0BFFFh High memory area Video, ROM, adapter memory 0FFFFh Free memory pointer

DOS Memory Usage 00000h Interrupt vector 003FFh Various DOS/BIOS vars Memory in use by your program 0BFFFh High memory area Video, ROM, adapter memory 0FFFFh Free memory pointer Free memory area

Terminate and Stay Resident (TSR) 00000h Interrupt vector 003FFh Various DOS/BIOS vars Marked by the program as resident and protected by DOS 0BFFFh High memory area Video, ROM, adapter memory 0FFFFh Free memory pointer Free memory area

Terminate and Stay Resident (TSR) oYour program can have a resident portion and a transient portion oThe main program, normal data, support routines etc, are usually transient oYou can define ISRs and maintain them in memory after the program terminates using the resident portion oUse DOS function 31h with the size of the resident portion passed in dx

Terminate and Stay Resident (TSR) oIf you want to use resident ISR’s you should define them in the lower parts of your memory address space oYou have to set your DS properly oThe resident code has no idea about the values of the segment registers oYou have to set the data segment to the value of your code segment push ds push cs pop ds; this moves cs to ds … pop ds