Archived

This topic is now archived and is closed to further replies.

Arrays in ASM

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright I have been reading a book on a assembler and toying with a simulator, but I have come to a dead end on how to mimick arrays. And it sort of hit me that dynamic allocation at assembly level is probably the next best thing since ebola, or smallpox -- I was wonderring if anyone has a notion of how to do something like this -- dynamic allocation of an array at run time in assembly? HELP!!!

Share this post


Link to post
Share on other sites
You''d do the same thing you''d ordinarily do ... call GlobalAlloc, or a similar memory allocation routine. All of this stuff is accessible via assembler .. you just have to be sure to set up the stack properly before calling.

You can also allocate memory dynamically on the stack, in the same manner as you''d decalre local variables, but there is less stack memory available than system memory so you''d have to be a bit more careful there.

Assuming you''ve got a C compiler, code up an allocation routine using GlobalAlloc, compile to Asm source, and look at what the code does. Then code a little routine to allocate an array on the stack, and look at the asm output for that.

Share this post


Link to post
Share on other sites
pointers and arrays are next to identical, technically theres not such thing as arrays or refrences. all languages have pointers most just hided except for C/C++. arrays and refrences are all translated at compile time to pointers by the compliler. this is with high level languages. thast why refrences and arraysthere is not the same in assembly, its mostly pointers.

[edited by - Commander Keen on March 26, 2004 4:37:41 PM]

Share this post


Link to post
Share on other sites
Related question:

How is the heap allocated or tracked or whatever - I guess in C/C++ because obviously its not going to be tracked natively by assembled code?

How do you know how much memory the OS or whatever system has allotted you -- I''m guessing again -- but that it would be in the in the manual for the OS, or something, or heck I really don''t know.

HELP--O


Share this post


Link to post
Share on other sites
quote:
Original post by newOperator
Related question:

How is the heap allocated or tracked or whatever - I guess in C/C++ because obviously its not going to be tracked natively by assembled code?

How do you know how much memory the OS or whatever system has allotted you -- I''m guessing again -- but that it would be in the in the manual for the OS, or something, or heck I really don''t know.

HELP--O





The heap is all managed by the operating system. You just need to ask for the amount of memory you need with one of the alloc functions and it gives it to you if it can.

Share this post


Link to post
Share on other sites
All memory, either the code, data, the stack or the heap, is managed by the operating system and in the case of multitasking systems like Windows and Linux you are not allowed to access anything outside your own memory allocations. That leads to those dreaded ''Access Violations'' with which we are familiar.

Under DOS you had all the machine and could read/write anything anywhere (almost).

I read somewhere that Windows allocates a notional 4GB to each process, although 2GB is shared for inter-process communication. If you want to use more than your program initially requested in its DATA and STACK directives, you have to ask for it (Heap Allocation is the usual way).

If you do get memory off the heap, don''t forget to give it back when your program closes. Failure to do this leads to ''memory leaks''




Stevie

Don''t follow me, I''m lost.

Share this post


Link to post
Share on other sites
quote:
Original post by stevie56

If you do get memory off the heap, don''t forget to give it back when your program closes. Failure to do this leads to ''memory leaks''



Doesn''t the OS automatically free up the memory your program requests from the heap when it exits?





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
quote:
Doesn't the OS automatically free up the memory your program requests from the heap when it exits?


Windows2000/XP+ can but the OS can still be brought down, especially if the running program continues to leak =)~
For anyone with windows95/98, they may end up rebooting.



Here is my array:


.data?
A1 dd ? ;points to the allocated memory

.code
heapallocate proc

invoke GetProcessHeap
invoke HeapAlloc,eax,HEAP_ZERO_MEMORY,1024*sizeof(DWORD) ;up to 1024 dwords

mov A1,eax

ret
heapallocate endp

cleanup proc
invoke GetProcessHeap
invoke HeapFree, eax, 0, A1
ret
cleanup endp


to access each element, it must be scaled in whatever you reserved the memory as, whether it be a structure, float, byte, dword, etc..

mov edx,A1
mov ecx,5 ;point to the 5th array element
imul ecx, sizeof(dword) ;scale it
mov eax,[edx+ecx]


voila!

You should really visit the [plug]win32asm forums[/plug] for these sort of questions.

I fseek, therefore I fam.



[edited by - drarem on March 31, 2004 3:55:10 AM]

Share this post


Link to post
Share on other sites