Passing a string constant pointer

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Passing a string constant pointer

Post by Coccoliso » Mon Mar 09, 2015 12:27 pm

I want to pass a string constant pointer to a function and then read its contents character by character as I do with SFRx, INDFx and POSTINCx ..
which is the most clean process?
Thanks.

Code: Select all

Private Const
    myString  = "Hello World!"

Public Function MyFunction(pMyStringAddr as Word) as Boolean
... 
End Function

Dim Res as Boolean
Res = MyFunction(@myString)
...

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Passing a string constant pointer

Post by Jerry Messina » Mon Mar 09, 2015 1:19 pm

If you're looking for low-level access, the rom equivalent of the FSR is the TABLEPTR

Code: Select all

private const
    myString = ("Hello World!")

public inline function TBLRD_POSTINC() as TABLAT
    asm
        TBLRD*+
    end asm
end function


public function MyFunction(pMyStringAddr as TABLEPTR) as boolean
    dim b as byte
    
    repeat
        b = TBLRD_POSTINC()
    until (b = 0)
    
    result = true
end function

Myfunction(addressof(mystring))

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Passing a string constant pointer

Post by Coccoliso » Mon Mar 09, 2015 1:42 pm

Hello Jerry,
it is true .. I remember I had seen something like that for constant arrays of strings.
Thank You.

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Passing a string constant pointer

Post by Coccoliso » Mon Mar 09, 2015 8:40 pm

Still a question.
If I have to pass the same pointer to a second function called inside the up level function..
Is perhaps best an example:

Code: Select all

Private Inline Function TBLRD_POSTINC() As TABLAT
    Asm
        TBLRD*+
    End Asm
End Function

Private Inline Function TBLRD_PREINC() As TABLAT
    Asm
        +TBLRD*
    End Asm
End Function

Private Inline Function TBLRD_VALUE() As TABLAT
    Asm
        TBLRD*
    End Asm
End Function

Public Function cLength(pAddr As TABLEPTR) As Word
    Result = 0
    While TBLRD_POSTINC()<>0
        Inc(Result)
    End While
End Function

Public Function MyFunction(pCStrAddr As ????) As Boolean
    Dim sLen As Word
    sLen = cLength(pCStrAddr)
    ...
.. is only a sophism but I'd like to know how to do it.

Thank.

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Passing a string constant pointer

Post by Jerry Messina » Mon Mar 09, 2015 9:09 pm

If you declare the function(s) with TABLEPTR used as a direct parameter then you have to be careful what you do with it.
If you want to be able to call other routines that also use the value then you'll have to use a variable to hold the address instead of using the TABLEPTR directly.

Just be sure to load the TABLEPTR with the address value before using the TBLRD_xxxx functions/instructions

Code: Select all

Public Function cLength(pAddr As word) As Word
    Result = 0
    TABLEPTR = pAddr
    While TBLRD_POSTINC()<>0
        Inc(Result)
    End While
End Function

Public Function MyFunction(pCStrAddr As word) As Boolean
    Dim sLen As Word
    sLen = cLength(pCStrAddr)
    TABLEPTR = pCStrAddr
    ...

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Passing a string constant pointer

Post by Coccoliso » Mon Mar 09, 2015 10:20 pm

Thank :D

Post Reply