Annotation of os2sdk/startup/dos/stdenvp.asm, revision 1.1.1.1

1.1       root        1:        TITLE   stdenvp - standard _setenvp routine
                      2: ;***
                      3: ;stdenvp.asm - standard _setenvp routine
                      4: ;
                      5: ;      Copyright (c) 1985-1987, Microsoft Corporation.  All rights reserved.
                      6: ;
                      7: ;Purpose:
                      8: ;      Standard _setenvp routine.
                      9: ;
                     10: ;*******************************************************************************
                     11: 
                     12: 
                     13: include        version.inc
                     14: .xlist
                     15: include        cmacros.inc
                     16: include        msdos.inc
                     17: .list
                     18: 
                     19: 
                     20: sBegin data
                     21: assumes        ds,data
                     22: externW        _psp                    ; PSP segment #
                     23: externDP environ               ; environment pointer
                     24: 
                     25: sEnd   data
                     26: 
                     27: sBegin code
                     28: assumes        ds,data
                     29: assumes        cs,code
                     30: 
                     31: externNP _myalloc              ; allocation routine for arguments
                     32: 
                     33: page
                     34: ;***
                     35: ;_setenvp - standard routine to set envp for C programs.
                     36: ;
                     37: ;Purpose:
                     38: ;      Reads the environment and build the envp array.
                     39: ;
                     40: ;Entry:
                     41: ;
                     42: ;Exit:
                     43: ;
                     44: ;Uses:
                     45: ;
                     46: ;Exceptions:
                     47: ;
                     48: ;*******************************************************************************
                     49: 
                     50: cProc  _setenvp,<PUBLIC>,<>
                     51: 
                     52: cBegin
                     53:        push    bp
                     54:        mov     ds,[_psp]       ; get psp segment
                     55: 
                     56: assumes        ds,nothing
                     57: 
                     58: ;      setup envp vector and move environment to heap
                     59: 
                     60: ;      DS = PSP, SS = DGROUP
                     61: 
                     62:        xor     cx,cx           ; cx = 0
                     63:        mov     ax,cx           ; ax = 0 (search byte)
                     64:        mov     bp,cx           ; bp = 0 (envp count)
                     65:        mov     di,cx           ; di = 0 (initial offset)
                     66:        dec     cx              ; cx = ffff (inifinite count)
                     67: 
                     68:        mov     si,word ptr DS:[DOS_ENVP]
                     69:        or      si,si           ; test for null envp
                     70: 
                     71:        jz      noenv           ;   none
                     72:        mov     es,si           ; es:di = environment table
                     73: scanenv:
                     74:        repnz   scasb
                     75:        inc     bp              ; bp = envp count
                     76:        scasb
                     77:        jnz     scanenv
                     78: 
                     79: noenv:
                     80:        inc     bp              ; bp = envp count + 1
                     81:        xchg    ax,di           ; ax = envlen
                     82:        inc     ax
                     83:        and     al,not 1        ; round up to even
                     84:        mov     di,bp           ; di = argument count
                     85:        shl     bp,1            ; argument count*2
                     86: if     sizeD
                     87:        shl     bp,1            ; argument count*2
                     88: endif
                     89:        add     ax,bp           ; ax = space to allocate
                     90:        push    ss
                     91:        pop     ds              ; restore DS
                     92: 
                     93: assumes        ds,data
                     94: 
                     95: ; Make sure there is enough memory for the environment.
                     96: ; If there is not enough, try to get some from the OS if possible.
                     97: ; Register usage at this point:
                     98: ;      ax = total # bytes needed for environment ptr table and strings
                     99: ;      di = # of entries in env ptr table = # of env args + 1
                    100: ;      bp = # bytes required to store env ptr table
                    101: ;      ss = ds = DGROUP
                    102: ;      es = PSP segment
                    103: 
                    104:        push    di              ; save # of env args + 1
                    105:        mov     di,9            ; error message in case of death.
                    106:        call    _myalloc        ; takes bp = # bytes for ptr table ax = total #
                    107:                                ; bytes
                    108:        pop     di              ; recover # of env args + 1
                    109: 
                    110: 
                    111: ; Now there is enough memory for the environment, so copy it in.
                    112: ; Register usage at this point:
                    113: ;      dx = size of environment in bytes
                    114: ;      di = # of entries in env ptr table = # of env args + 1
                    115: ;      bp points to env table
                    116: ;      ax is size of env table in bytes.
                    117: ;      ss = ds = DGROUP
                    118: ;      es = PSP segment
                    119: 
                    120: 
                    121:        mov     cx,di           ; cx = envcnt
                    122:        mov     di,bp
                    123:        add     di,ax           ; di points past env ptr table
                    124:                                ; store strings there
                    125:        mov     word ptr [environ],bp ; save envp[]
                    126: if     sizeD
                    127:        mov     word ptr [environ+2],ds ; segment address for large model
                    128: endif
                    129: 
                    130:        push    ds
                    131:        pop     es              ; es:di = env dest
                    132:        mov     ds,si           ; ds:0 = env table
                    133:        xor     si,si
                    134: 
                    135: ; SS = ES = DGROUP
                    136: ; DS = PSP segment
                    137: 
                    138: assumes        es,data
                    139: assumes        ds,nothing
                    140: 
                    141:        dec     cx              ; adjust for the last entry of 0000
                    142:        jcxz    envdone         ;   done - no environment
                    143: 
                    144: envloop:
                    145:        cmp     word ptr [si],';'+'C'*100h ; test if starts with ';C'
                    146:        je      envstr          ; don't save pointer if ';C'
                    147: 
                    148:        mov     [bp],di         ; save pointer to start of string
                    149: if     sizeD
                    150:        mov     [bp+2],es       ; segment address for large model
                    151:        add     bp,4
                    152: else
                    153:        inc     bp
                    154:        inc     bp
                    155: endif
                    156: 
                    157: envstr:
                    158:        lodsb
                    159:        stosb
                    160:        or      al,al
                    161:        jnz     envstr          ; keep copying string
                    162:        loop    envloop         ; more strings to copy
                    163: 
                    164: envdone:
                    165:        mov     [bp],cx         ; zero terminate envp table
                    166: if     sizeD
                    167:        mov     [bp+2],cx       ; extra zero for large model
                    168: endif
                    169:        push    ss              ; DS = DGROUP
                    170:        pop     ds
                    171:        pop     bp
                    172: cEnd
                    173: 
                    174: sEnd   code
                    175: 
                    176:        end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.