home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l360 / 2.ddi / LINITC31.@SM / LINITC31.ASM
Encoding:
Assembly Source File  |  1991-04-08  |  10.9 KB  |  434 lines

  1.     name    LINITC
  2.     title    LINITC - Lattice C Initialization
  3.     subttl    Copyright (C) Micro Focus Ltd 1987
  4.  
  5.     public    __NCLINKM    ; these are routines called from LCOBOL
  6.     public    __NCLINKI    ; from strategic places such as
  7.     public    __NCLINKX    ; exit from a program, startup, closedown
  8.     public    __NCLINKXS    ; etc.
  9.     public    __NCLINKS
  10.     public    __NCLINK
  11.     public    _MAIN        ; dummy routine to satisfy ref in LC.LIB
  12.  
  13.     .8087
  14.  
  15. CBLRUDAT segment para public 'CBL_DATA'
  16.  
  17.     include cblrudat.equ
  18.  
  19. CBLRUDAT ends
  20.  
  21.     include    handle.equ
  22.  
  23. ; This module provides an interface for calling Lattice V3.1x objects.
  24. ; Link your program as follows
  25. ;    LINK COBOL_objects+LINITC+LINITIO+C_objects,,,LCOBOL+LC+C_libraries/m;
  26.  
  27.  
  28. ;**
  29. ;
  30. ; Assembly parameters
  31. ;
  32. STKMIN    EQU    512            ; minimum run-time stack size
  33. TAB    EQU    09H            ; tab character
  34. STKRSV    EQU    128            ; reserved stack size
  35.  
  36.  
  37.     extrn    LINITIO:far
  38.  
  39.  
  40. ;**
  41. ;
  42. ; Define data group
  43. ;
  44. DGROUP    GROUP    DATA,UDATA,XSTACK
  45.     ASSUME    DS:DGROUP
  46.  
  47.  
  48. ; The following routine is taken partly from the file C.ASM provided
  49. ; with the Lattice C product and does necessary initialization
  50. ; for Lattice C objects. It will only handle 'L' model programs
  51. ; so to use this and LINITIO you must compile your lattice C programs with
  52. ; the -ml option.
  53.  
  54. ; The pool size allocated in this module is by default 64k, but if
  55. ; the environment variable POOL is set then this value will be used.
  56. ; so to allocate 150k of pool size type the following to your DOS 
  57. ; prompt
  58. ;     set pool=150
  59.  
  60. CSEG    segment byte public 'CODE'
  61.  
  62.     assume    cs:CSEG,ds:DGROUP
  63.  
  64.     extrn    __GETGLOBES:near
  65.     extrn    __MFGETENV:near
  66.  
  67. installed    db    0
  68.  
  69. __NCLINKM    proc    near        ; Called from LCOBOL on startup
  70.  
  71.     test    installed,1        ; is lattice c already installed ?
  72.     jnz    nm10            ; yes -->
  73.     or    installed,1
  74.     push    ds
  75.     push    bx
  76.     push    cx
  77.     push    dx
  78.     call    LINITC            ; initialize lattice
  79.     call    LINITIO            ; initialize lattice IO library
  80.     pop    dx
  81.     pop    cx
  82.     pop    bx
  83.     pop    ds
  84. nm10:
  85.     ret
  86.  
  87. __NCLINKM    endp
  88.  
  89. __NCLINKS    proc    near        ; Called from LCOBOL on closedown
  90.  
  91.     test    installed,1        ; is lattice c already installed ?
  92.     jz    ns10            ; no -->
  93.     push    ds
  94.     mov    ax,dgroup    
  95.     mov    ds,ax
  96.     mov    es,word ptr [environ+2]    ; free allocated environment
  97.     mov    ah,49h
  98.     int    21h
  99.     mov    es,word ptr [_MBASE+2]    ; free allocated pool memory
  100.     mov    ah,49h
  101.     int    21h
  102.     pop    ds
  103. ns10:
  104.     ret
  105.  
  106. __NCLINKS    endp
  107.  
  108.  
  109. __NCLINKI  proc near
  110. __NCLINKX  proc near
  111. __NCLINKXS proc near
  112. __NCLINK   proc near
  113.            ret
  114. __NCLINK   endp
  115. __NCLINKXS endp
  116. __NCLINKX  endp
  117. __NCLINKI  endp
  118.  
  119.  
  120. ; The following routine is taken partly from the file C.ASM provided
  121. ; with the Lattice C product and does necessary initialization
  122. ; for Lattice C objects. It will only handle 'L' model programs
  123. ; so to use this and INITIO you must compile your lattice C programs with
  124. ; the -ml option.
  125.  
  126. ; The pool size allocated in this module is by default 64k, but if
  127. ; the environment variable POOL is set then this value will be used.
  128. ; so to allocate 150k of pool size type the following to your DOS 
  129. ; prompt
  130. ;     set pool=150000
  131.  
  132. LINITC    proc    near            ; Initialize lattice C
  133.  
  134.     mov    ax,DGROUP        ; ax = lattice C data segment
  135.     mov    ds,ax            ; ds = ""
  136.     call    __GETGLOBES        ; es = COBOL global data segment
  137.     mov    es:[cdataseg],ax    ; set up for native code access
  138.     mov    ah,30h            ; get DOS version
  139.     int    21h
  140.     mov    word ptr [_DOS],ax    ; DOS version number
  141.  
  142.     mov    ax,sp
  143.     mov    [_SP],ax
  144.     mov    [_SS],ss
  145.     add    ax,bpregz+pgdata+14h    ; stack fudge
  146.     mov    [_TOP],ax        ; ss relative stack top
  147.  
  148.     mov    ax,seg _PROG        ; calculate _PSIZE
  149.     mov    bx,ds
  150.     sub    bx,ax
  151.     mov    cl,4
  152.     rol    ax,cl
  153.     mov    word ptr [_PSIZE],ax
  154.     and    word ptr [_PSIZE],0fff0h
  155.     and    ax,0fh
  156.     mov    word ptr [_PSIZE+2],ax
  157.  
  158.     mov    ah,62h
  159.     int    21h            ; set up PSP address
  160.     mov    word ptr [_PSP+2],bx
  161.     mov    es,bx
  162.  
  163.     mov    ax,[_TOP]        ; calculate _TSIZE
  164.     add    ax,0fh
  165.     mov    cl,4
  166.     shr    ax,cl
  167.     mov    bx,ss
  168.     add    ax,bx            ; ax = top of program (stack is last)
  169.     mov    bx,word ptr [_PSP+2]
  170.     sub    ax,bx            ; subtract start address (PSP)
  171.     mov    [_TSIZE],ax        ; _TSIZE = total size of program
  172.  
  173.     mov    ax,es:[2ch]        ; ax = pointer to environment
  174.     mov    word ptr [_ENV+2],ax
  175.     mov    es,ax
  176.     xor    di,di            ; es:di -> environment
  177.     xor    bx,bx
  178.     xor    al,al            ; now process environment to C format
  179.     mov    cx,0ffffh        ; always find !
  180. LIC10:
  181.   repnz    scasb                ; find end of next env string
  182.     inc    bx            ; increment string count
  183.     scasb                ; end of environment ?
  184.     jnz    LIC10            ; no -->
  185.  
  186.     mov    word ptr [_ESIZE],di    ; size of environment
  187.     add    di,2
  188.     and    di,0fffeh        ; make even number
  189.     mov    word ptr [_XSIZE],di    ; size of extended env
  190.     mov    [_ENVC],bx        ; number of environment strings
  191.  
  192.     shr    bx,1            ; allocate space for env vector
  193.     shr    bx,1            ; of pointers
  194.     inc    bx            ; plus space for terminator
  195.     mov    ah,48h
  196.     int    21h
  197.     jnc    LIC20
  198.     mov    ax,offset MEMERR    ; no memory
  199.     jmp    XCABT
  200. LIC20:
  201.     xor    si,si
  202.     push    ds
  203.     mov    word ptr [environ+2],ax    ; pointer to environment struct
  204.     mov    bx,[_ENVC]
  205.     or    bx,bx            ; any env strings ?
  206.     jz    LIC40            ; no -->
  207.     les    di,[_ENV]        ; es:di points to env
  208.     mov    cx,[_ESIZE]
  209.     inc    cx
  210.     mov    ds,ax            ; ds:si points to alloc'd memory
  211.     xor    ax,ax
  212. LIC30:
  213.     mov    [si],di            ; set up ptr to next env string
  214.     mov    [si+2],es        ; in env structure
  215.     add    si,4
  216.   repnz    scasb                ; find next one
  217.     dec    bx
  218.     jnz    LIC30
  219.     mov    [si],bx            ; terminate with nulls
  220.     mov    [si+2],bx
  221.     pop    ds            ; ds = DGROUP again
  222.  
  223. LIC40:                    ; allocate space for memory pool
  224.     mov    [_STACK],800h        ; stack size
  225.     mov    ax,ds
  226.     mov    es,ax
  227.     mov    bx,offset DGROUP:str_pool ; ds:bx -> "POOL"
  228.     mov    di,offset DGROUP:str_poolsz ; es:di -> area to be returned
  229.     mov    cl,10
  230.     call    __MFGETENV        ; get env var.
  231.     jc    LIC45            ; no such name
  232.     xor    ch,ch            ; cl = length of result buffer
  233.     xor    ax,ax            ; dx;ax to contain result
  234.     xor    dx,dx
  235.     xor    bx,bx
  236.     mov    si,10            ; for multiplication by 10
  237. LIC42:
  238.     mul    si            ; size = size * 10
  239.     mov    bl,es:[di]        ; size = size + digit
  240.     sub    bl,'0'
  241.     inc    di            ; next digit
  242.     add    ax,bx
  243.     adc    dx,0
  244.     loop    LIC42
  245.     mov    si,1024            ; result is in nK
  246.     mul    si            ; ... so multiply by 1024
  247.     mov    cl,4             ; calculate num paras
  248.     ror    dx,cl            ; to allocate
  249.     shr    ax,cl
  250.     or    ax,dx
  251.     mov    poolsize,ax        ; set up poolsize
  252.  
  253. LIC45:
  254.     mov    bx,poolsize        ; allocate pool
  255.     mov    ah,48h
  256.     int    21h
  257.     jnc    LIC50
  258.     mov    ax,offset DGROUP:MEMERR    ; got memory ?
  259.     jmp    XCABT
  260.  
  261. LIC50:
  262.     mov    word ptr [_MBASE+2],ax    ; base of memory pool
  263.     mov    word ptr [_MNEXT+2],ax    ; next memory address
  264.     mov    ax,poolsize        ; set up MNEED
  265.     mov    cl,4
  266.     rol    ax,cl
  267.     mov    bx,ax
  268.     and    ah,0fh
  269.     mov    word ptr [_MNEED+2],ax
  270.     mov    word ptr [_MSIZE+2],ax
  271.     and    bx,0fff0h
  272.     mov    word ptr [_MNEED],bx
  273.     mov    word ptr [_MSIZE],bx
  274.     mov    [_STACK],800h        ; stack size
  275.  
  276. ;
  277. ; initialize 8087 numeric data processor
  278. ;
  279.     FNINIT                ; reset
  280.     FNSTSW    _NDPSW            ; get status
  281.     MOV    AX,100            ; this is just for delay
  282.     MOV    DX,AX
  283.     IMUL    DX
  284.     TEST    _NDPSW,0B8BFH        ; 8087 will reset all these
  285.     JNZ    LIC_x
  286.     INC    _NDP            ; indicate ndp present
  287.  
  288. LIC_x:
  289.     ret
  290.  
  291. LINITC    endp
  292.  
  293. ;**
  294. ;
  295. ; name        XCABT -- Ignominious abort
  296. ;
  297. ; description    This area is entered by direct jump with a message
  298. ;        pointer in DS:DX.  It sends the message to the 
  299. ;        console via DOS function 9 and then aborts.
  300. XCABT    proc    near
  301.     MOV    AH,9            ; print error message
  302.     INT    21H
  303.     MOV    ES,WORD PTR _PSP+2
  304.     MOV    AX,4C01H
  305.     INT    21H
  306. XCABT    endp
  307.  
  308. _MAIN    proc    near            ; dummy _MAIN procedure
  309.     ret
  310. _MAIN    endp
  311.  
  312. CSEG    ends
  313.  
  314.  
  315. _PROG    segment byte public 'PROG'
  316. _PROG    ends
  317.  
  318.  
  319. ;**
  320. ;
  321. ;  DGROUP includes the segments named DATA, UDATA, and XSTACK.  The startup
  322. ;  routine initializes DS to point to this group, and DS must then be pre-
  323. ;  served throughout the program.  The segments within DGROUP are defined
  324. ;  as follows:
  325. ;
  326. ;    DATA   => Contains all static (local and global) initialized items.
  327. ;    UDATA  => Contains all static (local and global) uninitialized items.
  328. ;    XSTACK => Stack for the startup routine.
  329. ;
  330. ;  During the startup routine, the initial stack (XSTACK) is replaced with
  331. ;  one that has the correct size for program execution.  This size is 
  332. ;  determined by examining the command line and the _STACK global item. Then
  333. ;  for the S and P memory models, the stack is set up relative to DGROUP (i.e.
  334. ;  stack items can addressed via DS).  For the D and L models, the stack 
  335. ;  segment stands alone and can be up to 64K bytes.
  336. ;
  337. ;  The heap (i.e. the space used by the memory allocator) resides above the
  338. ;  stack and is also initialized by the startup routine.  Any space not
  339. ;  immediately needed for the heap (as defined by _MNEED) is returned to DOS.
  340. ;  
  341. ;  At the end of the startup routine, memory is organized in the following 
  342. ;  sequence:
  343. ;
  344. ;    -- code --
  345. ;    -- DATA  --
  346. ;    -- UDATA --
  347. ;    -- stack --
  348. ;    -- heap --
  349. ;
  350. ;  FOR PROPER OPERATION OF THE STANDARD MEMORY ALLOCATOR, THIS SEQUENCE IS
  351. ;  EXTREMELY IMPORTANT.  IF YOU TAMPER WITH THE STARTUP ROUTINE OR INTRODUCE
  352. ;  SEGMENTS AND CLASSES THAT DO NOT FOLLOW LATTICE CONVENTIONS, CHECK THE
  353. ;  LOAD MAP CAREFULLY.
  354. ;
  355. ;**
  356.  
  357. ;**
  358. ;
  359. ; Initialized data 
  360. ;
  361. DATA    SEGMENT PARA PUBLIC 'DATA'
  362.     EXTRN    _STACK:WORD
  363.     EXTRN    _MNEED:DWORD
  364.     PUBLIC    _MODEL,_VER,_TOP,_BASE,_PSP,_MBASE,_MNEXT,_MSIZE,_DSIZE,_PSIZE
  365.     PUBLIC    _ENV,_DOS,_TSIZE,_ESIZE,_XSIZE,_SS,_SP,_NDP,_NDPSW,_NDPCW
  366.     PUBLIC    _FPA,_FPERR,_OSERR,_SIGFPE,_ARGV,_ARGC,_ENVC,environ,_DSP
  367.  
  368. poolsize    dw    1000h        ; pool size in paragraphs
  369. str_pool    db    "POOL",00    ; pool environment string
  370. str_poolsz    db    10 dup(' ')    ; value of pool environment string
  371.  
  372. _MODEL    DW    3
  373. _VER    DB    "LC 3.00",0
  374. _DOS    DB    0            ; DOS major version number
  375.     DB    0            ; DOS minor version number
  376. _SS    DW    0            ; stack segment number
  377. _SP    DW    0            ; SP reset value
  378. _TOP    DW    0            ; top of stack (relative to SS)
  379. _BASE    DW    OFFSET DGROUP:SBASE    ; base of stack (relative to DS)
  380. _PSP    DW    0            ; program segment prefix pointer
  381.     DW    0
  382. _DSP    DW    0            ; data segment pointer
  383.     DW    DGROUP
  384. _MBASE    DW    0            ; base of memory pool
  385.     DW    0
  386. _MNEXT    DW    0            ; next available memory location
  387.     DW    0
  388. _MSIZE    DW     0             ; number of bytes left in pool
  389.     DW    0
  390. _TSIZE    DW    0            ; total size in paragraphs
  391. _PSIZE    DD    0            ; size of program in bytes
  392. _DSIZE    DW    OFFSET DGROUP:SBASE    ; size of static data in bytes
  393.     DW    0
  394. _ARGV    DD    0            ; argument vector pointer
  395. environ    DD    0            ; environment vector pointer
  396. _ARGC    DW    0            ; argument count
  397. _ENVC    DW    0            ; environment count
  398. _ARG    DD    0            ; far pointer to original arg array
  399. _ENV    DD    0            ; far pointer to original env array
  400. _ESIZE    DW    0            ; environment size in bytes
  401. _XSIZE    DW    0            ; extended env size in bytes
  402. _FPA    DQ    0            ; floating point accumulator
  403. _FPERR    DW    0            ; floating point error code
  404. _NDP    DB    0            ; non-zero if 8087 is installed
  405. _NDPSW    DW    0FFFFH            ; 8087 status word
  406. _NDPCW    DW    0            ; 8087 control word
  407. _OSERR    DW    0            ; DOS error code
  408. _SIGFPE    DD    0            ; Floating point error signal
  409. STKERR    DB    "Invalid stack size",0DH,0AH,"$"
  410. MEMERR    DB    "Insufficient memory",0DH,0AH,"$"
  411.  
  412.  
  413. DATA    ENDS
  414.  
  415.  
  416. ;**
  417. ;
  418. ; Uninitialized data
  419. ;
  420. UDATA    SEGMENT    PARA PUBLIC 'DATA'
  421. UDATA    ENDS
  422.  
  423. ;**
  424. ;
  425. ; The stack segment is included to prevent the warning from the
  426. ; linker, and also to define the base (lowest address) of the stack.
  427. ;
  428. XSTACK    SEGMENT STACK PARA 'DATA'
  429. SBASE    DB    STKRSV DUP (?)
  430. XSTACK    ENDS
  431.  
  432.     end
  433.