How to use malloc and free in 64

In 64-bit NASM, I'm allocating a memory block of 8000 bytes using malloc() from the C library, and when I'm finished with it, I deallocate it by calling free().

My research has come up with a lot of conflicting information about how to do this in 64-bit NASM, and much of the information is 32-bit, where the calling convention is different, or it's C or C++, not NASM.

I think I have the malloc part right, but I'm not sure about the free part. I'm posting this question because I don't want to test it and have a memory block allocated but not freed.

So my two questions are simple:
(1) do I have this right for 64-bit NASM?
(2) is the syntax the same for Windows and Linux?

I'm showing only the malloc and free parts of my program:

extern malloc
extern free

push rdi

; Allocate the memory buffer
mov rdi,8000
call malloc
mov [array_pointer],rax ;array_pointer is initialized in .data

; Code that uses the buffer goes here.  

; Free the memory buffer
push rdi
call free
add rsp,8

pop rdi
ret

Assembly language doesnt have a standard library. So this is not an assembly langauge question necessarily this is I have a set of libraries that conform to this calling convention or made by X compiler and version with such and such settings. And I want to link with and use those libraries from assembly language. Well first and foremost just write it in that language and compile and save temps or compile to assembly, and start with that code. Or disassemble such code to discover the calling convention and compare that to what you find when reading up on the calling convention for this target platform with this compiler.

If it is a system call and you want to do that directly, and not a library call then you need to read up on the system call interface for this platform and operating system, no reason to assume any two are the same (Linux, BSD, Windows, etc). Nor that major versions of each are the same although they probably are...

then write your code to conform to whichever you found.

链接地址: http://www.djcxy.com/p/80408.html

上一篇: calloc()比malloc()和memset()慢

下一篇: 如何在64中使用malloc和free