Imagine I have some memory used to represent an array. What if I want to increase the size of my array? I should move all the data behind it and then insert a new piece of data? or?
Thanks in advance
Jannes
Check the HeapReAlloc documentation, and the movsb (or lodsb and stosb) instructions in \Masm32\help\opcodes.chm
Lazy assembler programmers have other options, too:
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
Init
Dim My$() ; create a dynamic string array
xor ecx, ecx
.Repeat
Let My$(ecx)=Str$("This is string #%i", ecx)
inc ecx
.Until ecx>5
Insert My$(3)
Let My$(3)="This string was inserted"
xor ecx, ecx
.Repeat
Print My$(ecx), CrLf$
inc ecx
.Until ecx>6
Inkey
Exit
end start
Output:
This is string #0
This is string #1
This is string #2
This string was inserted
This is string #3
This is string #4
This is string #5
But of course, under the hood you'll find HeapReAlloc, too ;-)
If you only perform a few insertions, you can simply HeapReAlloc and move the necessary data along to make space.
If you expect to make many insertions, it may be better to use a more appropriate data structure, e.g. a linked-list.
if i have a large array, rather than move the data, i will use a "circular buffer" design
that means you keep a pointer to the beginning (head) and a pointer to the end (tail)
you also know the buffer beginning and end addresses
by adjusting the head and tail pointers, and wrapping around the buffer end to beginning, you create a circular buffer
such a buffer is handy for things like serial I/O
it's much faster to change the value of the pointers than it is to move all that data :t
Okay and if I have an array, where should I store it? Can I simply choose an arbitrary address? or are there special addresses for this or something?
it depends
life-span - is it to exist temporarily, or for the life of the program session?
scope/visiblity - some arrays may be accessed, internal to a specific function - others may be accessed by any function
size - small arrays might be allocated in one of the data sections - larger ones might be on the heap or virtual
initialized/uninitialized - some may have values pre-assigned - some may not
the programmer takes all those things into consideration and chooses the best method
initialized or uninitialized data section, stack, HeapAlloc, VirtualAlloc are some of the most common choices
Quote from: gelatine1 on December 14, 2013, 08:15:47 PM
Okay and if I have an array, where should I store it?
If you
have an array, it's already stored somewhere:
include \masm32\include\masm32rt.inc
.code
MyArray REAL8 1.23, 4.56, 7.89, 9.87
start:
inkey real8$(MyArray)
exit
end startAny kind of memory is OK. Check hlhelp.chm, macro categories, memory allocation.
jj2007:
I would like to implement an array myself. So in this case I believe this simply requires one register to hold a pointer to the beginning of the array.
dedndave:
life-span: for the life of the program session.
visibility: global/ public
size: any size
both initialized and uninitialized possible.
Could you tell me where I can find information about the heapalloc and virtual alloc?
Thanks in advance
Quote from: gelatine1 on December 15, 2013, 05:06:59 AMCould you tell me where I can find information about the heapalloc and virtual alloc?
Go to the PowerBasic tools page (http://www.powerbasic.com/support/downloads/Microsoft%20Tools.htm) and download the
Windows 32-bit API Help File.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366569%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366569%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366701%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366701%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887%28v=vs.85%29.aspx)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366892%28v=vs.85%29.aspx (http://msdn.microsoft.com/en-us/library/windows/desktop/aa366892%28v=vs.85%29.aspx)
for smaller arrays, you can just create buffers in .DATA or .DATA? sections
.DATA
InitArray db 1,2,3,4,5,6,7,8
.DATA?
UnInitArray db 32768 dup(?) ;32 KB
You have two approaches to moving memory when you are inserting or deleting sections, you can re-alloc at the end of the memory and then move items around to fit OR if its not too big, just allocate a big enough buffer and rewrite the whole lot to the new buffer in the order you require. The second is usually faster but at the price of temporarily allocating twice the memory. Depending on your computer memory size, if its just a couple of meg, use another buffer, if its a gig do it the hard way to preserve memory.