home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / os / msdos / programm / 11452 < prev    next >
Encoding:
Text File  |  1992-12-16  |  7.6 KB  |  257 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!haven.umd.edu!wam.umd.edu!kovert
  3. From: kovert@wam.umd.edu (Todd Kover)
  4. Subject: tsr
  5. Message-ID: <1992Dec16.203706.21696@wam.umd.edu>
  6. Keywords: int17 int dasness tsr
  7. Sender: usenet@wam.umd.edu (USENET News system)
  8. Nntp-Posting-Host: rac2.wam.umd.edu
  9. Organization: Workstations at Maryland, University of Maryland, College Park
  10. Date: Wed, 16 Dec 1992 20:37:06 GMT
  11. Lines: 244
  12.  
  13.  
  14. I'm in the process of writing a TSR that buffers the characters that go to
  15. the LPT2 port so that they can be picked up by another program.  Unfortunately,
  16. as I free up memory right before exiting (int 21h/4ah) I get a "memory
  17. control block destroyed" error.  I'd imagine I've overlooked something, but
  18. I've spent many hours last night (read: this morning) and today trying to
  19. solve this final problem...  This is my first venture into TSR writing,
  20. as well.
  21.  
  22. Anybody got any comments as to what I'm doing wrong? Thanks in advance for
  23. your help!
  24.  
  25. -Todd
  26.  
  27. ;
  28. ; RDLTSR.ASM -- Resident LPT Port printer memory buffer
  29. ;
  30.  
  31. .MODEL TINY
  32. .STACK 400h
  33. .CODE
  34.  
  35. @display MACRO msg
  36.         push ax
  37.         push ds
  38.         push dx
  39.         mov  ax,@code
  40.         mov  ds,ax
  41.         mov  dx,offset msg      ; msg to printout.
  42.         mov  ah,09h             ; DOS PrintString function
  43.         int  21h
  44.         pop  dx
  45.         pop  ds
  46.         pop  ax
  47. ENDM
  48.  
  49. StartOProgram:
  50.         jmp Initilize_Program                  ; goto start of program
  51.  
  52. ACK             equ     4242h   ; acknowledge our existance with this #.
  53. PrintStatus     equ     10010000B
  54. BufSize         equ     8196    ; Size of the buffer we kee
  55.  
  56.  
  57. LPTPort         dw      0       ; # for LPT port: 0==LPT1, 1==LPT2, 2==LPT3
  58. old_17h         dd      0       ; Address of int 17h's old routine
  59. LPTBuffer       db      BufSize DUP (?)
  60. LPTHead         dw      (?)
  61. LPTTail         dw      (?)
  62.  
  63. PUBLIC _int_17h_proc
  64.  
  65. ;------------------------------------------------------------------------------
  66. ;
  67. ; Replacement interupt vector for INT 17
  68. ;
  69. ;------------------------------------------------------------------------------
  70. PROC _int_17h_proc far
  71.         ASSUME DS:NOTHING,ES:NOTHING
  72.         pushf
  73.         push ds
  74.         push bx
  75.         push ax
  76.  
  77.         cmp ah,05h      ; Looking to see if we're here?
  78.         je  ack_em      ; tell em we're here.
  79.  
  80.         cmp ah,06h      ; They're asking for a byte.
  81.         je  get_byte    ; give it to 'em
  82.  
  83.         cmp ah,07h      ; Setting the LPT port
  84.         je  set_lpt     ; set it
  85.  
  86.         cmp DX, [cs:LPTPort]    ; our printer?
  87.         jne old_handle17        ; no.
  88.  
  89.         cmp ah, 00h     ; print call?
  90.         je  capture     ; go do stuff
  91.  
  92. ret_stat:               ; otherwise do the normal thang.
  93.         pop ax
  94.         pop bx
  95.         pop ds
  96.         popf
  97.         mov ah, PrintStatus
  98.         iret
  99.  
  100. ack_em:
  101.         pop ax
  102.         pop bx
  103.         pop ds
  104.         popf
  105.         mov ax,ACK              ; We acknowledge our presence with this
  106.         iret
  107.  
  108. set_lpt:
  109.         mov LPTPort,dx          ; LPT port is put in dx
  110.         pop ax
  111.         pop bx
  112.         pop ds
  113.         popf
  114.         mov ax,ACK              ; non-zero for success
  115.         iret
  116.  
  117. get_byte:
  118.         pop ax                          ; recover from before
  119.         pop bx
  120.         pop ds
  121.         popf
  122.  
  123.         push bp                         ; let the games begin.
  124.         push ds
  125.  
  126.         mov ax,cs
  127.         mov ds,ax
  128.  
  129.         mov bp,sp
  130.         xor ax,ax                       ; set value to zero in case 'o error
  131.  
  132.         mov bx,[LPTTail]                ; empty?
  133.         cmp bx,[LPTHead]
  134.         je  return_byte                 ; yes -- return
  135.  
  136.         mov al, [byte ptr bx]           ; get byte from current place
  137.         inc [LPTTail]                   ; goto next space
  138.         cmp [word ptr LPTTail], offset LPTBuffer+BufSize        ; end'obuf?
  139.         jb  return_byte
  140.  
  141.         mov [LPTTail], offset LPTBuffer ; at end -- goto beginning
  142.  
  143. return_byte:
  144.         pop ds
  145.         pop bp
  146.         mov ah,PrintStatus
  147.         iret
  148.  
  149. old_handle17:
  150.         pop ax
  151.         pop bx
  152.         pop ds
  153.         popf
  154.         jmp [CS:old_17h]
  155.  
  156. capture:
  157.                                                 ; al contains byte to write
  158.  
  159.         mov bx, [LPTHead]                       ; Place to put byte
  160.         mov [byte ptr bx], al                   ; Move byte to place in
  161.         inc bx                                  ; Next space is the place
  162.         cmp bx, offset LPTBuffer + BufSize      ; at end?
  163.         jb @@10
  164.         mov bx, offset LPTBuffer                ; wrap around
  165. @@10:
  166.         cmp bx, [LPTTail]
  167.         jne @@20
  168.         mov bx, [LPTHead]
  169. @@20:
  170.         mov [LPTHead],bx
  171. @@end:
  172.         jmp ret_stat
  173. ENDP    _int_17h_proc
  174.  
  175. EndOfRes        db      0
  176.  
  177. WelcomeMsg      db '╔════════════════════════╗',13,10
  178.                 db '║                        ║',13,10
  179.                 db '║ RDLINK - PTRBUF TSR v1 ║',13,10
  180.                 db '║    by Todd M. Kover    ║',13,10
  181.                 db '║                        ║',13,10
  182.                 db '╚════════════════════════╝',13,10,13,10,'$'
  183.  
  184. InstallMsg      db 'TSR Installed.',13,10,'$'
  185. AlreadyHereMsg  db 'TSR Already Installed.',13,10,'$'
  186. AllocErrorMsg   db 'Memory Allocation Error.  TSR Not Installed.',13,10,'$'
  187.  
  188. Initilize_Program:
  189.         mov ax,cs
  190.         mov ds,ax
  191.  
  192.         @display WelcomeMsg
  193.  
  194.         mov ah,05h                      ;Check to see if we're here
  195.         int 17h
  196.         je already_installed            ;notify user of carelessness
  197.  
  198.         mov ax, offset LPTBuffer        ;Set Head AND Tail to beginning
  199.         mov [LPTHead], ax               ;of buffer
  200.         mov [LPTTail], ax
  201.  
  202.         mov ax,3517h                    ;get current intvect for 17h
  203.         int 21h
  204.         mov [word ptr old_17h],bx
  205.         mov [word ptr old_17h+2],es     ;save it for later use
  206.  
  207.         mov ax,2517h                    ;set current intvect for 17h
  208.         mov dx, offset _int_17h_proc    ;Our interrupt vector
  209.         int 21h                         ;ds == cs
  210.  
  211.         mov ax,@code
  212.         mov es,ax                       ;Put SEGMENT of block in ES
  213.         mov bx,offset EndOfRes          ;Get end of resident code
  214.         add bx,15                       ;Add a paragraph
  215.         mov cl,4                        ;Shift by 4 to divide by 16
  216.         shr bx,cl                       ;This converts to paragraphs
  217.         mov ax,4a00h                    ;Dos setblock function call
  218.         int 21h                         ;Modify memory block
  219.         jnc ExitTSR                     ;If it worked ok, then continue
  220.  
  221. Alloc_Error:
  222.         @display AllocErrorMsg
  223.         mov dx,[word ptr old_17h]       ;restore old int 17h vector
  224.         mov ax,[word ptr old_17h+2]
  225.         mov ds,ax
  226.         mov ax,2517h                    ;Restore int 17h vector from DS:DX
  227.         int 21h
  228.  
  229.         JMP ExitNoTSR                   ;Exit the TSR wo/residence
  230.  
  231. ExitTSR:
  232.         @display InstallMsg
  233.  
  234.         mov dx,offset EndOfRes          ;Move End of Resident Part to dx
  235.         add dx,15
  236.         mov cl,4                        ;shifting right 4 == division by 16
  237.         shr dx,cl                       ;Conversion to paragraphs
  238.         mov ax,3100h                    ;func 31; return code == 0
  239.         int 21h                         ;Terminate and Stay Resident
  240.  
  241. already_installed:
  242.         @display AlreadyHereMsg
  243.  
  244. ExitNoTSR:
  245.         mov ax,4c00h                    ;4c==exit, retcode==0
  246.         int 21h                         ;c-ya
  247.  
  248. END
  249.  
  250. --<cut here>
  251.  
  252. -- 
  253. Internet: kovert@wam.umd.edu +================+ #include <witty/disclaimer.h>
  254.          kovert@glue.umd.edu | Todd Kover //  | "Murphy's Law of Dating II: If
  255. Fido: 1:109/447 & 1:261/5016 |          \X/   |  you like her, she lives in a
  256.              CIS: 70621,2376 +================+  different state."
  257.