home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / tnylib.lzh / LBEGIN.ASM < prev    next >
Encoding:
Assembly Source File  |  1987-02-25  |  4.6 KB  |  254 lines

  1. ; adapted from LBEGIN by Manx Software Systems
  2.  
  3. ; This routine provides the interface between the DOS loader and
  4. ; the C program. The linker fills in $MEMRY with the address of
  5. ; the first unused RAM area; control enters at $begin when the
  6. ; program is loaded. In the C86 system the corresponding routine
  7. ; is $main..............................
  8.  
  9.     include lmacros.h
  10.  
  11. codeseg segment para public 'code'
  12.     public    $MEMRY
  13.     public    _mbot_, _sbot_, _mtop_,_lowwater_
  14.  
  15. dataseg segment para public 'data'
  16. $MEMRY    dw    -1
  17.     dw    -1
  18.     public    errno_
  19. errno_    dw    0
  20.     public    _dsval_,_csval_
  21. _dsval_ dw    0
  22. _csval_ dw    0
  23. _mbot_    dw    0,0
  24. _sbot_    dw    0,0
  25. _mtop_    dw    0,0
  26. _lowwater_    dw    -1
  27.     public    _PSP_
  28. _PSP_    dw    0
  29.     extrn    _HEAPSIZ_:word,_STKSIZ_:word
  30.     extrn    _STKLOW_:word,_STKRED_:word
  31.     extrn    _Uorg_:byte,_Uend_:byte
  32.  
  33. ; for d2io library (C) 1986, Jim Kyle, Chip Rabinowitz
  34.  
  35.     public    _ftb_
  36. ;_ftb    equ    this byte
  37. _ftb_    dw    0ffffh            ; stdin ungetc
  38.     db    1,0,0
  39.     dw    0ffffh            ; stdout
  40.     db    2,1,0
  41.     dw    0ffffh            ; stderr
  42.     db    2,2,0
  43.     dw    0ffffh            ; stdlst
  44.     db    3,3,0
  45.     dw    0ffffh            ; stdaux
  46.     db    3,4,0
  47.     dw    0ffffh            ; first user handle
  48.     db    0,0,0
  49.     dw    0ffffh
  50.     db    0,0,0
  51.     dw    0ffffh
  52.     db    0,0,0
  53.     dw    0ffffh
  54.     db    0,0,0
  55.     dw    0ffffh
  56.     db    0,0,0
  57.     dw    0ffffh
  58.     db    0,0,0
  59.     dw    0ffffh
  60.     db    0,0,0
  61.     dw    0ffffh
  62.     db    0,0,0
  63.     dw    0ffffh
  64.     db    0,0,0
  65.     dw    0ffffh
  66.     db    0,0,0
  67.     dw    0ffffh
  68.     db    0,0,0
  69.     dw    0ffffh
  70.     db    0,0,0
  71.     dw    0ffffh
  72.     db    0,0,0
  73.     dw    0ffffh
  74.     db    0,0,0
  75.     dw    0ffffh
  76.     db    0,0,0
  77.  
  78. dem    db    0dh,0ah,'DOS version 2.x or higher required!$'
  79. dataseg ends
  80.  
  81. exitad    dw    0
  82. exitcs    dw    0
  83.  
  84.     assume    cs:codeseg,ds:dataseg,es:dataseg,ss:dataseg
  85.  
  86. ifdef FARPROC
  87.     extrn Croot_:far
  88. else
  89.     extrn Croot_:near
  90. endif
  91.  
  92.     public    $begin
  93.     public    _exit_
  94.  
  95. $begin    proc    far
  96.     mov    bp,dataseg
  97.     test    bp,bp
  98.     jnz    notcom
  99.     mov    ax,253        ; This CAN'T be a com file
  100.     jmp    badexit
  101. notcom:
  102.     mov    ah,'0'  ;30h
  103.     int    21h
  104.     mov    dx,ss            ;save SS value
  105.     cmp    al,2
  106.     jnb    dok
  107.     mov    ds,dx
  108.     mov    dx,offset dem
  109.     mov    ah,9
  110.     int    21h
  111.     int    20h
  112. dok:
  113.     mov    exitcs,ds
  114.     mov    es,bp
  115.     mov    es:_PSP_,ds
  116.     mov    bp,offset _Uend_ ; stack starts at Uend
  117.     add    bp,15        ; round up to next paragraph
  118.     rcr    bp,1
  119.     mov    cl,3        ; and convert to paragraph form
  120.     shr    bp,cl
  121.     and    bp,01fffh
  122.     mov    dx,es        ; add in datasegment base paragraph
  123.     add    bp,dx
  124.     mov    bx,es:_STKRED_
  125.     shr    bx,cl        ;compute (2*STKRED)/16    note: cl==3
  126.     cmp    bx,es:_STKSIZ_    ;allocate either 2*STKRED or STKSIZ
  127.     jae    ok1
  128.     mov    bx,es:_STKSIZ_    ;whichever is bigger
  129. ok1:
  130.     cmp    bx,1000h    ; stack can't be bigger than 64K
  131.     jb    ok2
  132.     mov    bx,1000h
  133. ok2:
  134.     mov    dx,bx
  135.     mov    cl,4
  136.     shl    bx,cl
  137.     cli
  138.     mov    ss,bp
  139.     mov    sp,bx
  140.     sti
  141. ;
  142.     add    dx,bp
  143.     inc    dx
  144.     mov    es:$MEMRY+2,dx    ; and save as bottom of heap
  145.     mov    es:$MEMRY,0
  146.     mov    es:_mbot_+2,dx    ; and save as bottom of heap
  147.     mov    es:_mbot_,0
  148. ;                now adjust heap size if necessary
  149.     mov    bx,es:_HEAPSIZ_
  150.     add    bx,es:$MEMRY+2    ; add in base paragraph of heap
  151.     mov    es:_mtop_+2,bx    ; and save as top of heap
  152.     mov    es:_mtop_,0
  153.     mov    si,ds
  154.     sub    bx,si        ; get size of total program in paragraphs
  155.     mov    bp,es
  156.     mov    es,si        ; point es at PSP
  157.     mov    ah,4ah
  158.     int    21h        ; SETBLOCK to raise or lower allocation
  159.     jnc    didheap
  160.     mov    ax,254
  161.     jmp    badexit
  162. didheap:
  163.     mov    es,bp        ; restore es to point at dataseg
  164. ;
  165.     cld
  166. ;        clear uninitialized data
  167.     mov    di,offset _Uorg_
  168.     mov    cx,offset _Uend_
  169.     sub    cx,di
  170.     inc    cx
  171.     shr    cx,1
  172.     jcxz    noclear
  173.     sub    ax,ax
  174. rep    stosw
  175. noclear:
  176. ;
  177.     mov    es,[2cH]        ;get enviroment segment
  178.     sub    di,di
  179.     mov    cx,7fffH
  180. arglook:
  181.     mov    ah,es:byte ptr [di]
  182.     cmp    ah,'='                  ;look for null named env. string
  183.     je    found_args
  184.     test    ah,ah
  185.     jz    no_args
  186. repne    scasb                ;look for null byte
  187.     jz    arglook
  188. no_args:
  189.     mov    si,ds
  190.     mov    es,si
  191.     mov    si,081h
  192.     mov    di,080h
  193.     mov    cl,[80h]
  194.     sub    ch,ch
  195.     jcxz    nomov
  196. rep    movsb
  197. nomov:
  198.     sub    al,al
  199.     stosb
  200.     mov    ax,1
  201.     mov    di,080h
  202.     jmp    short mov_args
  203. ;
  204. found_args:
  205.     sub    ax,ax
  206.     stosb            ;zap and skip over '='
  207. mov_args:
  208.     push    ax        ; first arg # for Croot
  209.     push    es
  210.     push    di        ; argp argument for Croot
  211.     mov    es,bp
  212.     mov    ds,bp        ;set DS, now DS, SS, ES are equal
  213.     mov    _dsval_,ds
  214.     mov    _csval_,cs
  215.     mov    ax,_STKRED_
  216.     mov    _sbot_,ax
  217.     call    Croot_        ;Croot(argp, first)
  218.     jmp    short exits
  219. _exit_:
  220.     pop    ax
  221.     pop    ax        ;fetch return code
  222. ifdef FARPROC
  223.     pop    ax
  224. exit    label    far
  225. else
  226. exit:
  227. endif
  228. exits:
  229. badexit:
  230.     mov    ah,4cH
  231.     int    21H
  232.     jmp    dword ptr exitad
  233. $begin    endp
  234.  
  235. retip    dw    0
  236. retcs    dw    0
  237.  
  238.     public $dbgentry
  239. $dbgentry proc far
  240.     pop    cs:retip
  241.     pop    cs:retcs    ; save return address into debugger
  242.     pop    ds        ;set DS
  243.     call    caller
  244.     jmp    dword ptr cs:retip ; return to debugger
  245. $dbgentry endp
  246.  
  247. caller    proc
  248.     push    di        ;CS value of local function
  249.     push    si        ;IP value call local function
  250.     db    0cbH        ;far return instruction
  251. caller    endp
  252. codeseg ends
  253.     end    $begin
  254.