Page 1 of 2

passing strings

Posted: Sat Oct 09, 2004 5:25 pm
by ukandrewc
Hello

I was wondering how to get a pointer to a string into a string variable so that I can use string functions on it e.g.

Code: Select all

Procedure AmendString(PtrToString.l)

;I appreciate this isn't valid but how do you assign pointer to string

String.s=PtrToString
Many thanks

Posted: Sat Oct 09, 2004 5:34 pm
by Num3
I think this is what you want:

Code: Select all

a$="Mary had a little lamb"

Procedure Test(pointer.l)
  b$=peeks(pointer)
 debug b$
endprocedure

Test(@a$)

Posted: Sat Oct 09, 2004 5:49 pm
by ukandrewc
Excellent, thanks num3

PB does seem to be a powerful language but it does take some time to know.

Thanks again

Andrew

Posted: Sat Oct 09, 2004 6:01 pm
by ukandrewc
Maybe I was a little premature. PeekS creates a new string, I am after a way to get a string variable for the same string.
e.g.

Code: Select all

b$=PeekS(PtrString)
;Creates a new string

;I need something like
b$=""
;Then point b$ at PtrString
Thanks

Posted: Sat Oct 09, 2004 6:49 pm
by wilbert
Why would you want two variable names pointing at the same string :?

Well, here's a way how to do it

Code: Select all

a$ = "Johnny had a big cow"
b$ = ""

! pushd [v_a$]
! popd [v_b$]

Debug @a$
Debug @b$
As you can see both are pointing at the same memory location

Posted: Sat Oct 09, 2004 8:40 pm
by ukandrewc
Thanks wilbert, I'll play with that.

I want to be able to pass a pointer to a string from VB and manipulate it in PB.

The actual code would be more like:

Code: Select all

ProcedureDLL ManipulateString(PtrString.l)
a$=""
! pushd [v_PtrString] 
! popd [v_a$]
Thanks again

Posted: Sat Oct 09, 2004 9:46 pm
by ukandrewc
Hello

Unfortunately I get an 'undefined variable' error when compiling the example, any help would be appreciated.

Posted: Sat Oct 09, 2004 10:10 pm
by GedB
Andrewc,

The problem is that PB only defines labels like v_a$ for variables at a global level.

Within procedures the paramaters and local paramaters are created on the stack.

Here's some code that would work the way you want it to:

Code: Select all

Procedure.s UseString(*String.l)
  DefType.s Result
  !MOV eax,dword [esp]
  !MOV [esp+4], eax
  ProcedureReturn Result
EndProcedure

String1.s = "Once upon a time..."
String2.s = UseString(@String1)
Debug "[" + Hex(@String1) + "] " + String1
Debug "[" + Hex(@String2) + "] " + String2
esp is the stack pointer. The paramater is at the top of the stack (esp).

The local variable is the next item on the stack (esp+4).

Question to the Gurus: If no procedure return is defined the compiler will add a default return. Is there any way to supress this so that the above function could be defined simply as '!MOV eax,dword [esp]', leaving eax as the return value?

Posted: Sat Oct 09, 2004 10:31 pm
by freak
If you want to return the value currently in EAX, you must just write ProcedureReturn
(without any variable)

Posted: Sat Oct 09, 2004 10:37 pm
by ukandrewc
Thanks for that, I'll have a play.

Where is all this stuff documented?

Andrew

Posted: Sun Oct 10, 2004 9:00 am
by GedB
It isn't documented specifically as PB.

The documents tell you that you can see the assembly generated by the compiler by using the /COMMENTED flag on the command line.
http://www.purebasic.com/documentation/ ... piler.html

This creates Purebasic.asm in the compilers directory. You can see, line by line, how the Purebasic code has been translated into ASM.

From there it is a question of learning Assembly. Randall Hyde's 'Art of Assembly Language Programming' is a good place to start:
http://webster.cs.ucr.edu/

There may not be much specific documentation, but I don't know of any other compiler that exposes it's innards so conveniently.

Posted: Sun Oct 10, 2004 9:19 am
by ukandrewc
OK, thanks GedB

I am familiar with ASM but it's things like the ! in front of assembler commands that don't seem to be documented

Andrew

Posted: Sun Oct 10, 2004 10:57 am
by GedB
I can't find the ! syntax described anywhere in the documentation. I think it's an unofficial feature.

The docs push the use of proper inline ASM.
http://www.purebasic.com/documentation/ ... edasm.html

I can see why, because with inline ASM supported the code is much friendlier

Code: Select all

Procedure.s UseString(*String.l)
  DefType.s Result
  MOV eax,*String
  MOV Result, eax
  ProcedureReturn Result
EndProcedure

String1.s = "Once upon a time..."
String2.s = UseString(@String1)
Debug "[" + Hex(@String1) + "] " + String1
Debug "[" + Hex(@String2) + "] " + String2 
Still, a note on the inlined page about using ! wouldn't do any harm. I wouldn't mind doing it myself if I was given CVS access.

Posted: Sun Oct 10, 2004 11:51 am
by Pupil
@ukandrewc
If you start a line with the '!' character everything behind it is passed directly to FASM. So everything that's available in FASM is avaiable in PB only you have to work a bit harder :)

Posted: Sun Oct 10, 2004 12:23 pm
by GedB
It seems that just return eax isn't enough. Looking at the ASM some fiddling is necessary when returning strings.