Lecture 10 Interrupts, Part 2, putting it all together Dr. Dimitrios S. Nikolopoulos CSL/UIUC.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

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.
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.
Structure of DOS application programs. Contents: 1. PSP 2..COM and.EXE 3. TSR: Terminate and Stay Resident Programs.
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.
More on Interrupts. Interrupt service routines oDOS facilities to install ISRs oRestrictions on ISRs oCurrently running program should have no idea that.
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.
Interrupts, Part 1 Dr. Dimitrios S. Nikolopoulos CSL/UIUC
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 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
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.
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.
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.
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
Microprocessor Systems Design I
Timer and Interrupts.
Interrupts In 8085 and 8086.
Chapter 10 And, Finally... The Stack
Chapter 10 The Stack.
Stack and Subroutines Module M17.1 Section 11.2.
Programming 8086 – Part IV Stacks, Macros
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:

Lecture 10 Interrupts, Part 2, putting it all together Dr. Dimitrios S. Nikolopoulos CSL/UIUC

ECE291 Lecture 102 Interrupt service routines DOS facilities to install ISRs Restrictions on ISRs –Currently running program should have no idea that it was interrupted. –ISRs 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

ECE291 Lecture 103 Installing ISRs Let N be the interrupt to service Read current function pointer in vector table –Use DOS function 35h –Set AL = N –Call DOS Function AH = 35h, INT 21h –Returns: ES:BX = Address stored at vector N Set new function pointer in vector table –Use DOS function 25h –Set DS:DX = New Routine –Set AL = N –DOS Function AH = 25h, INT 21h

ECE291 Lecture 104 Installing ISR Interrupts can be installed, chained, or called Install New interrupt replace old interrupt Chain into interrupt Service myCode first Call Original Interrupt Service MyCode last 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

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

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

ECE291 Lecture 107 ISR overview Install the ISR for vector 08h Call original timer interrupt Increment a counter on every interrupt from the timer (count) Increment a second counter (scount) when count=18 and reset count Increment a minute counter (mcount) when scount=60 and reset the second counter The program will print the scount, mcount and count values in red, green and blue colors It will look as a (somewhat low-resolution) timer printed in your screen

ECE291 Lecture 108 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

ECE291 Lecture 109 Timer interrupt – complete main proc..start movax, cs;Initialize DS=CS movds, ax movax, 0B800h;ES=VideoTextSegment moves, ax callinstall;Insert my ISR showc: 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 movdi,12;Column 6 (DI=12/2) movah, b;Intense Green callpxy movax,[count];Int Count (1/18th sec) 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

ECE291 Lecture 1010 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 ;====== Install Interrupt ===== install;Install new INT 8 vector pushes pushdx pushax pushbx moval, 8;INT = 8 movah, 35h;Read Vector Subfunction int21h;DOS Service movword [oldv+0], bx movword [oldv+2], es moval, 8;INT = 8 movah, 25h;Set Vector Subfunction movdx, myint ;DS:DX point to function int21h;DOS Service pop bx pop ax pop dx pop es ret

ECE291 Lecture 1011 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

ECE291 Lecture 1012 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

ECE291 Lecture 1013 The complete code with exe

ECE291 Lecture 1014 Replacing An Interrupt Handler ;install new interrupt vector %macro setInt 3 ;Num, OffsetInt, SegmentInt pushax pushdx pushds movdx, %2 movax, %3 movds, ax moval, %1 movah, 25h;set interrupt vector int21h popds popdx popax %endmacro ;store old interrupt vector %macro getInt 3 ;Num, OffsetInt, SegmentInt pushbx push es moval, %1 movah, 35h ;get interrupt vector int21h mov %2, bx mov %3, es popes popbx %endmacro

ECE291 Lecture 1015 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

ECE291 Lecture 1016 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 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.

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

ECE291 Lecture 1018 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 ; 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

ECE291 Lecture 1019 Reentrancy The code between the mov ax, [MSEC] and the mov [MSEC], ax must be executed atomically, without any interruptions This is called a critical region You 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

ECE291 Lecture 1020 Reentrancy in practice First thing to remember: don’t call DOS from your ISRs, DOS is not reentrant –DOS subroutines assume to be entered by a single point at any time –If you write an ISR and attempt to call DOS you will most likely hang the machine forever Second thing to remember: BIOS is not reentrant There are ways to check if you’re executing inside DOS by testing a flag (function code 34h) –If the flag is 0 it is safe to call DOS –If the flag is 1 it might not be safe to call DOS –You can also check if DOS is “idling” (function code 28h), in that case it is safe to call DOS

ECE291 Lecture 1021 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

ECE291 Lecture 1022 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

ECE291 Lecture 1023 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

ECE291 Lecture 1024 Terminate and Stay Resident (TSR) Your program can have a resident portion and a transient portion The main program, normal data, support routines etc, are usually transient You can define ISRs and maintain them in memory after the program terminates using the resident portion Use DOS function 31h with the size of the resident portion passed in dx

ECE291 Lecture 1025 Terminate and Stay Resident (TSR) If you want to use resident ISR’s you should define them in the lower parts of your memory address space You have to set your DS properly –The resident code has no idea about the values of the segment registers –You 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