Passing dynamic strings

A dynamic string variable is defined as a 32-bit data item, which contains a pointer (or offset) to the string characters.  When passed by value, the parameter is actually a 32-bit offset of the data.  When passed by reference or by copy, the parameter is a pointer to another pointer that contains the offset of the actual string data.  A dynamic string passed by reference is usually accessed in this way:

SUB MyProc(abc$)

! PUSH EBX

! MOV  EBX, abc$   ; EBX is a pointer to the string handle

! MOV  EBX, [EBX]  ; EBX is now a pointer to string data

! MOV  AL, [EBX]   ; AL contains the 1st char of the string

' more code could go here

! POP  EBX

END SUB

If you need to determine the current length of a dynamic string, there are two ways to do so.  The end of string is always followed by a nul, CHR$(0), so it is possible to scan the string for the first occurrence.  Of course, this will only work if there are no embedded nul bytes in the string data.  An alternative method is to read the 32-bit Long-integer that immediately precedes the start of the string data, as the current length is always stored there.

PowerBASIC also calculates string literals in reverse order, in keeping with standard assembler operation.  For example:

FUNCTION ab(x???) AS DWORD

  ! PUSH EBX

  ! MOV  EBX, x???

  ! MOV  DWORD PTR [EBX], "ABCD"

  ! POP  EBX

END FUNCTION

The above code stores the value &H41424344 in the DWORD variable x, passed BYREF from the calling code.  However, since the Intel platform uses little-endian numeric data format, the actual bytes are written to memory in the reverse order.  For example, if we were to call the code above, and examine the actual memory locations of the passed parameter after the function call, we can see the effect of the reverse memory storage:

DIM a AS STRING, x AS DWORD

CALL ab(x)

a = HEX$(x,8)          ' a = "41424344"

a = PEEK$(VARPTR(x),4) ' a = "DCBA"

 

See Also

The Inline Assembler

Passing parameters

Parameters passed by reference or by copy

Parameters passed by value

Passing dynamic strings

Accessing PowerBASIC variables by name