Author Topic: ByRef  (Read 1397 times)

0 Members and 1 Guest are viewing this topic.

Peter

  • Full Member
  • ***
  • Posts: 161
ByRef
« on: February 08, 2016, 02:35:01 PM »
Hi Charles,

BYREF is a problem for your  compiler?

Code: [Select]
sub swap( byref a as sys, byref b as sys )
    iEcx = a
    iEax = b
    a = iEax
    b = iEcx
end sub

v1=10
v2=20

swap v1, v2
print  v1 & " " & v2  'correct

sub swop( byref sys a, byref sys b )
    mov Ecx, a
    mov Eax, b
    mov a, Eax
    mov b, Ecx
end sub

v1=10
v2=20

swop v1, v2
print  v1 & " " & v2  'odd
« Last Edit: February 09, 2016, 11:38:33 AM by Peter »

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #1 on: February 10, 2016, 03:17:53 AM »

Hi Peter,

The solution is to use addr for indirect variables

Code: OxygenBasic
  1. sub swop( byref a as sys, byref b as sys )
  2.     addr esi,a
  3.     addr edi,b
  4.     mov  ecx,[esi]
  5.     mov  edx,[edi]
  6.     mov  [esi],edx
  7.     mov  [edi],ecx
  8. end sub
  9.  

Peter

  • Full Member
  • ***
  • Posts: 161
Re: ByRef
« Reply #2 on: February 10, 2016, 05:08:58 AM »
Yes: I see it now, never used ADDR

Thanks Charles. 
                                                                                German:     
This is funny:   Charles Insectivorous Tree Finch.    Kleinschnabel-Darwinfink
                        Large Insectivorous Tree Finch.       Papageischnabel-Darwinfink
                        Small Insectivorous Tree Finch.       Zweigdarwinfink
                                             :D                               

edcronos

  • Jr. Member
  • **
  • Posts: 64
Re: ByRef
« Reply #3 on: December 21, 2017, 01:08:04 PM »
Hello
I can use byref in macros and functions in DLL that will be used in other programs or the best is to use byval?
to have a higher speed was thinking of sending the array of data that is already worked by other functions for the function in the DLL generated by the oxygen, and have an array with all results also by reference

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #4 on: December 21, 2017, 01:39:25 PM »
Arrays can only be passed by reference, and the same applies to most UDts. With single primitives, you have a choice

edcronos

  • Jr. Member
  • **
  • Posts: 64
Re: ByRef
« Reply #5 on: December 22, 2017, 09:08:57 PM »
as test to work in excel, it was like this

includepath "$/inc/"
$filename "And32count.dll"
$dll
include "RTL32.inc"
extern export

function And32(int a,b,byref c)
 

   mov eax, a
   mov ebx, b
   and eax, ebx

    addr edi ,c   '<---- ??
                       ' 'mov edi , eax
   mov c, eax    '<---- ??

end function

and to declare the dll

Private Declare Sub And32 Lib "D:\Programação\OxygenBasic\And32count.dll" _
                          (ByVal n As Long, ByVal n2 As Long, ByRef out As Long)


I have to see how to create multiple functions in the same dll
« Last Edit: December 22, 2017, 09:35:30 PM by edcronos »

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #6 on: December 23, 2017, 02:03:21 AM »
Almost!

Code: [Select]
includepath "$/inc/"
$filename "And32count.dll"
$dll
include "RTL32.inc"
extern export

function And32(long a,b,*c)
   mov eax, a
   mov ecx, b 'avoid using ebx
   and eax, ecx

    addr edi ,c
     mov [edi] , eax

     'the compiler will produce similar assembly code:
    'c=a and b
end function
and to declare the dll
Code: [Select]
Private Declare Sub And32 Lib "D:\Programação\OxygenBasic\And32count.dll" Alias "And32" _
                          (ByVal n As Long, ByVal n2 As Long, ByRef out As Long)

You can add as many subs or functions as needed.
« Last Edit: December 23, 2017, 02:16:13 AM by Charles Pegge »

edcronos

  • Jr. Member
  • **
  • Posts: 64
Re: ByRef
« Reply #7 on: December 23, 2017, 02:15:08 AM »
I just did not understand why it worked
    addr edi ,c   '<---- ??
   mov c, eax    '<---- ??
I did not understand the "edi" reference with c

in the case would be that when you pass the address "addr edi, c"
  automatically "c" happens to be the same as "edi"?
« Last Edit: December 23, 2017, 02:29:47 AM by edcronos »

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #8 on: December 23, 2017, 02:59:17 AM »
addr is a pseudo instruction for loading the address of a variable into a register, in this case, edi. Then you can use [edi], in square brackets, to access the variable.

Try #show to reveal the compiled assembly code:
#show c=a and b

edcronos

  • Jr. Member
  • **
  • Posts: 64
Re: ByRef
« Reply #9 on: December 23, 2017, 08:11:30 AM »
thanks for the explanation, had worked out I think because of the pointer, not the referral exchange
even with "addr esi, c"
   "mov c, eax" '<---- c continues being edi

anyway I'll have to wait for popcnt and continue using 32bit integers
I would not like to swap my excel, and found no way to work with more than 32bits with the dll
it would be nice if it were possible for an 8-byte array to work directly as a 64-bit integer, but I can not even dream much

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #10 on: December 23, 2017, 09:13:36 AM »

[edi] is used by all byref parameters. So your code works correctly by accident :)

edcronos

  • Jr. Member
  • **
  • Posts: 64
Re: ByRef
« Reply #11 on: December 23, 2017, 02:10:08 PM »
I'm sorry, but I'm going to ask a rather stupid question.
I practically know the answer, but the question kills me

why is the binary system that way?
and not from the least significant to the most significant and sequential way?

of the current way that it is complicates the interaction between systems of different bases

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #12 on: December 23, 2017, 06:59:24 PM »

edcronos

  • Jr. Member
  • **
  • Posts: 64
Re: ByRef
« Reply #13 on: December 23, 2017, 07:33:37 PM »
Wow, I did not even know that, I honestly thought I was talking a big bogey
Thank you for showing me this fact.
then this feeling that something is strange is not for nothing?
in my attempts to bular this communication inconsistency or the values are wrong or gives pane in excel, then I realized that a smaller variable does not fit into a larger one properly

https://www.ibm.com/developerworks/aix/library/au-endianc/index.html?ca=drs-

https://support.industry.siemens.com/cs/document/29155499/how-can-you-convert-a-big-endian-word-or-double-word-into-a-little-endian-word-or-double-word-and-vice-versa-?dti=0&lc=en-WW


but ok, I'm just being curious,
I do not consider myself qualified to work directly with ASM, and it is not because of the logic that I find very simple, but because of the care that has to be taken
I think even passing 4 integers of 32 bit in an array with all the data to be worked must have a good gain of performance, still more using popcnt
« Last Edit: December 23, 2017, 09:22:24 PM by edcronos »

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3584
    • Oxygen Basic
Re: ByRef
« Reply #14 on: December 24, 2017, 06:03:13 AM »
You can do 128 bit logic (but not integer arithmetic), using the SIMD instructions and XMM registers:

Code: [Select]
'128 bit logic
'2017-12-24 T 12:02:25
indexbase 0
int a={1,2,3,4,5,6,7,8}
movups xmm0,a[0]
movups xmm1,a[4]
andps  xmm0,xmm1  'and
'andnps xmm0,xmm1 'and not
'orps   xmm0,xmm1 'or
'xorps  xmm0,xmm1 'xor
movups a[0],xmm0
print a[0]