home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / COMM / MISC / SRC26_2.ZIP / SRC / CRT.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-05-31  |  3.7 KB  |  187 lines

  1. ;
  2. ; crt.asm  'small' C startup and runtime library for TSR programs
  3. ;
  4. ; Author: HIRANO Satoshi
  5. ; (C) 1990  Halca Computer Science Laboratory  TM
  6. ;            University of Tokyo
  7. ;
  8. ; 'Standard Free Software Statement' here :-)
  9. ;
  10. ; usage:
  11. ;    masm -Mx crt.asm;
  12. ;    cl -Ox -c tsr.c
  13. ;    link crt+tsr,tsr;
  14. ;
  15. ;    crt.asm calls main() in tsr.c after stack/data segment adjustment. 
  16. ;    There are two method to exit from main() function.
  17. ;    First is _exit(n) function which exits from the program normally
  18. ;    with return code 'n'.
  19. ;    Second is implied return from main() function which terminates the
  20. ;    program but stay it regident.
  21. ;
  22. ; library:
  23. ;    crt.asm provides some useful library functions.
  24. ;
  25. ;    void _exit(n)
  26. ;    int n;
  27. ;        Terminate the program. never return.
  28. ;
  29. ;    void putMsg(p)
  30. ;    char *p;
  31. ;        Print message to console. message must be terminated by '$' char.
  32. ;
  33. ;    void (interrupt far *getVect(vec))()
  34. ;    int vec;
  35. ;        Return far address of interrupt handler corresponding with 'vec'.
  36. ;    
  37. ;    void setVect(vec, address)
  38. ;    int vec;
  39. ;    void (interrupt far *address())();
  40. ;        Set interrupt handler 'address' to specified vector 'vec'.
  41. ;
  42. ;    int pspSeg;
  43. ;        PSP segment
  44. ;
  45.  
  46.         name    crt
  47.  
  48. _TEXT    SEGMENT  WORD PUBLIC 'CODE'
  49.         assume    cs:_TEXT
  50.         org        2ch
  51. envSeg    dw        ?                ; segment of environment area 
  52.         org        100h
  53. _TEXT    ENDS
  54. _DATA    segment word public 'DATA'
  55. public    __acrtused                ; MSC5.1 magic variable for debugger
  56.         __acrtused = 9876h
  57. public    _pspSeg
  58. _pspSeg dw        0
  59.         even
  60.         db        256 dup(?)
  61. stackTop    label    word
  62. _DATA    ends
  63. CONST    SEGMENT  WORD PUBLIC 'CONST'
  64. CONST    ENDS
  65. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  66. _BSS    ENDS
  67. DGROUP    GROUP    CONST, _BSS, _DATA
  68.         ASSUME    CS:_TEXT,DS:DGROUP,SS:DGROUP
  69.  
  70. extrn    _main:near
  71. extrn    _endAddress:byte
  72.  
  73. _TEXT    segment word public 'CODE'
  74.         ASSUME    CS:_TEXT,DS:DGROUP,SS:DGROUP
  75.  
  76. ;
  77. ; program entry point
  78. ;
  79.         public    __main
  80. __main:
  81.         mov        di,DGROUP
  82.         mov        ds,di
  83.         mov        ss,di
  84.         xor        bp,bp                    ; clear bp for debugger
  85.         mov        sp,offset ds:stackTop    ; set stack
  86.         mov        ds:_pspSeg,es            ; psp segment address
  87.         push    es                        ; PSP segment
  88.         mov        ax,81h                    ; PSP:0x81 = argument area
  89.         push    ax                        ; push (far) argument pointer
  90. ; free environment variable area
  91.         push    es
  92.         mov        es,es:[envseg]
  93.         mov        ah,49h
  94.         int        21h
  95.         pop        es
  96.         mov        es:[envseg],0
  97. ; call main program
  98.         call    _main                    ; call main(char far *argp)
  99. ; terminate but stay regident the program
  100.         mov        dx,offset cs:_endAddress
  101.         add        dx,10fh
  102.         mov        cl,4
  103.         shr        dx,cl                    ; dx := paragraph size
  104.         mov        ax,3100h
  105.         int        21h                        ; terminate but stay regident
  106. ; fall thru if error
  107.  
  108.  
  109. ;
  110. ; micro library for TSR program
  111. ;
  112.  
  113. ; _exit(code)
  114. ; int code
  115.         public    __exit
  116. __exit:
  117. ; terminate process
  118.         mov        ah,4ch                ; terminate process with AL
  119.         int        21h
  120. safety:    jmp        safety                ; I don't believe MS-DOS.
  121.  
  122. ;
  123. ; putMsg(p)        /* put message */
  124. ; char *p;
  125. ;
  126.         public    _putMsg
  127. _putMsg:
  128.         push    bp
  129.         mov        bp,sp
  130.         mov        dx,[4+bp]            ; string address
  131.         mov        ah,9                ; display string
  132.         int        21h
  133.         pop        bp
  134.         ret
  135.  
  136. ;
  137. ; void (interrupt far *getVect(vecNo))()
  138. ; int vecNo;
  139.         public    _getVect
  140. _getVect:
  141.         push    bp
  142.         mov        bp,sp
  143.         push    bx
  144.         push    es
  145.         mov        ax,[4+bp]
  146.         mov        ah,35h                ; al := vectNo, ah := get interrupt vector
  147.         int        21h
  148.         mov        dx,es                ; dx:ax := interrupt handler address
  149.         mov        ax,bx
  150.         pop        es
  151.         pop        bx
  152.         pop        bp
  153.         ret
  154.  
  155. ;
  156. ; setVect(vectNo, address)
  157. ; int vectNo;
  158. ; void far *address;
  159.         public    _setVect
  160. _setVect:
  161.         push    bp
  162.         mov        bp,sp
  163.         push    ds
  164.         push    dx
  165.         mov        ax,[4+bp]            ; vectNo
  166.         mov        ah,25h                ; set interrupt vector
  167.         mov        ds,[8+bp]
  168.         mov        dx,[6+bp]            ; ds:dx := handler address
  169.         int        21h
  170.         pop        dx
  171.         pop        ds
  172.         pop        bp
  173.         ret
  174.  
  175.         public    _int5
  176. _int5:
  177.         int        5h
  178.         ret
  179.  
  180.         public    _int6
  181. _int6:
  182.         int        6h
  183.         ret
  184.  
  185. _TEXT    ends
  186.         end __main
  187.