Hello,

I was wondering how you'd make dynamic arrays in Assembly.

(please excuse the C++)

For instance in C++, Id do char *blah;

blah = new char[256];


How would that work in Assembly?


Thanks :)
Posted on 2003-05-11 13:53:46 by Torch
	invoke GetProcessHeap

invoke HeapAlloc, eax,
HEAP_ZERO_MEMORY or HEAP_GENERATE_EXCEPTIONS,
256 ; bytes needed
push eax ; For HeapFree


; use the pointer in EAX


invoke GetProcessHeap
push NULL
push eax
call HeapFree
...or...
	sub	esp, 256


;ESP is the pointer to memory

add esp, 256
Posted on 2003-05-11 13:56:14 by bitRAKE
if I wanted to move this to a variable for use later, what would I do?

mov var,

or

mov var, eax

?

and the var would need to be what?
Posted on 2003-05-11 13:58:55 by Torch
mov pARRAY, eax

EAX is a 32-bit memory pointer.

In MASM you can define it like:

pARRAY BYTE PTR ?

...or...

pARRAY DWORD ?

Most people use DWORD, but I like to be more descriptive if I can.
Posted on 2003-05-11 14:02:48 by bitRAKE
Thank you much :)


Im not very good at finding this type of stuff on the net.

Is there a book or tutorial thats different that Art of Assembly?


Also, why does it crash when I leave out "push eax"?
Posted on 2003-05-11 14:05:10 by Torch

Also, why does it crash when I leave out "push eax"?
It's the same as:
	pARRAY BYTE PTR ?

...
invoke GetProcessHeap
invoke HeapAlloc, eax,
HEAP_ZERO_MEMORY or HEAP_GENERATE_EXCEPTIONS,
256 ; bytes needed
mov pARRAY, eax


; use the pointer in pARRAY


invoke GetProcessHeap
invoke HeapFree, eax, NULL, pARRAY
...I just cut-n-pasted from some code - which I like to just store it on the stack sometimes. Do you need to beef up on assembly or windows programming? For assembly Randy's AoA is really good and just try to understand as much code as possible (download stuff, add little features, break it, etc. :) ). For windows, learn enough C/C++ to understand the PSDK - nothing better than the horse's mouth.
Posted on 2003-05-11 14:20:10 by bitRAKE
Need to beef up on Assembly :)

I can use C++ pretty decently
Posted on 2003-05-11 14:21:34 by Torch
Damn arrays =P


Ok.. if I want to move an entire array to a register, I should do what?

All I saw in AoA was how to access single elements
Posted on 2003-05-11 15:03:19 by Torch

Damn arrays =P
Ok.. if I want to move an entire array to a register, I should do what?

That's like asking how to move an entire array into a C++ int :) You can't, registers are 32-bit. You can get one element of the array into it (or multiple at the same time, as long as it doesn't exceed 32 bits), or move a pointer to the array into it but not the entire array.

Thomas
Posted on 2003-05-11 15:11:14 by Thomas
I'll keep messing with it :/


Trying to do an OpenFile dialog but it keeps failing or not working at all

edit: And I got it :/ hehe
Posted on 2003-05-11 15:20:59 by Torch
You can only load the address of the array to a register using lea and not the whole array into register. Unless of course the whole array is 32 bit :)

Nice to see stack used as a buffer. Nice idea bitRAKE :)
Posted on 2003-05-12 00:26:10 by roticv