Oxygen Basic

Programming => Problems & Solutions => Topic started by: Frankolinox on February 08, 2014, 01:25:07 AM

Title: string literal adress question
Post by: Frankolinox on February 08, 2014, 01:25:07 AM
lalala :)
Title: Re: string literal adress question
Post by: Charles Pegge on February 08, 2014, 04:04:38 AM
In Basic and Asm:

Code: [Select]

function Reverse(string s) as string
string t=s
sys le=len t
for i=1 to le
mid t,i,mid s,le-i+1
next
return t
end function

print reverse "abcdefghijklmnopqrstuvwxyz"

function Reverse(string s) as string
string t=s
sys le=len t
addr edi,t
addr esi,s
mov ecx,le
dec ecx
(
 mov al,[esi]
 mov [edi+ecx],al
 inc esi
 dec ecx
 jge repeat
)
return t
end function


print reverse "abcdefghijklmnopqrstuvwxyz"

And another using byte pointers:

Code: [Select]
function Reverse(string s) as string
string t=s
sys le=len t
byte a at (strptr s)
byte b at (le-1+strptr t)
for i=1 to le
b=a : @b-- : @a++
next
return t
end function

print reverse "abcdefghijklmnopqrstuvwxyz"

Title: Re: string literal adress question
Post by: Charles Pegge on February 10, 2014, 02:16:57 AM
Hi Frank, If you mean translation from string to integer, and integer to string:

string to sys

string s="abcd"
sys     a
a="0x"+s 'auto-conversion val()
print hex a

and back to string again:

s=hex a
print s

Title: Re: string literal adress question
Post by: Charles Pegge on February 10, 2014, 09:26:23 AM
Will this do?

Code: [Select]
'Reading String as array of integers

string s="Oxygen"

long a at (strptr s)

print hex(a[1],8)+"    "+hex(a[2],8)

'Reading array of integers as string

zstring z at (@a)

print z

Title: Re: string literal adress question
Post by: Aurel on February 10, 2014, 11:36:32 AM
Situation will be much better if we can use cast
something like this:

string s="promo"
sys p = *<string>s
print p

ahhh whishes ::)

Hmm Charles
I still don't understand real purpose of this at
Title: Re: string literal adress question
Post by: Charles Pegge on February 10, 2014, 01:54:41 PM
Recent Oxygen releases also support the equivalent C-like syntax:

long*a=strptr s

Code: [Select]
'Reading String as array of integers

string s="Oxygen"

long*a=strptr s

print hex(a[1],8)+"    "+hex(a[2],8)

'Reading array of integers as string

zstring*z=@a

print z
Title: Re: string literal adress question
Post by: Aurel on February 10, 2014, 10:50:46 PM
Hmm yes it looks that only solution is zstring *z

Charles
What Free Basic have ...i think something like reinterpret_cast or
something like that?

Is C way *<type>p correct  ?
because  i am not sure , and i can agree that syntax is little bit strange ...
what you think ?
Title: Re: string literal adress question
Post by: Charles Pegge on February 10, 2014, 11:09:11 PM
You can cast with round '()' brackets, but it is not necessary to cast here. Oxygen accepts any expression for a pointer in this kind of definition.
Title: Re: string literal adress question
Post by: Aurel on February 10, 2014, 11:13:22 PM
Cast with brackets ?
How?
Title: Re: string literal adress question
Post by: Charles Pegge on February 11, 2014, 10:15:04 AM
Integers reverse the order of the 4 ascii codes: thus you will see a reversal in their hex values:

This is due to the x86 processor taking the 1st byte of an integer as being the lowest. This behaviour of the Processor is often termed:  little-endian

"abcdefgh" --->

int 1 = 0x68676665 'dcba'
int 2 = 0x6C6B6A69 'hgfe'

You could look at the string as an array of bytes, instead of an array of 32bit integers. It is less confusing, and you will not be troubled by these order effects. :)

Code: [Select]
s="Oxygen"
byte a at (strptr s)
print hex(a[1],2)+" "+hex(a[2],2)+" "+
     +hex(a[3],2)+" "+hex(a[4],2)+" "+
     +hex(a[5],2)+" "+hex(a[6],2)+" "
Title: Re: string literal adress question
Post by: Emil_halim on February 12, 2014, 08:58:51 AM
Hi Charles,

i see that

long*a=strptr s

is the same as

long a at (strptr s)

am i wright ?

if so we need some doc that shade some light on Oxygen basic , it has may goodies but need to be clear in help file.