Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More on Interrupts. Interrupt service routines oDOS facilities to install ISRs oRestrictions on ISRs oCurrently running program should have no idea that."— Presentation transcript:

1 More on Interrupts

2 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

3 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

4 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

5 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

6 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

7 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

8 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

10 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,00001010b ;Intense Green callpxy movax,[count];Int Count (1/18th sec) movbx,pbuf callbinasc movbx, pbuf Contd…

11 Timer interrupt – complete main proc.. 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 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

12 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…

13 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

14 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

15 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

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

17 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

18 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

19 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…

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

21 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

22 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…

23 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

24 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

25 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…

26 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

27 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

28 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

29 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

30 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

31 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


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

Similar presentations


Ads by Google