【问题标题】:Visual Studio C/C++ Project with MASM code produces error `error A2008: syntax error : .` [closed]带有 MASM 代码的 Visual Studio C/C++ 项目产生错误“错误 A2008:语法错误:。” [关闭]
【发布时间】:2020-10-11 08:59:55
【问题描述】:

我有一个 Visual Studio C/C++ 项目,其中包含一个程序集文件 main.asm。在构建项目的过程中,我得到了错误:

1>main.asm(5): error A2008: syntax error : .
1>main.asm(6): error A2008: syntax error : .
1>main.asm(7): error A2008: syntax error : .
1>main.asm(8): error A2008: syntax error : ,
1>main.asm(20): error A2008: syntax error : INVOKE

我的 ma​​in.asm 代码是:

; program 3.1
; sample Assembly program - MASM (32-bit)


.386                                ; Line 5
.MODEL FLAT, stdcall                ; Line 6
.STACK 4096                         ; Line 7
ExitProcess PROTO, dwExitCode:DWORD ; Line 8

.data
sum DWORD 0

.code
_main PROC
mov eax, 25
mov ebx, 50
add ebx, ebx
mov sum, eax

INVOKE ExitProcess, 0               ; Line 20
_main ENDP
END

带有我的代码和错误的 Visual Studio 屏幕截图:

为什么会出现这些错误,我该如何解决?

【问题讨论】:

  • 您的问题下方有一个edit 按钮,用于编辑问题。您不应将此信息放在评论中。 (我在你的问题中添加了图片)
  • 之前有人问过(并回答了)类似的问题,请参阅:stackoverflow.com/questions/46611550/error-a2008-syntax-error
  • 顺便说一句,将代码作为 TEXT 发布,而不是图片更适合问题....
  • 这太令人沮丧了,我不明白它在 Luuk 链接中的意思。为什么我不能开始编程。我已经设置了我的组装环境。我很困惑为什么它不起作用。我已经为此工作了几个小时。为什么我在该图像的第 5 行出现语法错误我已经尝试摆脱 cmets 但这不是问题。

标签: visual-studio assembly x86 masm


【解决方案1】:

根据您在以.MODEL.STACK.386 开头的行上显示的错误,我只能推测您正在为 64 位目标而不是 32 位目标构建。您可能还会收到与INVOKE 指令相关的错误。 64 位 MASM 不支持这些指令,因此会产生错误。在 64 位代码中,模型始终假定为平面模型,并且 CDECL、STDCALL、THISCALL、FASTCALL 等的调用约定都相同,并遵循 Windows 64-bit Calling Convention

你有两个选择:

  • 构建 32 位应用程序。在 Visual Studio 中,作为菜单栏的一部分,平台有一个下拉框。平台可以在工具栏中通过将x64更改为x86进行调整:

  • 修改代码以使用 64 位代码。要构建 64 位,您必须将 INVOKE 替换为 CALL 并且必须使用 Windows 64-bit calling convention。您可以删除 .STACK.MODEL.386 指令。通过使用PROC 类型声明它们EXTERN 来修改外部过程的定义。代码可能类似于:

    ; program 3.1
    ; sample Assembly program - MASM (64-bit)
    
    extern ExitProcess:PROC
    public mainCRTStartup
    
    .data
    sum DWORD 0
    
    .code
    mainCRTStartup PROC       ; Use mainCRTStartup if making a CONSOLE app without
                              ;   any C/C++ files AND if you haven't overridden the
                              ;   ENTRY point in the Visual Studio Project.
      sub rsp, 8+32           ; Align stack on 16 byte boundary and allocate 32 bytes
                              ;   of shadow space for call to ExitProcess. Shadow space
                              ;   is required for 64-bit Windows Calling Convention as
                              ;   is ensuring the stack is aligned on a 16 byte boundary
                              ;   at the point of making a call to a C library function
                              ;   or doing a WinAPI call.
      mov eax, 25
      mov ebx, 50
      add ebx, ebx
      mov sum, eax
    
      xor ecx, ecx            ; Set Error Code to 0 (RCX is 1st parameter)
      call ExitProcess
    mainCRTStartup ENDP
    END
    

    根据您是创建 GUI 还是 CONSOLE 应用程序以及您的项目是否存在 C/C++ 文件,您的环境的入口点可能会有所不同。

    与 32 位 Windows 代码不同,64 位调用约定中的函数名称不需要以下划线开头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2022-11-30
    • 2017-05-18
    相关资源
    最近更新 更多