Functions...another newbie question

New to FreeBASIC? Post your questions here.
Post Reply
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Functions...another newbie question

Post by newbieforever »

Another newbie question:

Code: Select all

Declare Function Ustr2Wstr(sss As String) As String
Function Ustr2Wstr(sss As String) As String
  sss = sss & chr(0) & chr(0)
  Dim As Wstring * 500 aux = ""
  asm pushad 
  asm mov esi, [sss]
  asm lea edi, [aux]
  asm mov ecx, 500
  asm rep movsb
  asm popad
  Return aux
End Function
The lines from 'sss ...' to 'asm popad' work perfectly if used directly in the code, buth the function fails. Can this be repaired?
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Functions...another newbie question

Post by newbieforever »

PS: aux to be returned is Wstring. Is this the problem?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Functions...another newbie question

Post by jj2007 »

I assume "the function fails" means the code builds without error messages but doesn't work, i.e. crashes or produces wrong results. Try being precise in the description of the problems encountered.

Remember the assembly code is a hack: The rep movsb just copies bytes from one memory location to another. The problem with your function here is another one, though: You return a local string variable. That can't work because in the moment when you leave the function, that local memory area (on the stack) is no longer valid. Strange that the compiler does not throw an error, though.

Btw it might be better if you keep your questions in one "personal" thread; otherwise the forum will get very cluttered with tiny threads like this.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: Functions...another newbie question

Post by newbieforever »

Yes, fails means no usable aux returned; no crash and no error message. OK, the reason must be associated to the assembly code hack, although I don't understand why aux can not be returned... But the use of a function was just an attempt, I don't need it really. (I assumed this was a basic newbie issue refering to functions, so the new thread was more meaningful.)

Btw, I am making huge progress in my project thanks to your help. (The breakthrough was 'instr(stg, chr(58) & chr(0))' instead of failing 'instr(stg, ":" & chr(0))', and of course your tricky unicodestring2wstring method.)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Functions...another newbie question

Post by jj2007 »

jj2007 wrote:You return a local string variable. That can't work because in the moment when you leave the function, that local memory area (on the stack) is no longer valid.
I am probably wrong on this: Disassembly shows that the "local" string variable gets malloc'ed. Test case:

Code: Select all

function addbrackets (byref s as string) as string
  dim as string MyStr="["+s+"]"
  return MyStr
end function

Print addbrackets("Just a string")

sleep()
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Functions...another newbie question

Post by Josep Roca »

The problem is that as the return type has been declared AS STRING, the returned WSTRING will be automatically converted to ansi.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Functions...another newbie question

Post by deltarho[1859] »

@newbieforever
Instead of typing a lot of asm, check out Asm/End Asm.
Post Reply