为什么我无法在 Windows 上链接我的汇编代码?

问题描述 投票:0回答:1

我正在使用 nasm 汇编器学习汇编,在链接 .o 文件时遇到麻烦。几天来无法解决这个问题,也不明白出了什么问题。这是代码和命令:

  section .data
      hello db 'Hello, World!', 0
  
  section .text
      global _start
  
  _start:
      ; Write message to stdout
      mov eax, 4          ; sys_write
      mov ebx, 1          ; file descriptor (stdout)
      mov ecx, hello      ; message to write
      mov edx, 13         ; message length
      int 0x80            ; interrupt to make syscall
  
      ; Exit
      mov eax, 1          ; sys_exit
      xor ebx, ebx        ; return 0
      int 0x80

组装和连接:

nasm -f elf32 hello.asm -o hello.o
gcc -m32 hello.o -o hello.exe

错误:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to WinMain@16'
windows assembly x86 nasm
1个回答
0
投票

根据您对

int 0x80
的使用判断,您的目标是 Linux。

所以,问题上的 [Windows] 标签是 Red Herring

然后您执行

nasm -f elf32 hello.asm -o hello.o
,这进一步表明您的目标是 Linux。 到目前为止一切顺利。

但是,您可以执行针对 Windows 的

gcc -m32 hello.o -o hello.exe
。 (
.exe
是Windows的东西。Linux中没有
.exe
这样的东西。)

因此,您必须决定:您要以 Linux 为目标,还是以 Windows 为目标? 一旦你决定了,就相应地编写你的代码,并相应地使用你的工具。 你不能混合搭配。

© www.soinside.com 2019 - 2024. All rights reserved.