Parameters passed by reference or by copy

When a parameter is passed by reference (the default method), PowerBASIC passes a 32-bit pointer on the stack.  That pointer is the actual 32-bit offset, or memory location, of the variable to be utilized as a parameter.  A 32-bit pointer occupies exactly four bytes of stack space.  A parameter passed by reference is typically accessed in this way:

SUB MyProc(xyz&)   ' This will increment the

                   ' parameter variable by one

  ! PUSH EBX

  ! MOV  EBX, xyz&

  ! INC  DWORD PTR [EBX]

  ! POP  EBX

END SUB

Parameters passed by copy (such as expressions or constants) also take precisely 4 bytes on the stack.  In this case, just as in parameters by reference, the item on the stack is not the value of the parameter.  Rather, it is the address of a temporary location in memory where the value is stored.  This may seem roundabout, but it has two distinct advantages.  First, assembler routines can handle parameters BYREF and BYCOPY in precisely the same way.  Second, routines can modify the value of a parameter without altering the original variable in the main program.

Suppose the first and only parameter is a Long-integer.  In that case, you can put the integral value into the ECX register by writing:

SUB MySub(xyz&)

  ! PUSH EBX

  ! MOV  EBX, xyz&   ; EBX is a pointer to xyz&

  ! MOV  ECX, [EBX]  ; ECX now contains xyz&

  ' ...more code would go in here

  ! POP  EBX

END SUB

In these cases, you must use the correct and complete address to access the value.  But regardless of whether the parameter represents a variable, an expression, or a literal constant, or whether it was passed by reference or by copy, the routine will always work correctly.

 

See Also

The Inline Assembler

Passing parameters

Parameters passed by value

Passing arrays

Passing dynamic strings