Hello Tim
On Friday, 8 November 1996 22:57:12, you wrote to All:
TH> For instance, I want to call an assembly SUB like any other
TH> SUB. eg.
TH> MySub MyString$
TH> I want to be able pass a literal or a simple string, or an
TH> array.
TH> I want the SUB to find out where the string is and set local
TH> pointers to it, like SI and DI. That way I can access and modify
TH> the string as needed.
For accessing strings in assembler procedures PB has some internal
procedures. The one you need here is GetStrLoc. It gives the length
of the string in CX and a pointer in DX:AX. When you use PB's
assembler it goes like this:
declare function GetStrLoc (byval Handle as integer) as long
SUB MySub (MyString AS STRING)
asm push ds ; preserve register contents
asm push di
asm lds bx, MyString ; load pointer to handle
asm mov ax, [bx] ; load handle
asm push ax ; push handle on stack for GetStrLoc
asm call GetStrLoc ; get pointer to string in dx:ax,
; and length in cx
asm mov es, dx ; let es:di point to string
asm mov di, ax ;
asm ....
asm pop di ; restore register contents
asm pop ds
END SUB
You can change the string from this sub. If you declare the string
parameter as BYVAL you can access the string, but you can't change
it. The string handle itself is pushed on the stack instead of its
pointer. That's two bytes shorter, and you can access it faster. The
necessary code is:
SUB MySub (BYVAL MyString AS STRING)
asm push di ; preserve register contents
asm mov ax, MyString ; load handle
asm push ax
etc.
TH> I would also want to be able to incorporate this into
TH> FUNCTIONs.
For function parameters it is not different. To return a value for a
string function you have to allocate a new string and return the
handle as function value. Example:
DECLARE FUNCTION GetStrLoc (BYVAL Handle%) AS LONG
DECLARE FUNCTION GetStrAlloc(BYVAL AllocSize%) AS INTEGER
FUNCTION YourString$ (BYVAL MyString AS STRING)
' This function copies MyString to YourString
asm push ds ; preserve register contents
asm push si
asm push di
asm mov ax, MyString ; load handle
asm push ax ; handle on stack for GetStrLoc
asm call GetStrLoc ; get pointer to string in dx:ax,
; and length in cx
asm mov ds, dx ; let ds:si point to string
asm mov si, ax ;
asm push cx ; length on stack for GetStrALloc
asm call GetStrAlloc ; gives new string handle in ax
asm push ax ; preserve new handle
asm push ax ; handle on stack for GetStrLoc
asm call GetStrLoc ; get pointer to new string in dx:ax,
; and length in cx
asm mov es, dx ; let es:di point to string
asm mov di, ax ;
asm rep movsb ; copy string
asm pop ax ; restore handle
asm pop di ; restore preserved registers
asm pop si
asm pop ds
asm mov FUNCTION, ax ; set function value
END FUNCTION
TH> string vars are accessed. I need to read them and sometimes I need
TH> to modify their contents and maybe even the length.
Modifying only, without changing the string's length, is easy if you
know the address. For changing the string's length you have to
allocate a new string with the new length, copy the contents of
the old string to the new one as far as it fits, and release the old
string memory. The function to release string memory is
DECLARE FUNCTION RlsStrAlloc%(BYVAL StringHandle%)
Try it yourself!
Friendly greeting you,
Hans Lunsing, Fido : 2:281/607.214, 2:282/610.12
Internet : jlunsing@doge.nl
--- Terminate 4.00/Pro
# Origin: BBS De Lauwers For BASIC Programmers! ++31594688407 (2:282/610.12)
---------------
* Origin: United Bbs Systems Europe MailGate to -> Fido USA (2:2802/337.0)
|