Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 10 Interrupts, Part 2, putting it all together Dr. Dimitrios S. Nikolopoulos CSL/UIUC."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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,00001100b;Intense Red callpxy movax,[scount];Second Count movbx,pbuf callbinasc movbx, pbuf movdi,12;Column 6 (DI=12/2) movah,00001010b;Intense Green callpxy movax,[count];Int Count (1/18th sec) movbx,pbuf callbinasc movbx, pbuf movah,00000011b;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

10 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

11 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

12 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

13 ECE291 Lecture 1013 The complete code with exe www.ece.uiuc.edu/ece291/lecture/timer.asm www.ece.uiuc.edu/ece291/lecture/timer.exe

14 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

15 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

16 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.

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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


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

Similar presentations


Ads by Google