如何在64位NASM中使用malloc和free? [英] How to use malloc and free in 64-bit NASM?

查看:84
本文介绍了如何在64位NASM中使用malloc和free?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在64位NASM中,我正在使用C库中的malloc()分配8000个字节的内存块,并在完成后通过调用free()对其进行分配.

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().

我的研究提出了很多关于如何在64位NASM中执行此操作的冲突信息,并且很多信息是32位的,其中调用约定不同,或者是C或C ++,而不是NASM .

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.

我认为我拥有malloc部分,但是我不确定免费部分.我发布此问题是因为我不想测试它,并且分配了一个内存块,但没有释放它.

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.

所以我的两个问题很简单:
(1)我对64位NASM拥有此权利吗?
(2)Windows和Linux的语法是否相同?

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?

我只显示程序的malloc和自由部分:

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

推荐答案

汇编语言没有标准库.因此,这不是一个汇编语言问题,这肯定是我有一组符合此调用约定或由具有此类设置的X编译器和版本制作的库.我想链接并使用汇编语言中的那些库.首先,最好只用该语言编写,然后编译并保存临时文件或编译为汇编文件,然后从该代码开始.或反汇编此类代码以发现调用约定,并将其与使用此编译器读取该目标平台的调用约定时所发现的内容进行比较.

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.

如果这是系统调用,并且您想直接执行此操作,而不是库调用,那么您需要在此平台和操作系统的系统调用接口上进行阅读,没有理由假设任何两个都是相同的( Linux,BSD,Windows等).尽管它们可能是相同的,但每个版本的主要版本也不相同...

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.

这篇关于如何在64位NASM中使用malloc和free?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆