Hi all,
The question is how can I make an array it's size is specified with a variable?
thanks all for help
Posted on 2002-08-31 03:02:23 by amr
You'll have to implement this yourself.

First, you must learn how to allocate memory. The 'VirtualAlloc()' function, IIRC, is the way most of the people here prefer to allocate memory. You can look this up in MSDN, or on win32.hlp (bazik's sig has a link to MSDN). Now, VirtualAlloc() requires the size, IN BYTES, of the memory you need to allocate. So you must now the size, IN BYTES, of EACH ELEMENT in your array. Then, multiply the size of EACH ELEMENT with the variable which contains the number of elements in your array, to get the byte size of the whole array.

Addressing into the memory given by VirtualAlloc is the same as addressing any memory address, EXCEPT, YOU MUST NEVER LOSE THE POINTER RETURNED TO YOU. You can save this pointer in a variable in your .data section if you wish. To use the array, you need to reload the pointer to the array into a register, and use indirect addressing:


;get the 4th dword of the array pointed to by the array_ptr

;this loads the pointer to the array into ecx
mov ecx,array_ptr

;edx points within the array; it points to the fourth dword
mov edx,4*4

;now get it!
mov eax,[ecx][edx]



Hope this helped.
Posted on 2002-08-31 07:13:29 by AmkG
Personally, i prefer to use HeapCreate, HeapAlloc, etc. instead of VirtualAlloc et al. This method is (most) suitable for cases when you do not know the size of the array until runtime. Another method is to allocate a bunch of bytes in your .data section, then use that area to store the array values. Of course, you want more bytes allocated than you would ever intend to use, this method is most suitable for smaller arrays whose size is not changed at run time.

It is totally up to your preferences which method to use :)
Posted on 2002-08-31 07:46:28 by sluggy
Thanks for replying for my question but there is another question:
Can I use Indirect memery allocation ? or Effective address instruction in arrays?
thanks
Posted on 2002-09-03 00:44:57 by amr
amr,

most arrays end up being allocated in dynamic memory, GlobalAlloc(), VirtualAlloc() etc .. so as long as you can write to and reads from the memory, the type is only a metter of what is convenient to you.

What you need to get the swing of is the effective addressing modes in x86 assembler as this is how you access arrays at high speed.

Address of memory used is the BASE ADDRESS.

INDEX is a register that you use to modify the base address. The MULTIPLIER is used to set the data size being addressed and you can set a DISPLACEMENT as well from that address.

mov eax,

This translates to,

copy to EAX, the ADDRESS in ECX plus the value in EDX multiplied by 4 and with 4 added to it.

It works the same in reverse,

mov , eax

The only difference is the first one is moving a memory operand into a register where the second is moving a register into a memory operand.

Regards,

hutch@movsd.com
Posted on 2002-09-03 04:19:35 by hutch--
Thanks Hutch,
That was what I want to understand
amr
Posted on 2002-09-04 22:40:44 by amr