Redim problem with ASM addition of a 3D array floating point number

Windows specific questions.
Post Reply
fifoul
Posts: 15
Joined: Oct 17, 2005 13:20
Location: France

Redim problem with ASM addition of a 3D array floating point number

Post by fifoul »

In this code, If I replace the line :
dim as single num1(0 to kmax-1,0 to jmax-1,0 to imax-1)
by
REdim as single num1(0 to kmax-1,0 to jmax-1,0 to imax-1)
the result is fail ( it display : 13.3 + 14.4 = 14.4 )

What is wrong in the asm code with the REDIM declaration ?

Code: Select all

screenres 800,600,32

const imax = 30
const jmax = 20
const kmax = 10

dim as single num1(0 to kmax-1,0 to jmax-1,0 to imax-1)         ' If I use DIM, the result is correct : 13.3 + 14.4 = 27.7
'redim as single num1(0 to kmax-1,0 to jmax-1,0 to imax-1)       ' If I use REDIM, the result is wrong : 13.3 + 14.4 = 14.4      
dim as single num2,num3
dim as integer k,j,i

k=8
j=5
i=3
num1(k,j,i)=13.3
num2=14.4

asm
 mov ebx,dword ptr[k]           ' ebx = k 
 imul ebx,jmax                  ' ebx = k*jmax
 add ebx,dword ptr[j]           ' ebx = k*jmax+j
 imul ebx,imax                  ' ebx = (k*jmax+j)*imax
 add ebx,dword ptr[i]           ' ebx = (k*jmax+j)*imax+i
 sal ebx,2                      ' ebx = ((k*jmax+j)*imax+i)*4                   = offset address
 lea esi,[num1]                 ' esi = base address of num1(0,0,0)
 add esi,ebx                    ' esi = base address + offset address           = address of num1(k,j,i)
 movss xmm0,dword ptr[esi]      ' xmm0 = num1(k,j,i)
 movss xmm1,dword ptr[num2]     ' xmm1 = num2
 addss xmm0,xmm1                ' xmm0 = num1(k,j,i) + num2
 movss dword ptr[num3],xmm0     ' num3 = num1(k,j,i) + num2
end asm

?
? num1(k,j,i);" +";num2;" =";num3

sleep
Last edited by fxm on Apr 25, 2023 20:14, edited 2 times in total.
Reason: Added code tags.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Redim problem with ASM addition of a 3D array floating point number

Post by coderJeff »

fifoul wrote: Apr 25, 2023 19:58 What is wrong in the asm code with the REDIM declaration ?
DIM array( ... ) allocates the array on stack, and referring to the symbol 'array' in ASM refers to the first element of array()
So it works with ASM.

REDIM array( ... ) allocates a descriptor and the actual data is allocated in heap memory at runtime.
I suppose there are 2 issues to consider.
1) The descriptor (which describes important information about the array) has a pointer to the actual data. The only way to access the data is to first get a pointer to the descriptor and use that to figure out where the actual data is located.
2) fbc doesn't expose the address of the array descriptor to ASM (or user code in general for that matter, it might work in gcc backend, but is not handled in gas/gas64 backends).

Maybe this code will help show what is happening:

Code: Select all

#include once "fbc-int/array.bi"

'' x() is allocated on the stack: refering to 'x' in ASM refers to the
'' address of x(0,0,0) 
dim   as single x(0 to 7, 0 to 11, 0 to 13)

'' y() has a descriptor allocated statically and the actual data is allocated
'' in heap memory.  The descriptor has a field that points to data
'' refering to 'y' in ASM doesn't really mean anything since descriptor stuff
'' is solved by the compiler at compile time and 'y' may not even be emitted
'' as a symbol.  However, may find differences in how this is handled in the
'' various backends: gas/gas64/gcc/llvm/etc 
redim as single y(0 to 7, 0 to 11, 0 to 13)

dim px as any ptr   '' address of first element of x()
dim py as any ptr   '' address of first element of y() 

'' get the address of the descriptor
var desc = FBC.ArrayDescriptorPtr( y() )

#if defined(__FB_64BIT__)
asm
	lea rax, [x]     '' x is fixed length (no descriptor) 
	mov [px], rax

	'' lea rax, [y]  '' y is variable length array and has a descriptor
	''               '' lea rax, [y] doesn't work for gas64 backend

	mov rax, [desc]  '' desc is address of y's descriptor
	mov rax, 8[rax]  '' second element of the descriptor is the address of data 
	mov [py], rax
end asm
#else
asm
	lea eax, [x]     '' x is fixed length (no descriptor) 
	mov [px], eax

	'' lea eax, [y]  '' y is variable length array and has a descriptor
	''               '' lea eax, [y] doesn't work for gas backend

	mov eax, [desc]  '' desc is address of y's descriptor  
	mov eax, [eax+4] '' second element of the descriptor is the address of data 
	mov [py], eax
end asm
#endif

print "px       = " & hex(cint(px))
print "@x(0,0,0)= " & hex(cint(@x(0,0,0)))

print "@y       = " & hex(cint(desc)) & " (descriptor)"  
print "py       = " & hex(cint(py))
print "@y(0,0,0)= " & hex(cint(@y(0,0,0)))

sleep
fifoul
Posts: 15
Joined: Oct 17, 2005 13:20
Location: France

Re: Redim problem with ASM addition of a 3D array floating point number

Post by fifoul »

coderJeff,
Thank you very much for your explanation, it's very interesting and it will help me a lot later.
Post Reply