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