home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / t_power / getmem.asm < prev    next >
Assembly Source File  |  1988-09-29  |  12KB  |  274 lines

  1. ;*******************************************************
  2. ;                    GETMEM.ASM 5.00
  3. ;        Copyright (c) TurboPower Software 1987.
  4. ;                 All rights reserved.
  5. ;*******************************************************
  6.  
  7. DSEG    SEGMENT WORD PUBLIC
  8.  
  9.         EXTRN   PrefixSeg:WORD                  ;Variables in SYSTEM
  10.         EXTRN   FreePtr:DWORD
  11.         EXTRN   HeapPtr:DWORD
  12.         EXTRN   HeapOrg:DWORD
  13.  
  14. DSEG    ENDS
  15.  
  16. CSEG    SEGMENT BYTE PUBLIC
  17.  
  18.         ASSUME  CS:CSEG,DS:DSEG
  19.  
  20.         PUBLIC  SetMaxHeap
  21.         PUBLIC  GetMemDos
  22.         PUBLIC  Pointer2String
  23.         PUBLIC  String2Pointer
  24.  
  25. ofst    EQU     (WORD PTR 0)
  26. segm    EQU     (WORD PTR 2)
  27.  
  28. ;*********************************************************** SetMaxHeap
  29. ;  procedure SetMaxHeap(Bytes : LongInt);
  30. ;    Set maximum heap and adjust DOS memory allocation block
  31.  
  32. SetMaxHeap PROC FAR
  33.         MOV     BX,SP
  34.         LES     AX,SS:[BX+4]
  35.         MOV     DX,ES                           ;DX:AX = bytes we want
  36.         ADD     AX,0Fh                          ;Add 15 bytes to round up
  37.         ADC     DX,0
  38.         MOV     CX,4                            ;Divide bytes by 16
  39. SetMaxR:
  40.         SHR     DX,1                            ;Shift a LongInt right
  41.         RCR     AX,1
  42.         LOOP    SetMaxR
  43.         OR      DX,DX                           ;More than 640K?
  44.         JNZ     SetMaxDone                      ;Jump if so
  45.  
  46.         MOV     ES,[PrefixSeg]                  ;ES = base of program
  47.         MOV     DX,ES:[0002h]                   ;DX = Top of memory
  48.         MOV     BX,HeapOrg.segm                 ;BX = base of heap
  49.         SUB     DX,BX                           ;DX = paragraphs of heap now
  50.         CMP     DX,AX                           ;Space left to give away?
  51.         JBE     SetMaxDone                      ;Jump if not
  52.  
  53.         MOV     DX,ES                           ;DX = base of program
  54.         SUB     BX,DX                           ;BX = paras to base of heap
  55.         ADD     BX,AX                           ;BX = paras we want
  56.         MOV     AH,4Ah
  57.         INT     21h                             ;DOS SetBlock
  58.         JC      SetMaxDone                      ;Jump if error
  59.  
  60.         MOV     AX,ES
  61.         ADD     AX,BX                           ;AX = top of program segment
  62.         MOV     ES:[0002h],AX                   ;Store in PSP
  63.         SUB     AX,1000h                        ;FreePtr segment
  64.         MOV     FreePtr.segm,AX                 ;Store new FreePtr
  65.         XOR     AX,AX
  66.         MOV     FreePtr.ofst,AX
  67.  
  68. SetMaxDone:
  69.         RET     4
  70. SetMaxHeap ENDP
  71.  
  72. ;*********************************************************** Pointer2String
  73. ;   function Pointer2String(P : pointer) : string;
  74.  
  75. Pointer2String PROC FAR
  76.         MOV     BX,SP                           ;Set up stack frame
  77.         PUSH    DS                              ;Save DS
  78.         PUSH    SS
  79.         POP     DS                              ;DS = SS
  80.         LEA     SI,DWORD PTR SS:[BX+4]          ;DS:SI => P parameter
  81.         LES     DI,DWORD PTR SS:[BX+8]          ;ES:DI => Result
  82.         CLD
  83.         MOV     CX,8                            ;Eight bytes in string
  84.         MOV     AL,CL
  85.         STOSB                                   ;Store length byte
  86.         SHR     CX,1                            ;Four input bytes
  87.         MOV     DX,0F30h                        ;DH/DL are constants
  88.  
  89. P2SNext:
  90.         LODSB                                   ;Next byte
  91.         MOV     AH,AL                           ;Save it
  92.         AND     AL,DH                           ;Low nibble
  93.         ADD     AL,DL                           ;Bias into ASCII
  94.         STOSB                                   ;Store in string
  95.         MOV     AL,AH                           ;Restore byte
  96.         SHR     AL,1
  97.         SHR     AL,1
  98.         SHR     AL,1
  99.         SHR     AL,1                            ;High nibble
  100.         ADD     AL,DL
  101.         STOSB                                   ;Store in string
  102.         LOOP    P2SNext                         ;Do four bytes
  103.  
  104.         POP     DS                              ;Restore DS
  105.         RET     4                               ;Remove parameter and return
  106. Pointer2String ENDP
  107.  
  108. ;*********************************************************** String2Pointer
  109. ;   function String2Pointer(S : string) : pointer;
  110.  
  111. ;Parameters and locals
  112. S               EQU     DWORD PTR [BP+6]
  113. ResultHigh      EQU     WORD PTR [BP-2]
  114. ResultLow       EQU     WORD PTR [BP-4]
  115. Result          EQU     BYTE PTR [BP-4]
  116.  
  117. String2Pointer PROC FAR
  118.         PUSH    BP
  119.         MOV     BP,SP
  120.         SUB     SP,4                             ;Space for locals
  121.         PUSH    DS                               ;Save DS
  122.  
  123.         LDS     SI,S                             ;DS:SI => S
  124.         CLD                                      ;Forward
  125.         XOR     CX,CX                            ;Prepare for error
  126.         LODSB                                    ;AL = length byte
  127.         CMP     AL,8                             ;Proper length?
  128.         MOV     DX,CX                            ;Nil in DX
  129.         JNZ     S2PDone                          ;Wrong length, return nil
  130.  
  131.         PUSH    SS
  132.         POP     ES
  133.         LEA     DI,Result                        ;ES:DI => Result
  134.         MOV     CX,4                             ;4 words in string
  135.         MOV     DL,'0'                           ;Constant
  136.  
  137. S2PNext:
  138.         LODSW                                    ;First two bytes in AX
  139.         SUB     AH,DL                            ;Debias ASCII
  140.         SHL     AH,1
  141.         SHL     AH,1
  142.         SHL     AH,1
  143.         SHL     AH,1
  144.         SUB     AL,DL                            ;Debias ASCII
  145.         OR      AL,AH                            ;Put bytes back together
  146.         STOSB                                    ;Store in result
  147.         LOOP    S2PNext
  148.  
  149.         MOV     CX,ResultLow                     ;Get result in registers
  150.         MOV     DX,ResultHigh
  151.  
  152. S2PDone:
  153.         MOV     AX,CX                            ;Store low word
  154.         POP     DS                               ;Restore DS
  155.         MOV     SP,BP
  156.         POP     BP
  157.         RET     4                                ;Remove parameter and return
  158. String2Pointer ENDP
  159.  
  160. ;**************************************************************** GetMemDos
  161. ;  procedure GetMemDos(var P : pointer; Bytes : longint);
  162.  
  163. ;Parameters
  164. P               EQU     DWORD PTR [BP+10]       ;Pointer to initialize
  165. Bytes           EQU     DWORD PTR [BP+6]        ;Bytes to allocate
  166.  
  167. GetMemDos PROC    FAR
  168.  
  169.         PUSH    BP
  170.         MOV     BP,SP                           ;Set up stack frame
  171.  
  172. ;Get requested bytes and convert to paragraphs
  173.         LES     AX,Bytes                        ;Get the bytes longint
  174.         MOV     DX,ES                           ;DX:AX has bytes to allocate
  175.         ADD     AX,0Fh                          ;Add 15 bytes to round up
  176.         ADC     DX,0
  177.         MOV     CX,4                            ;Divide bytes by 16
  178. NextShiftR:
  179.         SHR     DX,1                            ;Shift a LongInt right
  180.         RCR     AX,1
  181.         LOOP    NextShiftR
  182.         OR      DX,DX                           ;Requesting more than 640K?
  183.         JNZ     Error                           ;Error
  184.  
  185. ;Try to get memory from DOS without affecting this process
  186.         MOV     BX,AX                           ;Paragraphs to request
  187.         INC     AX                              ;Ask for extra paragraph later
  188.         MOV     DI,AX                           ;DI = Paragraphs+1
  189.         MOV     AH,48h                          ;Allocate memory
  190.         INT     21h
  191.         JNC     Store                           ;Success if no carry
  192.  
  193. ;See if moving the free list down will fit the request
  194. ;Ignore the DOS free block since it may not be contiguous with the Turbo heap
  195.         LES     AX,FreePtr                      ;ES:AX = FreePtr
  196.         MOV     DX,ES                           ;DX:AX = FreePtr
  197.         PUSH    DX                              ;Save FreeSeg
  198.         PUSH    AX                              ;Save FreeOfs
  199.         OR      AX,AX                           ;FreeList empty?
  200.         JNZ     FreeListNotEmpty
  201.         MOV     AX,1000h                        ;Empty FreeList => +64K free
  202.         JMP SHORT GetFreeSpace
  203. FreeListNotEmpty:
  204.         MOV     CL,4
  205.         SHR     AX,CL                           ;Convert FreeOfs to paras
  206. GetFreeSpace:
  207.         ADD     DX,AX                           ;DX = segment of
  208.                                                 ;     lowest free list entry
  209.         LES     BX,HeapPtr                      ;ES:BX = HeapPtr
  210.         MOV     AX,ES                           ;AX is seg(HeapPtr^)
  211.         INC     AX                              ;Ignore up to 16 bytes
  212.         SUB     DX,AX                           ;DX = paragraphs free at
  213.                                                 ;  top of heap
  214.         CMP     DX,DI                           ;Is free amount >= request?
  215.         JB      Error                           ;No, error
  216.  
  217. ;Move free list down into free heap
  218.         POP     SI                              ;SI = FreeOfs
  219.         POP     DX                              ;DX = FreeSeg
  220.         PUSH    DI                              ;Save Paragraphs+1
  221.         MOV     CX,DX                           ;CX = FreeSeg
  222.         SUB     CX,DI                           ;CX = seg of new free list
  223.         MOV     ES,CX                           ;ES = seg of new free list
  224.         OR      SI,SI                           ;Is FreeList empty?
  225.         JZ      StoreFreePtr                    ;Yes, skip the move
  226.  
  227.         PUSH    DS                              ;Save DS
  228.         MOV     DS,DX                           ;DS:SI => old free list
  229.         CLD                                     ;Forward avoids overwrite
  230.         MOV     DI,SI                           ;ES:DI => new free list
  231.         MOV     CX,SI                           ;CX = FreeOfs
  232.         NEG     CX                              ;CX = 10000h-FreeOfs
  233.         SHR     CX,1                            ;Always even number of bytes
  234.         REP     MOVSW                           ;Copy the free list
  235.         POP     DS                              ;Restore DS
  236.  
  237. ;Store the new free list pointer
  238. StoreFreePtr:
  239.         POP     DI                              ;Restore Paragraphs+1
  240.         MOV     WORD PTR [FreePtr+2],ES         ;Store new FreeSeg
  241.  
  242. ;Shrink memory block for this process
  243.         MOV     BX,ES                           ;BX = NewFreeSeg
  244.         ADD     BX,1000h                        ;BX = Top of NewFreeSeg
  245.         MOV     AX,[PrefixSeg]                  ;AX = base segment of process
  246.         SUB     BX,AX                           ;BX = paras to keep
  247.         MOV     ES,AX                           ;ES = segment of memory block
  248.         MOV     AH,4Ah                          ;DOS SetBlock
  249.         INT     21h
  250.         JC      Error                           ;Error
  251.  
  252. ;Try to get the requested block again
  253. Get2:   MOV     BX,DI                           ;BX = Paragraphs+1
  254.         DEC     BX                              ;BX = Paragraphs
  255.         MOV     AH,48h                          ;Allocate memory
  256.         INT     21h
  257.         JNC     Store                           ;Success if no carry
  258.  
  259. Error:  XOR     AX,AX                           ;Error, return nil
  260.  
  261. Store:  LES     DI,P                            ;ES:DI => result pointer
  262.         MOV     WORD PTR ES:[DI],0              ;Store the return pointer
  263.         MOV     ES:[DI+2],AX                    ;Segment in AX
  264.  
  265.         MOV     SP,BP
  266.         POP     BP
  267.         RET     8
  268.  
  269. GetMemDos ENDP
  270.  
  271. CSEG    ENDS
  272.  
  273.         END
  274.