home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2MISC / CSDPMI3S.ZIP / SRC / CWSDPMI / START.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-07-31  |  5.9 KB  |  341 lines

  1. ; Copyright (C) 1995,1996 CW Sandmann (sandmann@clio.rice.edu) 1206 Braelinn, Sugarland, TX 77479
  2. ;
  3. ; This file is distributed under the terms listed in the document
  4. ; "copying.cws", available from CW Sandmann at the address above.
  5. ; A copy of "copying.cws" should accompany this file; if not, a copy
  6. ; should be available from where this file was obtained.  This file
  7. ; may not be distributed without a verbatim copy of "copying.cws".
  8. ;
  9. ; This file is distributed WITHOUT ANY WARRANTY; without even the implied
  10. ; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11.  
  12.     title    cwsdpmi
  13.     include    segdefs.inc
  14.  
  15. ;------------------------------------------------------------------------
  16.  
  17.     start_data16
  18.     public    __psp, __osmajor, __osminor, __brklvl, ___brklvl
  19. __psp        dw    0
  20. __osmajor    db    0
  21. __osminor    db    0
  22. __brklvl     dd    0
  23. ___brklvl     dw    offset DGROUP:bss_end
  24. firstf        dw    offset DGROUP:bss_end
  25.     extrn __stklen:word
  26.     end_data16
  27.  
  28.     start_bss
  29. bss_start     label    byte
  30.     public    _errno
  31. _errno        dw    ?
  32.     end_bss
  33.  
  34. _bssend segment
  35. bss_end        label    word
  36.     ends
  37.  
  38. ;------------------------------------------------------------------------
  39.  
  40.     start_code16
  41.  
  42. extrn    _main:near
  43.     .8086
  44.  
  45. start    proc near
  46.     mov    bp, ds:[2]    ; Highest memory segment
  47.     mov    dx, DGROUP
  48.     mov    ds, dx
  49.     mov    __psp, es
  50.  
  51.     sub    bp, dx        ; Our current DGROUP size in paragraphs
  52.     mov    di, __stklen    ; Requested stack size
  53.     add    di, offset DGROUP:bss_end
  54.     mov    cl, 4
  55.     shr    di, cl        ; Convert bss_end+stack to paragraphs
  56.     inc    di        ; Wanted DGROUP size in paragraphs
  57.     cmp    bp, di
  58.     jb    short _exit    ; Not enough memory (but we fixed header!?)
  59.  
  60.     mov    di, 1000h    ; More than 64K?
  61.     cmp    bp, di
  62.     ja    short mem_ok    ; Someone carried away with CWSPARAM, 64K Max
  63.     mov    di, bp        ; The usual case
  64.  
  65. ;    Move to new stack, then return extra memory to DOS
  66.  
  67. mem_ok:    mov    bx, di
  68.     shl    di, cl        ; Convert back to bytes from paragraphs
  69.     mov    ss, dx        ; Set the program stack
  70.     mov    sp, di
  71.  
  72.     add    bx, dx        ; Segment value for end of our new DGROUP
  73.     mov    word ptr __brklvl+2, bx
  74.     mov    ax, es        ; __psp 
  75.     sub    bx, ax        ; Number of paragraphs to keep
  76.     mov    ah, 04Ah    ; DOS resize memory block call
  77.     int    021h
  78.  
  79. ;    Clear BSS area to zero
  80.  
  81.     push    ds
  82.     pop    es
  83.     mov    di, offset DGROUP:bss_start
  84.     mov    cx, offset DGROUP:bss_end
  85.     sub    cx, di
  86.     xor    ax, ax
  87.     cld
  88.     rep    stosb
  89.  
  90.     mov    ah, 30h        ; get DOS version into TCC like variable
  91.     int    21h    
  92.     mov    word ptr __osmajor, ax
  93.  
  94.     mov    ds:[bss_end],8000h    ; For malloc
  95.  
  96.     call    _main        ; Never return, so this is OK
  97.     endp
  98.     
  99.     PUBLIC    __exit,_exit    ; Always error exit here
  100. __exit    proc    near
  101. _exit:    mov    ax,4C01h
  102.     int    21h
  103.     endp
  104.  
  105.     PUBLIC    _unlink
  106. _unlink    proc    near
  107.     push    bp
  108.     mov    bp,sp
  109.     mov    ah,41h        ; Delete file
  110.     jmp    short comn1
  111.     endp
  112.  
  113.     PUBLIC    __creat
  114. __creat    proc    near
  115.     push    bp
  116.     mov    bp,sp
  117.     xor    cx,cx        ; No attributes (ignore bp+6)
  118.     mov    ah,3Ch        ; Create new (or zero length existing) file
  119. comn1:    mov    dx,[bp+4]    ; Name of file
  120.     int    21h
  121.     jnc    short do_ret
  122.     mov    ah,-1        ; AX thus negative, shorter, AL has err code
  123.     jmp    short do_ret
  124.     endp
  125.  
  126.     PUBLIC    __write
  127. __write    proc    near
  128.     push    bp
  129.     mov    bp,sp
  130.     mov    ah,40h        ; Write
  131. com3:    mov    cx,[bp+8]    ; Buffer size
  132.     mov    dx,[bp+6]    ; Buffer offset
  133. com1:    mov    bx,[bp+4]    ; File handle
  134.     int    21h
  135.     jnc    short do_ret
  136.     xor    ax,ax
  137. do_ret:    pop    bp
  138.     ret
  139.     endp
  140.  
  141.     PUBLIC    __read
  142. __read    proc    near
  143.     push    bp
  144.     mov    bp,sp
  145.     mov    ah,3fh        ; Read
  146.     jmp    short com3
  147.     endp
  148.  
  149.     PUBLIC    _lseek
  150. _lseek    proc    near
  151.     push    bp
  152.     mov    bp,sp
  153.     mov    ax,4200h    ; Seek absolute (always abs here)
  154.     jmp    short com3
  155.     endp
  156.  
  157.     PUBLIC    __close
  158. __close    proc    near
  159.     push    bp
  160.     mov    bp,sp
  161.     mov    ah,3eh        ; Close
  162.     jmp    short com1
  163.     endp
  164.  
  165.     PUBLIC    __restorezero    ; Bogus, for TCC compatibility
  166. __restorezero    proc    near
  167.     ret
  168.     endp
  169.  
  170.     PUBLIC    _free
  171. _free    proc    near
  172.     push    bp
  173.     mov    bp,sp
  174.     mov    bx,[bp+4]    ; pointer
  175.     dec    bx
  176.     or    byte ptr [bx], 80h    ; negative size means "free"
  177.     dec    bx
  178.     cmp    firstf,bx
  179.     jb    short f1
  180.     mov    firstf,bx
  181. f1:    pop    bp
  182.     ret
  183.     endp
  184.     
  185.     PUBLIC    _malloc
  186. _malloc    proc    near
  187.     push    bp
  188.     mov    bp,sp
  189.     mov    dx,[bp+4]    ; size
  190.     add    dx,3        ; for header and roundup
  191.     and    dl,0feh        ; clear roundup
  192.  
  193.     mov    bx,firstf
  194. m0:    mov    ax,[bx]        ; header size/tag
  195.     test    ax,ax
  196.     js    short m1
  197.     cmp    firstf,bx    ; not free!
  198.     jne    short m3
  199.     add    firstf,ax
  200. m3:    add    bx,ax
  201.     jmp    short m0
  202.     
  203. m1:    and    ah,7fh        ; get the size
  204.     cmp    ax,dx
  205.     jb    short m2
  206.  
  207. ;    We have a big enough block
  208.  
  209.     sub    ax,dx        ; ax is now extra
  210.     jmp    short m4
  211.  
  212. m2:    or    ax,ax
  213.     jne    short m3    ; don't try to merge since all same size    
  214.  
  215. ;    End of chain (ax = 0)
  216.  
  217.     mov    cx,bx
  218.     add    cx,dx        ; new end
  219.     add    cx,380h        ; enough for a single HW interrupt
  220.     cmp    cx,sp
  221.     ja    short mend
  222.     mov    ax,8000h
  223. m4:    mov    [bx],dx
  224.     add    bx,dx
  225.     or    ax,ax
  226.     je    short m5
  227.     or    ah,80h
  228.     mov    [bx],ax
  229. m5:    mov    ax,bx
  230.     sub    ax,dx
  231.     cmp    firstf,ax
  232.     jne    short m6
  233.     mov    firstf,bx
  234. m6:    add    ax,2
  235. mend:    pop    bp
  236.     ret
  237.     endp
  238.  
  239. csub    macro    name
  240.     public    name
  241. name :    push    bp
  242.     mov    bp,sp
  243.     endm
  244.  
  245. asub    macro    name
  246.     public    name
  247. name :
  248.     endm
  249.  
  250. csub    _getvect
  251.     mov    ah,35h
  252.     mov    al,[bp+4]
  253.     int    21h
  254.     mov    dx,es
  255.     xchg    bx,ax        ; Shorter than mov ax,bx
  256.     pop    bp
  257.     ret
  258.  
  259. csub    _setvect
  260.     mov    ah,25h
  261.     push    ds
  262.     mov    al,[bp+4]
  263.     lds    dx,[bp+6]
  264.     int    21h
  265.     pop    ds
  266.     pop    bp
  267.     ret
  268.  
  269. csub    _memset
  270.     push    ds
  271.     pop    es
  272.     cld
  273.     push    di
  274.     mov    di,[bp+4]
  275.     mov    al,[bp+6]
  276.     mov    cx,[bp+8]
  277.     rep    stosb
  278.     pop    di
  279.     pop    bp
  280.     ret
  281.     
  282. csub    _movedata
  283.     push    si
  284.     push    di
  285.     push    ds
  286.     mov    ds,[bp+4]
  287.     les    si,[bp+6]        ; si=6,es=8 (horrid hack for space!)
  288.     mov    di,[bp+10]
  289.     mov    cx,[bp+12]
  290.     cld
  291.     rep    movsb
  292.     pop    ds
  293.     pop    di
  294.     pop    si
  295.     pop    bp
  296.     ret
  297.  
  298. csub    N_SCOPY@
  299.     push    si
  300.     push    di
  301.     push    ds
  302.     lds    si,[bp+4]
  303.     les    di,[bp+8]
  304.     cld
  305.     rep    movsb
  306.     pop    ds
  307.     pop    di
  308.     pop    si
  309.     pop    bp
  310.     ret    8
  311.  
  312. asub    N_LXMUL@            ; Cheat - assume only result is long
  313.     mul    bx
  314.     ret
  315.  
  316.     .386
  317. asub    N_LXLSH@
  318.     shld    dx,ax,cl
  319.     xor    bx,bx
  320.     shld    ax,bx,cl
  321.     ret
  322.  
  323. asub    N_LXRSH@
  324.     mov    bx,dx
  325.     sar    bx,15
  326. rsh:    shrd    ax,dx,cl
  327.     shrd    dx,bx,cl
  328.     ret
  329.  
  330. asub    N_LXURSH@
  331.     xor    bx,bx
  332.     jmp    short rsh
  333.  
  334.     end_code16
  335.  
  336. _STACK    segment stack 'STACK'
  337.     db    128 dup(?)    ;minimal startup stack (overwritten)
  338.     ends
  339.  
  340.     end    start
  341.