TIP: Click on subject to list as thread! ANSI
echo: power_bas
to: ALL
from: DAVE NAVARRO
date: 1995-05-16 16:58:00
subject: Swapping whole Arrays 1/2

'  The following example shows how useful ArrayInfo() is.
'
'  It will allow you to completely swap two arrays of like type and
'  element size.
'
'  Swapping each element individually is slow, and all we really need to
'  do is swap the array descriptors.
'
'  Use at your own risk!
'
'  Donated to the Public Domain
'  by Dave Navarro, Jr.
%TotalElements   = 1
%ElementSize     = 2
%ElementsPerPage = 3
%ArrayType       = 4
%SubscriptCount  = 5
DEFINT A-Z
DECLARE FUNCTION ArrayInfo(BYVAL Code%, BYVAL Pointr AS DWORD) AS LONG
DIM X(1:5)
DIM Y(1:5)
X(1) = 200            'assign a value to the first element of X
PRINT X(1), Y(1)      'print the value of X(1) and Y(1)
ArraySwap X(), Y()    'Do NOT use element numbers.  Empty parenthesis
                      ' tell PowerBASIC to pass the array descriptor.
PRINT X(1), Y(1)      'print the value of X(1) and Y(2), should be swapped
END
SUB ArraySwap(ANY,ANY) PUBLIC
  DIM Pointer1 AS DWORD
  DIM Pointer2 AS DWORD
  DIM Temp AS STRING * 64
  ! push DS
  ! les  DI, [BP+10]           ;get pointer to first array
  ! mov  Pointer1[0], DI       ;store offset in Pointer1
  ! mov  Pointer1[2], ES       ;store segment in Pointer1
  ! les  DI, [BP+6]            ;get pointer to second array
  ! mov  Pointer2[0], DI       ;store offset in Pointer2
  ! mov  Pointer2[2], ES       ;store segment in Pointer2
  ! pop  DS
  'If either array is not dimensioned, then exit
  IF (ArrayInfo(%TotalElements, Pointer1) * _
      ArrayInfo(%TotalElements, Pointer2)) = 0 THEN
    EXIT SUB
  END IF
  'If both array types match, swap the descriptors
  ' make sure the element sizes are the same in case of fixed length
  IF (ArrayInfo(%ArrayType, Pointer1) =    _
      ArrayInfo(%ArrayType, Pointer2)) AND _
     (ArrayInfo(%ElementSize, Pointer1) =  _
      ArrayInfo(%ElementSize, Pointer2)) THEN
     ! push DS
     ! lds  SI, [BP+6]         ;point to second array
     ! lea  BX, Temp           ;point BX at Temp buffer
     ! mov  DI, BX             ;put offset in DI
     ! push DI                 ;save offset for later
     ! push SS                 ;save segment for later
     ! push SS                 ;push stack segment on the stack
     ! pop  ES                 ;and put it in ES
     ! mov  CX, 32             ;descriptor is 32 words long
     ! rep  movsw              ;copy descriptor to temp buffer
     ! lds  SI, [BP+10]        ;point to first array
     ! les  DI, [BP+6]         ;point to second array
     ! mov  CX, 32             ;descriptor is 32 words long
     ! rep  movsw              ;copy first descriptor to second descriptor
     ! les  DI, [BP+10]        ;point to first array
     ! pop  DS                 ;restore segment of Temp buffer
     ! pop  SI                 ;restore offset of Temp buffer
     ! mov  CX, 32             ;descriptor is 32 words long
     ! rep  movsw              ;copy second descriptor to first descriptor
     ! pop  DS
(Continued to next message)
---
 * QMPro 1.53 * Let's face it, I'm right and you're not convinced!
--- WILDMAIL!/WC v4.12 
---------------
* Origin: Toast House * (314) 994-0312 * (1:100/560.0)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.