flat assembler
Message board for the users of flat assembler.

Index > Windows > Need help on getting the size of an array.

Author
Thread Post new topic Reply to topic
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
Hi folks!

Could someone please explain me how to get the size of an array?

Let's suppose i have a formatted text file that holds the values of an array, like:

1.3 2.5 4.7 7.9 6.7

I need to copy this array to memory. I read the text file, see how many numbers i have, them allocate enough memory just to hold these values to get something like something like: A = [1.3 2.5 4.7 7.9 6.7]

Later, i may need to add/remove numbers to this array. How can i do something like "sizeof(A)" to know the amount of memory i have to allocate?

Thanks,
ctl3d32
Post 13 Sep 2010, 04:09
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
You can't. You have to input the size by hand.
Post 13 Sep 2010, 04:36
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
When allocating, you could request 4 more bytes for the array. Then, before storing the pointer somewhere, you store the calculated size in the first four bytes and then you store the pointer but adding four first (optional step, but recommended so you don't have to worry about it when addressing individual cells).

If alignment is a concern (e.g. you'll use instructions that accesses more than 4 bytes at once), you may allocate more extra bytes instead of just 4 and use the rest for padding.

Example:
Code:
struc Array
{
  .length dd ?
  .base:
}
virtual at -4
  Array Array
end virtual

allocateArray:; EAX = number of (32-bit) floats.
   push    eax
   invoke  LocalAlloc, LMEM_FIXED, addr eax*4 + 4
   pop     dword [eax]
   add     eax, 4
ret

freeArray:; EAX = pointer to array
   invoke  LocalFree, addr eax - 4
ret

; Usage example

   mov     eax, 10
   call    allocateArray

   mov     ebx, eax
   mov     ecx, [ebx + Array.length] ; Equivalent to "mov ecx, [ebx - 4]"
   fld     dword [ebx+4*2] ; Load 3rd element into FPU

   mov     eax, ebx
   call    freeArray    
Post 13 Sep 2010, 05:00
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
to know the size of the array, you first need to read the text file, when converting each string in a value, you will increment a counter. ones you find the "endoftext", or "null" byte, it means that there are no more numbers, and then, the end of the array.
the counter will hold the size of the array. and is determined during the convertion of the text file in an array of datas.
Post 13 Sep 2010, 12:03
View user's profile Send private message Visit poster's website Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 204
Location: Brazil
ctl3d32
Thanks LocoDelAssembly!

Exactly what i needed!
Post 14 Sep 2010, 03:05
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.