Interactive PowerBasic Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Frank Brübach on December 29, 2023, 02:28:19 PM

Title: Asm 42 o2 convert
Post by: Frank Brübach on December 29, 2023, 02:28:19 PM
Hi Charles and all...

Now I have fixed an old freebasic example with Union ... End Union

For freebasic site its running Well

But for oxygen the Type structure cant be correct

Oxygen example



union utype
{
  byte  b
  short w
  long  i
}

utype v
v.b=42
print v.b
print v.w
print v.i

'---------------------------- //

type opcodestring 'union
s as byte 'ptr
function f() as long
'function f1( byval i as long ) as long ' problem
function f1(int i) as long
end type 'union
''
'' ----------------- freebasic code is working ----------------- //
''Union opcodestring
''    s as byte Ptr
''    f as function () as long
''    f1 as function ( byval i As Long ) as long 'fixed parameter type
''end union
''
'' ----------------- freebasic code is working ----------------- //
''
dim as string st, st1
dim as opcodestring act, act1

print "test1" 'go

' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret

act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

print "test2" 'go

' Test the strings
print act.s '12 '44 '52 '124 '132 '164
'-------------------------------------- //
print "test3" 'go
'
'---------- problem below ------------------- //
'
print act.f()    ' answer 1
'

print act1.f1(42) ' should be answer 42

print "test4" 'no go
'sleep 5000

Title: Re: Asm 42 o2 vonvert
Post by: Charles Pegge on December 29, 2023, 03:14:22 PM
functions f and f1 need to be defined. But what is this code meant to do?
Title: Re: Asm 42 o2 convert
Post by: Johan Klassen on December 29, 2023, 03:30:02 PM
hello Frank Brübach :)
I modified your FreeBasic code so that it works
Union opcodestring
    s as byte Ptr
    declare function f() as long
    declare function f1( byval i As Long ) as long 'fixed parameter type
end union

function opcodestring.f() as long
return 1
end function

function opcodestring.f1(byval i As Long ) as long
return i
end function
''
'' ----------------- freebasic code is working ----------------- //
''
dim as string st, st1
dim as opcodestring act, act1

print "test1" 'go

' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret

act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

print "test2" 'go

' Test the strings
print act.s '12 '44 '52 '124 '132 '164
'-------------------------------------- //
print "test3" 'go
'
'---------- problem below ------------------- //
'
print act.f()    ' answer 1
'

print act1.f1(42) ' should be answer 42

print "test4" 'no go
'sleep 5000
Title: Re: Asm 42 o2 convert
Post by: Charles Pegge on December 29, 2023, 03:56:14 PM
Thanks Johan, that clarifies. Functions defined outside the type definition.
Title: Re: Asm 42 o2 convert
Post by: Frank Brübach on December 29, 2023, 04:04:12 PM
Yes thanks Jonathan and hello :) my freebasic example already worked fine before but not oxygen: conversion

Problem Zone remains with act.f() and act1.f1(42)  for oxygen Second example

Free Basic: (Works OK)

Union opcodestring
    s as byte Ptr
    f as function () as long
    f1 as function ( byval i As Long ) as long
    'function f() as long
    'function f1( byval i as long ) as long ' fixed parameter type
end union

dim as string st, st1
dim as opcodestring act, act1

print "test1"
' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret

act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

print "test2"

' Test the strings

print act.f()    ' answer 1

' Fixed ------------------- //
print act1.f1(42) ' answer 42

print "test3"
'Sleep 5000


And Here oxygen Basic :(Missing little Thing)

type opcodestring
    s as byte 'Ptr
    function f() as long
    function f1( byval i As Long ) as long 'fixed parameter type
end type

function opcodestring.f() as long
return 1
end function

function opcodestring.f1(byval i As Long ) as long
return i
end function
''
'' ----------------- freebasic code is working ----------------- //
''
dim as string st, st1
'dim as opcodestring act, act1
dim as opcodestring act
dim as opcodestring act1

print "test1" 'go

' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret

act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

print "test2" 'go

' Test the strings
print act.s '12 '44 '52 '124 '132 '164 '220
'-------------------------------------- //
print "test3" 'go
'
'---------- problem below ------------------- //
'
print act.f()    ' answer 1
'
print "test4" 'no go

print act1.f1(42) ' should be answer 42

print "test5" 'no go

''sleep 5000
Title: Re: Asm 42 o2 convert
Post by: Charles Pegge on December 29, 2023, 04:28:15 PM
The simplest o2 solution is to put the functions inside the type:

type opcodestring
    s as byte 'Ptr
    function f() as long
return 1
    end function

    function f1(byval i As Long ) as long
return i
    end function

end type
''
'' ----
...
Title: Re: Asm 42 o2 convert
Post by: Nicola on December 29, 2023, 06:52:19 PM
Excuse me Charles, 
why does the value that returns from the function return a rounded integer?

use console

type opcodestring
    s as byte 'Ptr
    function f() as long
   return 1
    end function

    function f1(byval i As Long ) as long
      if i>0 and i<6: return i*i : endif
      if i>5 and i<51: return sin(rad(i)) : endif
      if i>50 : return sqr(i) : endif
     
    end function

end type
''
'' ----
dim as string st, st1
dim as opcodestring act
dim as opcodestring act1
int i

for i=60 to 90 step 2
  print "act1.f1(" i ") --> "
  printl act1.f1(i)  "  sqrt --> " sqrt(i)
next

wait

Title: Re: Asm 42 o2 convert
Post by: Charles Pegge on December 29, 2023, 08:07:21 PM
These functions return long. You can change this to float:

use console

type opcodestring
    s as byte 'Ptr
    function f() as float
   return 1
    end function

    function f1(byval i As float ) as float
      if i>0 and i<6: return i*i : endif
      if i>5 and i<51: return sin(rad(i)) : endif
      if i>50 : return sqr(i) : endif
     
    end function

end type
''
'' ----
dim as string st, st1
dim as opcodestring act
dim as opcodestring act1
int i

for i=60 to 90 step 2
  print "act1.f1(" i ") --> "
  printl act1.f1(i)  "  sqrt --> " sqrt(i)
next

wait
Title: Re: Asm 42 o2 convert
Post by: Frank Brübach on December 30, 2023, 11:42:47 AM
Here is a complete fixed Translation from freebasic to oxygen Basic and fine running Version of asm 42 Code. Thanks Charles and Johan for Help

Oxygen

' asm 42 example oxygen, conversion from freebasic site
' 29/30->12->2023, frank bruebach
'
'---------------- freebasic example conversion to oxygen
'
type opcodestring
    s as byte 'Ptr
    function f() as long ' good idea charles
    return 1
    end function

    function f1( byval i As Long ) as long 'fixed parameter type
    return i
    end function
end type

''
dim as string st, st1
dim as opcodestring act, act1

' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret

act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

' Test the strings
'
print act.f()    ' answer 1
print act1.f1(42) ' answer 42

' end


Update oxygen code

Regards Frank
Title: Re: Asm 42 o2 convert
Post by: Johan Klassen on December 30, 2023, 03:37:20 PM
Frank, step 2 is only needed in FreeBasic, in Oxygen the function definitions are in the type declaration
Title: Re: Asm 42 o2 convert
Post by: Frank Brübach on December 30, 2023, 04:17:15 PM
A) No sorry must disagree to you whats concerning freebasic Code ;)
See my Code example below

B) for oxygen Site you are right thx
See my Post #8

Freebasic example
' freebasic version 1.06, compiling file exe with 29 kbyte
' asm 42 formula test, freebasic works, 28-12-2023
' frank bruebach, testing for a later conversion to oxygen

Union opcodestring
    s as byte Ptr
    f as function () as long
    f1 as function ( byval i As Long ) as Long
End union

dim as string st, st1
dim as opcodestring act, act1

' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret
act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

' Test the strings
Print act.f()    ' answer 1

' Fixed ------------------- //
print act1.f1(42) ' answer 42
Sleep 5000
[/Code}
Title: Re: Asm 42 o2 convert
Post by: Johan Klassen on December 30, 2023, 04:47:20 PM
Frank, in your FreeBasic code the functions method is not defined, it simply can not work even though it compiles without error messages
the way that I showed you earlier is the right way  ;)
but this is all I will say about this matter, if your way works without crashing then who am I to argue about it
I have made a resolution not to post on any forum starting next year, good bye forums ;D
Title: Re: Asm 42 o2 convert
Post by: Frank Brübach on December 30, 2023, 05:40:08 PM
OK I am Here to learn more about programming dear Johan perhaps you are quite right and my knowledge isnt so much complex about These Things... I never Had checked after compiling a freebasic Code for example how IT Works and why not ...

I only found this example on an old USB Stick and wanted to translate to oxygen cause I am learning more about programming by doing mistakes lol

You can stay Here :) you are very Welcome oxygen needs more Users Here at the Board with Higher or/and good skills 

Here's the fixed freebasic Code example again supported by johans Help

' freebasic, fixed version by johan klassen, 29-12-2023
' thank you by frank bruebach, concerning oxygen translation
'
Union opcodestring
    s as byte Ptr
    declare function f() as long
    declare function f1( byval i As Long ) as long 'fixed parameter type
end union

function opcodestring.f() as long
return 1
end function

function opcodestring.f1(byval i As Long ) as long
return i
end function
''
'' ----------------- freebasic code is working ----------------- //
''
dim as string st, st1
dim as opcodestring act, act1

' clear the eax register, increment it, and return
st = chr(&h33) + chr(&hc0) + chr(&h40) + chr(&hc3)  ' xor eax,eax inc eax ret

' extract the parameter from the stack into eax and return
st1 = chr(&h5a) + chr(&h58) + chr(&h52) + chr(&hc3) ' pop edx pop eax push edx ret

act.s = strptr(st): act1.s = strptr(st1)  ' set string pointers

' Test the strings
Print act.f()    ' answer 1
Print act1.f1(42) ' answer 42

Sleep 5000


Regards Frank
Title: Re: Asm 42 o2 convert
Post by: Zlatko Vid on December 30, 2023, 06:52:38 PM
QuoteI have made a resolution not to post on any forum starting next year, good bye forums

woof...why we must know that ?
because maybe facebook,discord or some others are better ?
or you simply are too tiered from internet  ;D
Title: Re: Asm 42 o2 convert
Post by: Nicola on December 31, 2023, 05:49:04 PM
Quote from: Charles Pegge on December 29, 2023, 08:07:21 PMThese functions return long. You can change this to float:
....
Excuse me, I've made a mistake... "Long" I saw as float... instead it is a long integer.

Anyway thanks.