You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何使用MSVC 2019的ml.exe编译Windows简单Hello World汇编程序并解决语法错误?

Fixing ML.EXE Compilation Errors for NASM-Style Assembly Code

Hey there, the issue you're hitting is that your assembly code uses NASM syntax, but MSVC's ml.exe is a MASM (Microsoft Macro Assembler)-compatible tool—these two assemblers use very different syntax rules, which is why you're seeing all those syntax errors. Here's how to fix this while sticking with ml.exe:

1. Rewrite Your Code for MASM Compatibility

First, adjust your code to follow MASM's syntax rules. Here's the corrected version:

.386
.model flat, C
.stack 4096

EXTERN printf:PROC

.data
message db 'Hello, Worldsssss', 10, 0

.code
_main PROC
    push OFFSET message
    call printf
    add esp, 4
    ret
_main ENDP
END

Key Changes Explained:

  • .386 enables support for the 386 instruction set, .model flat, C sets up the 32-bit flat memory model (matching your original code's intent)
  • Replace NASM's section .text/section .data with MASM's .code and .data segment directives
  • Use EXTERN printf:PROC instead of extern _printf—MASM automatically handles the leading underscore for the C calling convention (you can also use EXTERN _printf:PROC if you prefer)
  • Define your entry point as a procedure with _main PROC/_main ENDP instead of using NASM's global directive
  • Use OFFSET message to get the address of your string (NASM's push message is equivalent to this in MASM)
  • Add the required END directive at the end of the file—MASM requires this to mark the end of the assembly code

2. Compile with the Correct ML.EXE Options

To compile the revised code, run this command (for 32-bit targets):

ml /c /coff HelloWorld_5.asm

What These Options Do:

  • /c: Tells ml.exe to only compile to an object file (.obj) and skip linking
  • /coff: Generates a COFF-format object file, which is the default format MSVC's linker expects

After compiling, link the object file with MSVC's link.exe:

link /subsystem:console HelloWorld_5.obj kernel32.lib user32.lib msvcrt.lib

/subsystem:console specifies this is a console application, and we link against the C runtime library (msvcrt.lib) and system libraries required for execution.

Important Note

There's no option to make ml.exe support NASM syntax—it's built exclusively for MASM-style assembly. So modifying your code to fit MASM's rules is the only way to use ml.exe for this task.

内容的提问来源于stack exchange,提问作者darek_911

火山引擎 最新活动