Assembly - Reserve array space during program?

2019-08-31 16:35发布

问题:

I am in the middle of an assembly program and I want to declare an array. I have the array size in a register and I know the type of the elements (i.e. how many bytes each element is) - how can I reserve space in the heap for this array (and then access particular elements)? Do I need to call malloc?

回答1:

"malloc()" is a creature of the C runtime library.

You can certainly call "malloc()" from assembly ... provided you initialize the C runtime system first.

C and C++ both do this automatically for you; before "main()" gets invoked.

For example, here's one link for how to do it on an ARM-bassed platform:

  • http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3730.html

Here's a link from Microsoft about what standard C functions to beware of on Windows:

  • http://msdn.microsoft.com/en-us/library/xwhcs5z6%28v=vs.80%29.aspx


回答2:

I think the best way is to indeed call malloc. Consider this code for FASM:

 include 'win32a.inc' 
 ...
 invoke  malloc,eax
 mov     [myHeap],eax 

Check out: http://www.delorie.com/djgpp/doc/ug/asm/calling.html