gpt4 book ai didi

windows - 在 32 位 Windows 上使用 NASM 汇编创建一个 exe 文件

转载 作者:可可西里 更新时间:2023-11-01 13:28:31 24 4
gpt4 key购买 nike

我正在 32 位 Windows 7 上使用 NASM 用汇编语言编写一个 hello world 程序。我的代码是:

section .text 
global main ;must be declared for linker (ld)
main: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel

section .data
msg db 'Hello, world!', 0xa ;our dear string
len equ $ - msg ;length of our dear string

我将这个程序保存为hello.asm。接下来,我创建了 hello.o:

nasm -f elf hello.asm 

现在我正在尝试使用此命令创建 exe 文件:

ld -s -o hello hello.o 

但是现在我收到这个错误:

ld is not recognized as an internal or external command, operable program or batch

为什么会出现此错误,我该如何解决?

最佳答案

下载并install Mingw .然后把nasm放到Mingw的bin文件夹中。在名为 Hellobin 文件夹中创建一个文件夹。在这个文件夹中,使用以下代码创建名为 main.asm 的文件:

extern _printf
global _main

section .data
msg: db "Hello, world!",10,0

section .text
_main:
push msg
call _printf
add esp,4
ret

从文件夹中打开终端并编译,首先,使用 nasm 对象代码:

D:\MinGW\bin\Hello> ..\nasm -fwin32 main.asm

其次,调用gcc链接:

D:\MinGW\bin\Hello> ..\gcc main.obj -o main.exe

最后,测试一下:

D:\MinGW\bin\Hello> main.exe
Hello, world!

关于windows - 在 32 位 Windows 上使用 NASM 汇编创建一个 exe 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37407363/

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