【发布时间】:2016-09-21 07:12:39
【问题描述】:
我正在用 NASM 在 32 位 Windows 7 上用汇编语言制作一个 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 不是内部或外部命令、可运行程序或批处理
为什么会出现此错误,我该如何解决?
【问题讨论】:
-
当然,看看你正在使用的 NASM 代码会很有趣。如果您碰巧在学习 Linux 教程,请不要期望程序能够正常运行。您能否提供您正在使用的教程的链接?
-
我已将您的代码放入您的问题中,作为对您的编辑。我怀疑你在 Windows 上使用 Linux 教程。不幸的是,Linux 不是 Windows。即使您将其链接到名为
hello.exe的可执行文件并运行它,它也会失败。int 0x80不适用于 Windows,它是 Linux 的 32 位系统调用。你将不得不找到一个关于用汇编程序编写 Windows 程序的教程。 -
如果你要安装TDM-GCC(我的第一条评论中有一个链接),它安装了 GCC 和 LD(GNU 工具链的一部分),然后按照Stackoverflow answer 的第一部分您也许可以打印
Hello World。该示例中的方法使用汇编代码中的 C 库函数printf进行打印。
标签: windows assembly linker x86 nasm