Oxygen Basic

Programming => Problems & Solutions => Topic started by: Emil_halim on March 29, 2013, 10:35:28 AM

Title: string and asm
Post by: Emil_halim on March 29, 2013, 10:35:28 AM

Hi all,

i have a function that return a string variable type and i call it by call asm instruction , then i want to save the returned string in file so i used putfile. but it did not wrok as expected , any help please.
Code: [Select]

function name() as string
     string s = "i am string variable"
     return s
end function


sub Main

     call  name
     mov eax,[eax]
     putfile "t.txt",eax

 
Title: Re: string and asm
Post by: Peter on March 29, 2013, 10:54:48 AM
Hi Emil,

Code: [Select]
function name() as string
     string s = "i am string variable"
     return s
end function


sub Main

     call  name
     mov eax,[eax]
     putfile "t.txt",eax
     
End Sub     

Main
Title: Re: string and asm
Post by: Emil_halim on March 29, 2013, 11:05:18 AM

thanks Peter ,

but open the t.txt file it contains not the "i am string variable" string and that is wrong.
Title: Re: string and asm
Post by: Peter on March 29, 2013, 11:19:11 AM
I wonder me, how can the eax register store  a string ?
you just store the address of the string !
Title: Re: string and asm
Post by: Emil_halim on March 29, 2013, 11:22:25 AM

Peter this line should get the address of string.
Code: [Select]
    call  name
    mov eax,[eax]  ' this line should get the address of string
Title: Re: string and asm
Post by: Emil_halim on March 29, 2013, 11:28:19 AM

Charles , does string variable has a string descriptor , and if  so how is it work ?   
Title: Re: string and asm
Post by: Peter on March 29, 2013, 11:59:57 AM
Hi Emil,

Code: [Select]
indexbase 0

function name() as string
     string s = "i am string variable"
     return s
end function


sub Main
     char s[22], dword x
     name
     lea esi,s
     mov edx,x 
     .m1 mov cl,[eax+edx]
mov [esi+edx],cl
         inc edx
         cmp edx,20
         jnz m1     
putfile "t.txt",s
End Sub     

Main
Title: Re: string and asm
Post by: Emil_halim on March 29, 2013, 12:15:31 PM

Ok thanks Peter ,

but why you copy the returned string in char array then pass this copy to Putfile?

BTW name is a dummy function here , in real code you did not know the length of returned string?
Title: Re: string and asm
Post by: Peter on March 29, 2013, 01:15:59 PM
Hi Emil
Code: [Select]
indexbase 0
sys length

function name() as string
     string s = "i am string variable"
     length = Len(s)
     return s
end function


sub Main
     char s[22], dword x
     name
     lea esi,s
     xor edx,edx 
     .m1 mov cl,[eax+edx]
mov [esi+edx],cl
         inc edx
         cmp edx,length
         jnz m1     
putfile "t.txt",s
End Sub     

Main
Title: Re: string and asm
Post by: Charles Pegge on March 29, 2013, 01:21:13 PM
A string function returns a bstring, which points to an array of characters.

With low level handling you must free the string after.

function name() as string
     string s = "i am string variable"
     return s
end function

'low level handling
sys a
call  name
mov a,eax
putfile "t.txt", cast bstring a
freememory a


But basic will do it all for you :)

putfile "t.txt",name



Title: Re: string and asm
Post by: Peter on March 29, 2013, 01:37:06 PM
Why do I love Assembler and not Basic.
Title: Re: string and asm
Post by: Charles Pegge on March 29, 2013, 02:10:38 PM
I felt the same about Basic too, which is why I started this project. Would you like to create your own language Peter?
Title: Re: string and asm
Post by: Emil_halim on March 30, 2013, 07:27:52 AM

Hi Charles ,

thanks for demonstration ,

Quote
'low level handling
sys a
call  name
mov a,eax
putfile "t.txt", cast bstring a
freememory a


using a variable for moving the address of string into it then casting it so that it works with putfile ,
is not good.
can Oxygen allow casting directly from register , something like this ?
Code: [Select]
call  name
putfile "t.txt", cast bstring  Eax
Title: Re: string and asm
Post by: Emil_halim on March 30, 2013, 07:40:39 AM

hi Peter
Quote
Why do I love Assembler and not Basic.

the best thing in Oxygen basic is that you have a variety of ways to achieve a certain things.

for example see the example of iteration. the for statement can be in pure basic and can be in mix between
basic & C and can be in pure C.

this make Oxygen a very good tool for programming.

I do like to mix my coding in basic , C , asm to get the best thing that i need , so the point here is not to love asm and not basic.       
Title: Re: string and asm
Post by: Charles Pegge on March 30, 2013, 08:50:53 AM
Yes, I agree about the variable. I forgot all about the stack :)

function name() as string
     string s = "i am string variable"
     return s
end function

'low level handling
name
push eax
putfile "t.txt",cast bstring eax
pop eax
freememory eax
Title: Re: string and asm
Post by: Emil_halim on March 30, 2013, 09:05:30 AM

Charles , what do you think about this

Code: [Select]
function name() as string
     string s = "i am string variable"
     return s
end function

'low level handling
name
push eax
putfile "t.txt",cast bstring eax
call @freememory   ' we already pushed the argument

how to call freememory from asm ?
Title: Re: string and asm
Post by: Peter on March 30, 2013, 01:21:05 PM
Quote
Would you like to create your own language Peter?

No, I have got one.
Title: Re: string and asm
Post by: JRS on March 30, 2013, 01:25:52 PM
Peter is switching to SB from what I was told. :o
Title: Re: string and asm
Post by: Peter on March 30, 2013, 01:26:42 PM
Quote
how to call freememory from asm ?

Code: [Select]
FreeMemory eax
Title: Re: string and asm
Post by: Charles Pegge on March 30, 2013, 09:07:37 PM

Charles , what do you think about this

Code: [Select]
function name() as string
     string s = "i am string variable"
     return s
end function

'low level handling
name
push eax
putfile "t.txt",cast bstring eax
call @freememory   ' we already pushed the argument

how to call freememory from asm ?

No Emil, stdcall, will remove parameter from the stack on return from functions. but you can push eax onto the stack twice.

One warning, this asm stuff, you will have to rewrite for 64 bit compiling. The 64 bit calling conventions are not very friendly to programmers. Hence my interest in exposing the abstract assembly layer.

Title: Re: string and asm
Post by: Emil_halim on March 31, 2013, 07:04:13 AM
Quote
Quote
how to call freememory from asm ?

Code:
FreeMemory eax

this is not what i asked for , my be i asked wrongly. any way i was asking for that is this legal
Code: [Select]
call freememory

or this
Code: [Select]
call @freememory

Quote
No Emil, stdcall, will remove parameter from the stack on return from functions. but you can push eax onto the stack twice.
why , i was thinking that this is good enough
Quote
putfile "t.txt",cast bstring eax
so oxygen will cast eax then push it as an argument ,so stdcall will remove the pushed eax in putfil line
and the upper push will work with freememory.
Title: Re: string and asm
Post by: Emil_halim on March 31, 2013, 07:23:43 AM

Charles , i used this to know how things works.
Code: [Select]
#show     putfile  file, cast bstring eax

then i get this list which i did not undersatnd any thing.
Quote
$op 1 C1 eax
$op 60 C1
$go 0 [ebx+0x8B0]
$op 4 C1
$pa 1 0 4 [ebp+0x8]
$ch 1
$go 0[ebx+344]
$go 0 [ebx+0x838]

so what is this list?
Title: Re: string and asm
Post by: Charles Pegge on March 31, 2013, 08:43:01 AM
Hi Emil,

You are seeing the machine-friendly / User-unfriendly OIL code for one line of basic. This gets converted to x86 assembly code. Should I turn this demon into an angel?  :)
Title: Re: string and asm
Post by: Emil_halim on March 31, 2013, 09:00:02 AM

okay , what about the above previous post?
Title: Re: string and asm
Post by: Peter on March 31, 2013, 09:45:20 AM
Quote
Should I turn this demon into an angel?

Yes, but 64bit.  :D
Title: Re: string and asm
Post by: JRS on March 31, 2013, 09:48:38 AM
Quote
Should I turn this demon into an angel?

(http://files.allbasic.info/O2/O2_Charles.jpg)

Based on everything I've seen to date, that should be a walk in the park. You are truly amazing Charles!