home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / alt / msdos / programm / 2717 < prev    next >
Encoding:
Text File  |  1992-11-12  |  4.2 KB  |  147 lines

  1. Xref: sparky alt.msdos.programmer:2717 comp.os.msdos.programmer:10544
  2. Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!datmuc!vincent
  3. From: vincent@datmuc.UUCP (vincent)
  4. Newsgroups: alt.msdos.programmer,comp.os.msdos.programmer
  5. Subject: my_tsr
  6. Message-ID: <1348@datmuc.UUCP>
  7. Date: 12 Nov 92 14:55:04 GMT
  8. Followup-To: alt.msdos.programmer,comp.os.msdos.programmer
  9. Lines: 136
  10.  
  11. This is a (re)repost I never got any replys, The Org. Q. was why this code
  12. doesn't work?
  13.  
  14. Some people on the net said that it was uncommented (correct I comment
  15. finish projects so my boss is happy). As to what the code does. Here we go.
  16.  
  17. I forge this sample tsr from the examples in Andrew Schulman's book
  18. 'Undocumented DOS':
  19.  
  20. The main part allocated a stack, frees the envirement, get and set the timer
  21. interrupt, calculates the size of the tsr, shrink the program block accordingnly
  22. and then go tsr.
  23.  
  24. The tsr should pop up durring timer interrupt, save forground stack and data
  25. segment, set our stack and data segement, make base pointer relative to the
  26. stack pointer, call the timer function (now only increments variable),
  27. on return from it reset the stack and the data segment and then chain
  28. the interrupt.
  29.  
  30. All data are in the code segment (for futur migth be a .COM file)
  31.  
  32. The compile flags turns off stack check, disable optimisation, use small
  33. memory model, ... (maybe need to put #pragma (check_pointer off)
  34.  
  35. ---------------------  snip snip -------------------------- makefile
  36.  
  37. CFLAGS = /Fc /Zi /f /Od /Gs /AS /W4 /nologo
  38.  
  39. main.exe : main.obj
  40.     cl /Zi /Fm main.obj
  41.  
  42. .c.obj:
  43.     cl /c $(CFLAGS) $(@B).c
  44.  
  45. ---------------------  snip snip --------------------------- main.c
  46.  
  47. #include    <stdlib.h>
  48. #include    <dos.h>
  49. #include    <bios.h>
  50.  
  51. #define    CODESEG            _based(_segname("_CODE"))
  52. #define MK_FP(seg,ofs)        ((void _far *) ((((long)seg)<<16) + (long)ofs))
  53. #define PARAGRAPHS(x)        ((FP_OFF(x) + 15) >> 4)
  54.  
  55. typedef            void    (interrupt _far *INTVECT) ();
  56.  
  57. struct {
  58.     INTVECT    old_timer;
  59.  
  60.     unsigned char _far * CODESEG tsrstack;
  61.  
  62.     int    oldSP,
  63.         oldSS,
  64.         oldDS,
  65.         ds_save,
  66.         seg_stack,
  67.         off_stack,
  68.         retadd,
  69.         tick_me;
  70. } CODESEG data;
  71.  
  72. void ctimer (void)
  73. {
  74.     data. tick_me++;
  75. }
  76.  
  77. void _interrupt _far new_timer (void)
  78. {
  79.     _asm {
  80.         mov    cs:data. oldSS, ss    ; save forground stuff
  81.         mov    cs:data. oldSP, sp
  82.         mov    cs:data. oldDS, ds
  83.                           ; Load new stack
  84.         mov    ss, cs:data. seg_stack
  85.         mov    sp, cs:data. off_stack
  86.         mov    ds, cs:data. ds_save
  87.  
  88.         mov    bp, sp            ; make bp relative to sp
  89.                         ; why? I don't know
  90.  
  91.         call    ctimer            ; do some proccessing
  92.  
  93.         mov    ss, cs:data. oldSS    ; restore the forground stuff
  94.         mov    sp, cs:data. oldSP
  95.         mov    ds, cs:data. oldDS
  96.  
  97.         mov    bp, sp
  98.     }
  99.     _chain_intr (data. old_timer);        ; jump to old function
  100. }
  101.  
  102. void main ()
  103. {
  104.     unsigned int memtop, dumy = 0;
  105.  
  106.     data. tick_me = 0;                // init. variables
  107.  
  108.                             // free envirement
  109.     _dos_freemem (*(short _far *) MK_FP (_psp, 0x2c));
  110.  
  111.                             // allocate a stack
  112.     data. tsrstack = malloc (1024);
  113.     data. tsrstack += 1024;                // go to the top of it
  114.  
  115.     data. seg_stack = FP_SEG (data. tsrstack);    // save the segment and
  116.     data. off_stack = FP_OFF (data. tsrstack);    // the offset for latter
  117.  
  118.     _asm mov cs:data. ds_save, ds            // save the data segment
  119.  
  120.                             // calculate the size
  121.     memtop = data. ds_save + PARAGRAPHS (data. tsrstack) - _psp;
  122.  
  123.     _dos_setblock (memtop, _psp, &dumy);        // shrink the prog.
  124.  
  125.     data. old_timer = _dos_getvect (0x08);        // get and set the timer
  126.     _dos_setvect (0x08, new_timer);            // interrupt
  127.  
  128.     _dos_keep (0, memtop);                // go tsr
  129. }
  130.  
  131. ---------------------------------------------------------------------
  132.  
  133. vincent@datmuc.dat.de
  134.  
  135. P.S. Well I hope this is better documented (my boss would be happy)
  136.  
  137. P.P.S. The problem is that on return to DOS (COMMAND) sucessive program
  138. crash (window for example);
  139.  
  140. P.P.P.S. I will continue on my side like instead of allocating a stack,
  141. have a fix buffer in the code segment. Also to date I'm not shure
  142. if I need to save the DS since it is anyway restore at the interrupt.
  143. Also in the inline asembly I might try to set ds to cs (for my data) and
  144. also for the stack seg.. (I guess I do need my own stack). The only thing I know
  145. about .COM is that LINK tell me that for tiny memory model I am not alowed
  146. to have _far pointers.
  147.