NASM ASM无法正确编译? [英] NASM ASM not being compiled properly?
问题描述
我正在遵循一个教程并正在学习 x64英特尔语法组件,并且我有这个代码示例,该示例应该可以毫无问题地进行编译,但是我遇到了错误,因此无法上手.
I am following a tutorial and learning x64 Intel Syntax Assembly and I have this code sample that suppose to compile without issues but I am getting errors, so I cannot get started.
代码示例:
.code
ASM_Debug proc
mov rax, 55
ret
ASM_Debug endp
end
如果我使用 x64 Intel Syntax MASM 使用内联汇编进行编译,则我正在使用 MinGW(G ++)代码编译器使用 NASM 汇编编译器我没有任何错误.
I am using NASM assembly compiler, using MinGW (G++) code compiler, if I compile using inline assembly using x64 Intel Syntax MASM I don't get any errors.
有人可以告诉我我需要做什么吗?什么是正确的 NASM 命令行来编译此代码,这样我就可以开始学习了.谢谢
Could someone tell me what do I need to do?, what is the correct NASM command line to compile this code so I could get started learning. Thanks
我当前的命令行是:nasm.exe -f elf64 Foo.asm -o Foo.o
My current command line is: nasm.exe -f elf64 Foo.asm -o Foo.o
PS:我不使用Visual Studio IDE进行开发,我不想听到有关它的任何消息.
PS: I am NOT using Visual Studio IDE for development, I don't want to hear anything about it.
PS2:我正在/ Windows OS(而不是Linux)上开发/工作.
PS2: I am developing/working on/for Windows OS, NOT Linux.
编译错误:
CoreASM.asm:1: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
CoreASM.asm:1: error: attempt to define a local label before any non-local labels
CoreASM.asm:2: error: parser: instruction expected
CoreASM.asm:5: error: symbol `ASM_Debug' redefined
CoreASM.asm:5: error: parser: instruction expected
CoreASM.asm:6: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
推荐答案
您编写的代码采用MASM语法,而NASM不支持.由于 PROC
/ ENDP
关键字,您可以分辨出它们,它们仅在MASM(和TASM,大多与MASM语法兼容)中.
The code you've written is in MASM syntax, which NASM doesn't support. You can tell because of the PROC
/ENDP
keywords, which are only in MASM (and TASM, which is mostly syntax-compatible with MASM).
所以,这是MASM语法:
So, this is MASM syntax:
.code
ASM_Debug proc
mov rax, 55
ret
ASM_Debug endp
end
这与翻译成NASM语法的代码相同:
And this is the same code translated to NASM syntax:
SECTION .text
ASM_Debug:
mov rax, 55
ret
尤其要注意:
-
.code
指令已由SECTION .text
代替. - 已删除
PROC
/ENDP
关键字,并使用一个简单标签来命名该过程.程序末尾不要重复其名称. - 代码文件末尾没有
END
指令.
- The
.code
directive has been replaced bySECTION .text
. - The
PROC
/ENDP
keywords have been eliminated, and a simple label is used to name the procedure. Procedures don't repeat their names at the end. - There is no
END
directive at the end of the code file.
您可以在线找到有关MASM和NASM之间的语法差异的多个指南.这是Dara的博客文章Hazeghi 看起来很全面.
You can find multiple guides to syntactical differences between MASM and NASM online. Here is a blog post by Dara Hazeghi that looks pretty comprehensive.
您用于运行NASM的命令行也是错误的,因为它试图构建Linux格式的二进制文件( elf64
格式).(当然,您可以 在Windows上的NASM中构建它们,但是您将无法运行.)对于64位Windows,您需要win64
.因此,它将是:
Your command line for running NASM is also wrong because it's trying to build a Linux-format binary (elf64
format). (You can build these in NASM on Windows, of course, but you won't be able to run them.) For 64-bit Windows, you want win64
. So, it would be:
nasm.exe -f win64 Foo.asm -o Foo.obj
(请注意,尽管您可以使用 .obj
,但是对于Windows上的目标文件而言,它是常规的,而不是 .o
.整个 -o
开关实际上是可选的;如果忽略它,NASM会自动使用标准扩展名和与我们的输入源文件相同的名称来命名目标文件.
(Note that .obj
is conventional for object files on Windows, not .o
, although you can use either. The entire -o
switch is actually optional; if you omit it, NASM will automatically name the object file with the standard extension and the same name as our input source file.)
但是,如果您要尝试学习如何进行汇编编程,则确实需要找到一个使用语法 identical 符合汇编程序期望的教程.如果要将NASM用作汇编程序,请查找也使用NASM的教程.或者,如果要使用MASM教程,请使用MASM作为汇编程序. MASM的版本8可以在此处下载-商业用途;它不是最新版本,但非常适合学习.但是,这是32位版本.如果要汇编64位代码,则需要x86-64版本,文件名为 ml64.exe
.我不知道您可以在何处单独下载此文件,但Windows SDK附带了 .至少,我知道 Windows 7 SDK ;我不确定最新的Windows 10 SDK中是否仍然存在该功能,但我认为是这样.
But, if you're trying to learn how to program in assembly, you really need to find a tutorial that uses syntax identical to what your assembler expects. If you want to use NASM as your assembler, then find a tutorial that also uses NASM. Or, if you want to use the MASM tutorial, then use MASM as your assembler. Version 8 of MASM can be downloaded here for non-commercial use; it isn't the newest version, but it's perfectly suitable for learning. However, that's the 32-bit version. If you want to assemble 64-bit code, you'll need the x86-64 version, with the filename ml64.exe
. I don't know anywhere you can download this separately, but it is included with the Windows SDK. At least, I know it was included with the Windows 7 SDK; I'm not sure if it's still there in the latest Windows 10 SDKs, but I think so.
这篇关于NASM ASM无法正确编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!