gpt4 book ai didi

C StdLib malloc 来自 (N)ASM

转载 作者:太空宇宙 更新时间:2023-11-04 02:51:54 26 4
gpt4 key购买 nike

我希望从 ASM 文件中调用 malloc()。在 ASM 中:

extern malloc

没用。我想链接 CStdLib。

最佳答案

“无效”到底是什么意思?您需要做的不仅仅是使用 extern,您需要链接到 C 库。最简单的方法是使用gcc 来链接。正如您没有提到您的代码是 32 位还是 64 位,我将使用 32 位。 64 位的过程基本相同。

extern exit, printf, malloc, free

global main

BUFFER_SIZE equ 27

section .data
fmtstr db "%s", 10, 0

section .text
main:

push BUFFER_SIZE
call malloc
add esp, 4 * 1
mov esi, eax

xor ecx, ecx
mov edx, 97

.FillBuffer:
mov byte [esi + ecx], dl
inc edx
inc ecx
cmp ecx, BUFFER_SIZE - 1
jne .FillBuffer

mov byte [esi + ecx], 0

push esi
push fmtstr
call printf
add esp, 4 * 2

push esi
call free
add esp, 4 * 1

push 0
call exit
add esp, 4 * 1

和生成文件:

APP=malloctest

all: $(APP) clean

$(APP): $(APP).o
gcc -o $(APP) $(APP).o

$(APP).o: $(APP).asm
nasm -f elf $(APP).asm

clean:
rm $(APP).o

enter image description here

关于C StdLib malloc 来自 (N)ASM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com