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

  1.     name    MINITF
  2.     title    MINITF - Microsoft Fortran Initialization
  3.     subttl    Copyright (C) Micro Focus Ltd 1987
  4.  
  5. ; The only difference between this and the module MINITC is that
  6. ; we swap stacks at the start of the MINITF routine. This is because
  7. ; the Fortran compiler does not allow the generation of code for split
  8. ; DATA and STACK segments. This means that we must use a STACK within
  9. ; DGROUP. This is of fixed size 8K.
  10.  
  11.     public    __NCLINKM    ; these are routines called from LCOBOL
  12.     public    __NCLINKI    ; from strategic places such as
  13.     public    __NCLINKX    ; exit from a program, startup, closedown
  14.     public    __NCLINKXS    ; etc.
  15.     public    __NCLINKS
  16.     public    __NCLINK
  17.     public    __astart        
  18.     public    __chkstk
  19.     public    _main        ; dummy routine to satisfy ref in LLIBC.LIB
  20.  
  21.     public    tmpstk
  22.  
  23. ?DF=        1            ; this is special for c startup
  24. ?PLM = 0
  25. memL = 1
  26.  
  27. ; LCOBOL library equates
  28.  
  29. cdataseg    equ    0f8h
  30. bpregz        equ    0a0h
  31. pgdata        equ    68h
  32.  
  33. .xlist
  34.     include    cmacros.inc
  35.     include    msdos.inc
  36.     include    brkctl.inc
  37. .list
  38.  
  39.     extrn    __cinit:far
  40.     extrn    _exit:far
  41.     extrn    __exit:far
  42.  
  43. ; This module provides an interface for calling Microsoft objects.
  44. ; Link your program as follows
  45. ;    LINK COBOL_objects+MINITF+Fortran_objects,,,LCOBOL+Fortran_libraries/m;
  46.  
  47. page
  48. ;===========================================================================
  49. ;
  50. ;    Segment definitions
  51. ;
  52. ;    The segment order is essentially the same as in XENIX.
  53. ;
  54. ;===========================================================================
  55.  
  56. DOSSEG
  57.  
  58. createSeg    _TEXT,    code,    byte,    public, CODE,    <>
  59. createSeg    C_ETEXT,etext,    byte,    public, ENDCODE,<>
  60.  
  61. createSeg    _DATA,    data,    word,    public, DATA,    DGROUP
  62. createSeg    BSS,    data,    word,    public, BSS,    DGROUP
  63. createSeg    STACK,    stack,    para,    stack,    STACK,    DGROUP
  64.  
  65. defGrp    DGROUP            ; define DGROUP
  66.  
  67.  
  68. ; We must here set up a temporary stack since it is impossible to tell
  69. ; the Fortran compiler to allocate separate data and stack segments
  70.  
  71. _DATA    segment word public 'DATA'
  72.  
  73. tempstk    db    8192 dup (0)    ; 8k temporary stack
  74.  
  75. _DATA    ends
  76.  
  77. page
  78.  
  79. public    __acrtused        ; trick to force in startup
  80.     __acrtused = 9876h    ; funny value not easily matched in SYMDEB
  81.  
  82.  
  83. _DATA    segment
  84.  
  85. extrn    _edata:byte        ; end of data (start of bss)
  86. extrn    _end:byte
  87.  
  88. externW _psp            ; psp:0 (paragraph #)
  89. externW __argc
  90. externDP __argv
  91. externDP environ
  92.  
  93. ;    these are used by DOS C memory management (not used in Windows)
  94.  
  95. globalW _asizds,0            ; DS size (in bytes)
  96. globalW _atopsp,0            ; top of stack (heap bottom)
  97.  
  98. labelW    <PUBLIC,_abrktb>        ; segment table for brkctl
  99.     dw    ?
  100.     dw    DGROUP
  101.     db    (MAXSEG-1) * (size segrec) dup (?)
  102.  
  103. labelW    <PUBLIC,_abrktbe>
  104. globalW _abrkp,<offset DGROUP:_abrktb>
  105.  
  106. _ENV    dd    0
  107. _ENVC    dw    0
  108. _ESIZE    dw    0
  109.  
  110. tmpstk    dw    100 dup (0)        ; temporary stack for calling __cinit
  111. endstk    db    ?            ; as need ss=ds for this routine
  112.  
  113. MEMERR    DB    "Insufficient memory",0DH,0AH,"$"
  114.  
  115. _DATA    ends
  116.  
  117.  
  118. sBegin    code
  119.     assume    cs:_TEXT,ds:DGROUP,es:DGROUP
  120.  
  121. oldsp        dw    0
  122. oldss        dw    0
  123.  
  124. ; The following routine is taken partly from the file CRT0.ASM provided
  125. ; with the MSC C product and does necessary initialization
  126. ; for Microsoft C objects. It will only handle 'L' model objects
  127. ; set up with ss not = ds (i.e. compiled with /Awlf).
  128.  
  129. ; Entry: es=cobol global data segment
  130.  
  131. MINITF    proc    far            ; Initialize msc C
  132.  
  133.     ; This code assumes that COBOL is the main program
  134.     ; since it only copies one COBOL stack frame to the new stack
  135.     ; FIX when Fortran allows split Stack and DATA segments
  136.     cli                ; change over stacks
  137.     mov    dx,es            ; save CBLRUDAT pointer
  138.     mov    bx,bp            ; calculate diff betw sp and bp in bx
  139.     sub    bx,sp
  140.     mov    ax,ss
  141.     mov    ds,ax            ; ds = old stack segment
  142.     mov    ax,DGROUP
  143.     mov    es,ax            ; es = DGROUP
  144.     mov    ss,ax            ; ss = DGROUP
  145.     mov    si,sp            ; si = source stack pointer
  146.     mov    cx,bpregz+pgdata+1eh    ; stack fudge size to copy
  147.     mov    di,8192
  148.     sub    di,cx            ; di = start of stack
  149.     mov    sp,di            ; set up sp to be new stack pointer
  150.     mov    bp,di
  151.     add    bp,bx            ; bp = new stack frame pointer
  152.    rep  movsb                ; copy old stack to new stack
  153.     mov    es,dx            ; restore CBLRUDAT pointer
  154.     sti
  155.  
  156.     mov    ax,DGROUP        ; ax = msc C data segment
  157.     mov    ds,ax            ; ds = ""    ""
  158.     mov    es:[cdataseg],ax    ; set up for native code access
  159.     mov    ax,sp
  160.     add    ax,bpregz+pgdata+1eh    ; stack fudge
  161.     mov    [_atopsp],ax        ; ss relative stack top
  162.     mov    bx,seg STACK
  163.     sub    bx,DGROUP
  164.     mov    cl,4
  165.     shl    bx,cl
  166.     add    ax,bx
  167.     mov    [_abrktb].sz,ax        ; ds relative top of memory
  168.     dec    ax
  169.     mov    [_asizds],ax        ; save DS size - 1
  170.  
  171.     mov    ah,62h
  172.     int    21h            ; set up PSP address
  173.     mov    word ptr [_psp],bx
  174.  
  175.     mov    es,[_psp]
  176.     mov    ax,es:[2ch]        ; ax = pointer to environment
  177.     mov    es,ax
  178.     mov    word ptr [_ENV+2],ax
  179.     xor    di,di            ; es:di -> environment
  180.     xor    bx,bx
  181.     xor    al,al            ; now process environment to C format
  182.     mov    cx,0ffffh        ; always find !
  183. MIC10:
  184.   repnz    scasb                ; find end of next env string
  185.     inc    bx            ; increment string count
  186.     scasb                ; end of environment ?
  187.     jnz    MIC10            ; no -->
  188.  
  189.     mov    word ptr [_ESIZE],di    ; size of environment
  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    MIC20
  198.     mov    ax,offset MEMERR    ; no memory
  199.     jmp    XCABT
  200. MIC20:
  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    MIC40            ; 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. MIC30:
  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    MIC30
  219.     mov    [si],bx            ; terminate with nulls
  220.     mov    [si+2],bx
  221.     pop    ds            ; ds = DGROUP again
  222.  
  223. ; zero data area (_BSS & c_common)
  224.  
  225.     push    ds
  226.     pop    es
  227.     cld
  228.     mov    di,offset dgroup:_edata    ; beginning of BSS area
  229.     mov    cx,offset dgroup: _end    ; end of BSS area
  230.     sub    cx,di
  231.     xor    ax,ax
  232.     rep    stosb            ; zero bss
  233.  
  234.     mov    [oldsp],sp        ; save stack for later
  235.     mov    [oldss],ss
  236.     mov    ax,ds            ; therefore need local stack
  237.     mov    ss,ax            ; temporarily
  238.     mov    sp,offset DGROUP:endstk    ; __cinit needs ds = ss
  239.     call    __cinit            ; C initialization
  240.     mov    ss,[oldss]        ; restore real stack
  241.     mov    sp,[oldsp]
  242.  
  243. MIC40:
  244.     ret
  245.     
  246. MINITF    endp
  247.  
  248. __chkstk proc   far
  249.     pop    bx            ; pop off return address
  250.     pop    cx
  251.     sub    sp,ax            ; assign local stack frame
  252.     push    cx            ; push back return address
  253.     push    bx
  254.     ret
  255. __chkstk endp
  256.  
  257. _main     proc    far
  258. __astart proc    far
  259.     ret
  260. __astart endp
  261. _main     endp
  262.  
  263. page
  264. ;------------------------------------------------------------------------
  265. ;
  266. ;    Fast exit fatal errors - die quick and return (255)
  267.  
  268. labelNP <PUBLIC,_cintDIV>
  269.  
  270. labelNP <PUBLIC,_amsg_exit>
  271.     call    __exit            ; _exit(255)
  272.  
  273. ;**
  274. ;
  275. ; name        XCABT -- Ignominious abort
  276. ;
  277. ; description    This area is entered by direct jump with a message
  278. ;        pointer in DS:DX.  It sends the message to the 
  279. ;        console via DOS function 9 and then aborts.
  280. XCABT    proc    near
  281.     MOV    AH,9            ; print error message
  282.     INT    21H
  283.     MOV    ES,WORD PTR _PSP+2
  284.     MOV    AX,4C01H
  285.     INT    21H
  286. XCABT    endp
  287.  
  288. sEnd
  289.  
  290. CSEG    segment    byte public 'CODE'
  291.  
  292.     assume    cs:CSEG,ds:DGROUP
  293.  
  294.     extrn    __GETGLOBES:near
  295.  
  296. installed    db    0
  297.  
  298. __NCLINKM    proc    near        ; Called from LCOBOL on startup
  299.  
  300.     test    installed,1        ; is msc already installed ?
  301.     jnz    nm10            ; yes -->
  302.     or    installed,1
  303.     push    ds
  304.     push    bx
  305.     push    cx
  306.     push    dx
  307.     call    __GETGLOBES        ; es = COBOL global data segment
  308.     call    MINITF            ; initialize msc
  309.     pop    dx
  310.     pop    cx
  311.     pop    bx
  312.     pop    ds
  313. nm10:
  314.     ret
  315.  
  316. __NCLINKM    endp
  317.  
  318. __NCLINKS    proc    near        ; Called from LCOBOL on closedown
  319.  
  320.     test    installed,1        ; is msc already installed ?
  321.     jz    ns10            ; no -->
  322.     push    ds
  323.     push    ax            ; push return code
  324.     mov    ax,DGROUP
  325.     mov    ds,ax
  326.     mov    es,word ptr [environ+2]    ; free allocated environment
  327.     mov    ah,49h
  328.     int    21h
  329.     call    _exit            ; terminate C routines
  330.     pop    ds
  331. ns10:
  332.     ret
  333.  
  334. __NCLINKS    endp
  335.  
  336.  
  337. __NCLINKI  proc near
  338. __NCLINKX  proc near
  339. __NCLINKXS proc near
  340. __NCLINK   proc near
  341.            ret
  342. __NCLINK   endp
  343. __NCLINKXS endp
  344. __NCLINKX  endp
  345. __NCLINKI  endp
  346.  
  347. CSEG    ends
  348.  
  349.     end
  350.