运行用 NASM 编写的 Win32 应用程序会导致“此应用程序无法在您的电脑上运行"错误 [英] Running a Win32 app written in NASM results in 'This app cant run on your pc' error
问题描述
我开始在 Windows 上学习 x86 汇编.我正在学习 32 位 x86 汇编.我使用 nasm
和 ld
来编译我的程序.我使用 mingw32-make
生成文件作为我的构建系统.我假设 ld
包含在 MinGW 中,但我不确定.我只知道它已经在我的电脑上.
I am starting to learn x86 assembly on Windows. I am learning 32 bit x86 assembly. I use nasm
and ld
in order to compile my programs. I use a mingw32-make
makefile as my build system. I am assuming ld
came included with MinGW, but I am not sure of that. All I know is that it was already on my computer.
我想编译一个非常简单的程序,以确保一切正常,当然,它没有.运行我的可执行文件时,一个巨大的蓝色框出现并显示此应用程序无法在您的电脑上运行",关闭通知后,Access is denied
字样打印在我的终端上.
I wanted to compile a very simple program just to make sure everything was working and of course, it wasn't. When running my executable, a giant blue box came up and said 'This app cant run on your pc' and after closing the notification, the words Access is denied
printed out onto my terminal.
这是我的程序:
global _main
_main:
mov eax, 1
ret
这是我的makefile:
And this is my makefile:
main: learn.asm
nasm -f win32 learn.asm -o learn.o
ld learn.o -o learn.exe
有人可以帮我解决这个问题吗?
Could someone help me fix this?
推荐答案
最初您的问题是询问使用 _start
作为入口点.您的链接方式不包括 C 运行时.
Originally your question asked about using _start
as an entry point. The way you were linking didn't include the C runtime.
在某些版本的 Windows 中,如果没有 .rdata
(只读数据),当与 MinGW LD 链接时,您可能会收到错误 This app can't run on your pc
部分情况下.一种情况似乎是带有 .idata
部分的可执行文件 (导入目录) 没有被标头引用并且不包含 .rdata
部分.当 MinGW LD 链接器在不使用 C 运行时从您的代码创建可执行文件时,似乎就是这种情况.要解决此问题,请添加至少包含一个字节数据的 .rdata
部分.这应该有效:
With some versions of Windows you may get the error This app cant run on your pc
when linking with MinGW LD if there is no .rdata
(read only data) section under some circumstances. One circumstance appears to be an executable with an .idata
section (import directory) not referenced by the header and doesn't contain an .rdata
section. This appears to be the case when the MinGW LD linker creates the executable from your code when not using a C runtime. To fix the problem add an .rdata
section with at least one byte of data. This should work:
global _start
section .rdata
db 0
section .text
_start:
mov eax, 1
ret
您还需要将您的代码放在 .text
部分,否则您可能会遇到其他问题.然后您可以组装和链接:
You also need to place your code in the .text
section or you may encounter other problems. You can then assemble and link with:
nasm -fwin32 learn.asm -o learn.obj
ld -o learn.obj -o learn.exe
如果您想使用 _main
并打算使用 C 运行时,那么您不需要创建 .rdata
部分,但是你会组装和链接:
If you want to use _main
and intend to use the C runtime then you don't need to create an .rdata
section, however you would assemble and link with:
nasm -fwin32 learn.asm -o learn.obj
gcc -m32 learn.obj -o learn.exe
我的链接建议
或者,您可以使用与 LD 不同的链接器.GoLink 特别应该从您使用的代码生成可执行文件.您可以通过以下方式创建带有 _start
入口点的 Win32 控制台应用程序:
My Recommendation for Linking
Alternatively you can use a different linker than LD. GoLink in particular should generate an executable from the code you were using. You can create a Win32 console application with a _start
entry point this way:
nasm -fwin32 learn.asm -o learn.obj
golink /console /entry _start learn.obj
GoLink 将生成一个具有适当标题和部分的可执行文件,Windows 运行应该没有问题,无需添加 .rdata
部分.
GoLink will produce an executable with proper headers and sections that Windows should have no problem running without the need to add an .rdata
section.
如果您安装了 MSVC/C++,您还可以使用 微软链接器:
If you have MSVC/C++ installed you can also use the Microsoft linker:
nasm -fwin32 learn.asm -o learn.obj
link learn.obj /entry:start /subsystem:console
这篇关于运行用 NASM 编写的 Win32 应用程序会导致“此应用程序无法在您的电脑上运行"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!