home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / dba1186.zip / ALLOCATE next >
Text File  |  1986-10-06  |  4KB  |  139 lines

  1.  
  2.          ; Function must be declared PUBLIC so the linker
  3.          ; can find it
  4.  
  5.          PUBLIC   ALLOCATE
  6.  
  7.  
  8.          ; EXTRN declarations for Clipper internal routines
  9.          ; and MACRO definitions
  10.  
  11.          INCLUDE  EXTENDA.MAC
  12.  
  13.  
  14.          ; Low-level Clip-On routines which do the actual work
  15.  
  16.          EXTRN    _CO_HEXASM:FAR    ; Converts number to
  17.                                     ; hexadecimal string
  18.          EXTRN    _CO_ALLOCMEM:FAR  ; Allocates memory
  19.          EXTRN    _CO_FREEMEM:FAR   ; Releases memory
  20.  
  21.  
  22. ;*******************************************
  23. CODESEG  SEGMENT  BYTE 'CODE'
  24.          ASSUME   CS:CODESEG
  25.  
  26. NULLSTR  DB       0                 ; Null string for error return
  27.  
  28. ;-------------------------------------------
  29. ;
  30. ; This procedure is a memory allocator
  31. ; callable from Clipper.
  32. ;
  33. ;-------------------------------------------
  34. ;
  35. ;     SYNTAX:  memvar = ALLOCATE( <expN> )
  36. ;
  37. ; PARAMETERS:  <expN> = number of bytes to allocate
  38. ;
  39. ;    RETURNS:  segment address of allocated memory
  40. ;              as a hexadecimal string
  41. ;
  42. ;              offset address is 0 (not returned)
  43. ;
  44. ;--------------------
  45. ALLOCATE PROC     FAR
  46.          PUSH     BP            ; Save Clipper's registers
  47.          MOV      BP,SP
  48.          PUSH     DS
  49.          PUSH     ES
  50.  
  51.  
  52.          ; Step 1.
  53.  
  54.          ; Make sure at least one parameter has been passed
  55.          ; by calling _PARINFO
  56.  
  57.          GET_PCOUNT               ; PCOUNT returned in AX
  58.          OR       AX,AX           ; Is PCOUNT 0?
  59.          JZ       ALLOC_ERR       ; Yes, so return error
  60.  
  61.          ; Step 2.
  62.  
  63.          ; PCOUNT greater than zero, so get parameter 1
  64.          ; as long integer in AX:BX
  65.  
  66.          GET_LONG 1               ; get parameter in AX:BX
  67.  
  68.          ; Step 3.
  69.  
  70.          ; Parameters require no further processing, so
  71.          ; we move on to Step 4.
  72.  
  73.          ; Step 4.
  74.  
  75.          ; Pass parameters to work routine (_CO_ALLOCMEM).
  76.  
  77.          PUSH     AX
  78.          PUSH     BX
  79.  
  80.          ;  parameters for _CO_ALLOCMEM are now on the top of the stack
  81.  
  82.          CALL     _CO_ALLOCMEM    ; returns pointer in AX:BX
  83.          ADD      SP,4
  84.  
  85.          ; Step 5.
  86.  
  87.          ; Receive and evaluate results from low-level routine.
  88.  
  89.          OR       AX,AX           ; was null pointer returned?
  90.          JZ       ALLOC_ERR       ; yes, return error
  91.  
  92.          ; Step 6.
  93.  
  94.          ; Convert segment address in AX to hexadecimal string
  95.          ; which we will return to Clipper.
  96.  
  97.          ; Here we call another low-level routine, _CO_HEXASM,
  98.          ; which returns a pointer to a hexadecimal string.
  99.  
  100.          XOR      BX,BX
  101.          PUSH     BX              ; PUSH a word of zeroes
  102.          PUSH     AX              ; push segment for _CO_HEXASM
  103.          CALL     _CO_HEXASM      ; AX:BX now has address
  104.                                   ; of hex string to return
  105.          ADD      SP,4
  106.          JMP      SHORT ALLOC_EXIT
  107.  
  108.  
  109. ALLOC_ERR:
  110.          ; Error return--return address of NULLSTR,
  111.          ; defined above
  112.  
  113.          MOV      BX,OFFSET CS:NULLSTR     ; return NULL pointer
  114.          MOV      AX,CS
  115.  
  116. ALLOC_EXIT:
  117.  
  118.          ; Step 7.
  119.  
  120.          ; Return results to Clipper, but first--
  121.  
  122.          ; RESTORE CLIPPER'S REGISTERS!
  123.  
  124.          POP      ES
  125.          POP      DS
  126.          POP      BP
  127.  
  128.          ; Now return the value
  129.  
  130.          RET_CHAR AX,BX           ; return char pointer
  131.          RET
  132.  
  133. ALLOCATE ENDP
  134. ;---------------------------------------------
  135. CODESEG  ENDS
  136. ;************************************************
  137.          END
  138.  
  139.