vbdpss.hlp (Table of Contents; Topic list)
Article Q44409
                                                 Contents  Index  Back
─────────────────────────────────────────────────────────────────────────────
                           Knowledge Base Contents  Knowledge Base Index
 
 Passing Dynamic Array of User-Defined TYPE from Basic to MASM - Q44409
 
 This article contains a code example of passing a dynamic array of
 user-defined TYPEs to an assembly routine that copies the array to
 another array that is passed back to Basic.
 
 More Information:
 
 The arrays being passed can be larger than 64K when the Basic program
 is compiled with the BC /AH option (or if the VBDOS.EXE editor is
 started with the /AH option).
 
 Code Example
 ------------
 
 ' Please note that it is necessary to pass the segment and the offset
 ' of a dynamic array being passed to assembly using VARSEG and VARPTR
 ' respectively.
 
 ' The following Basic program passes two arrays to a routine called
 ' MASM. The MASM routine copies the first array to the second array,
 ' passing it back to Basic through a parameter.
 
 ' To try this example in VBDOS.EXE:
 ' 1. From the File menu, choose New Project.
 ' 2. Copy the code example to the Code window.
 ' 3. Press F5 to run the program.
 
 REM $DYNAMIC
 
 TYPE testType
   StrElem AS STRING * 11
   NumElem AS INTEGER        ' Size of integer is 2.
 END TYPE
 
 ' The underscore characters used below are for readability purposes
 ' only, and must be removed for use with BC.EXE and VBDOS.EXE.
 
 DECLARE SUB Masm (
        BYVAL TypeLength AS INTEGER,_
        BYVAL Length AS INTEGER,_
        BYVAL SegAddr1 AS INTEGER,_
        BYVAL Addr1 AS INTEGER,_
        BYVAL SegAddr2 AS INTEGER,_
        BYVAL Addr2 AS INTEGER)
 Size% = 20%     ' Size of the array (# of elements).
 TypeSize% = 13%  ' Size of TYPEs stored in array.
 CLS
 DIM inArray(1 TO Size%) AS testType
 DIM outArray(1 TO Size%) AS testType
 
 ' Load inArray with a 11 character string " *inArray* " and index:
 FOR i = 1 TO Size%
   inArray(i).StrElem = " *inArray* "
   inArray(i).NumElem = i
 NEXT i
 
 ' The underscore characters used below are for readability purposes
 ' only, and must be removed for use with BC.EXE and VBDOS.EXE.
 
 ' Masm will copy the contents of inArray to outArray:
 CALL Masm(TypeSize%,_
           Size%,_
           VARSEG(inArray(1)),_
           VARPTR(inArray(1)),_
           VARSEG(outArray(1)),_
           VARPTR(outArray(1)))
 
 ' Print the inArray:
 PRINT
 PRINT
 PRINT "inArray: "
 FOR i = 1 TO Size%
   PRINT inArray(i).StrElem; inArray(i).NumElem
 NEXT i
 
 ' Print the outArray to see that the contents of inArray
 ' were copied to it:
 
 PRINT
 PRINT "outArray: "
 FOR i = 1 TO Size%
   PRINT outArray(i).StrElem; outArray(i).NumElem
 NEXT i
 END
 
 The Assembly Routine That Copies the Array
 ------------------------------------------
 
 ;***********************************************************
 ; The routine 'Masm' copies a dynamic TYPE array of any
 ;   length to another TYPE array.
 ; Warning:
 ;   -- Arrays must be adequately and equally dimensioned.
 ; Masm takes six parameters from the BASIC routine:
 ;   1 - Size of TYPEs in array to be copied (BX).
 ;   2 - # of elements in Array.
 ;   3 - Segment of source array.
 ;   4 - Offset of first element of source array.
 ;   5 - Segment of destination array.
 ;   6 - Offset of first element of destination array.
 ;***********************************************************
 
 .MODEL MEDIUM
 .CODE
 PUBLIC Masm
 
 Masm    PROC
      push   bp
      mov    bp, sp
 
      mov    bx, [bp+16]   ; Size of TYPEs in array -> BX.
      mov    ax, [bp+14]   ; Elements in array -> AX.
      mul    bx     ; Multiply AX by BX and put answer in AX.
      mov    cx,ax  ; Number of bytes in array -> CX.
 
      mov    es, [bp+12]   ; Segment of first array (inArray).
      mov    bx, [bp+10]   ; Offset of first element in first
                           ; array.
 ; body
      mov    si,0 ; Initialize first array index (inArray).
 again:
      mov    al,es:[bx] ; Load byte to copy to second array
                        ;  in AL.
      push   bx         ; save BX.
      push   es         ; save ES.
      mov    es, [bp+8] ; Segment of second array (outArray).
      mov    bx, [bp+6] ; Offset of second arrays first
                        ; element.
      add    bx,si ; Get correct offset into second array from index.
      mov    es:[bx],al ; Move the byte into the second array.
      pop    es ; Restore ES.
      pop    bx ; Restore BX.
      add    bx,1 ; Point to next element in first array (inArray).
      add    si,1 ; Increment second array (outArray) index.
      loop    again ; Loop until CX is 0.
 
      pop    bp
      ret
 Masm ENDP
      END