首页 > 解决方案 > Visual Studio C/C++ Project with MASM code produces error `error A2008: syntax error : .`

问题描述

I have a Visual Studio C/C++ project with a single assembly file main.asm in it. During the process of building the project I get the errors:

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

My code for main.asm is:

; 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

A screenshot of Visual Studio with my code and the errors:

 https://i.stack.imgur.com/VCtKg.jpg

Why am I getting these errors and how can I fix them?

标签: visual-studioassemblyx86masm

解决方案


Based on the errors you are showing on the line starting with .MODEL, .STACK, and .386 I can only gather that you are building for a 64-bit target rather than a 32-bit target. You likely also received errors related to the INVOKE directive as well. None of these directives are supported by 64-bit MASM and thus are generating errors. In 64-bit code the model is always assumed to be flat and the calling conventions for CDECL, STDCALL, THISCALL, FASTCALL etc are all the same and follow the Windows 64-bit Calling Convention.

You have two choices:

  • Build a 32-bit application. In Visual Studio as part of the menu bars there is a pull down box for the platform. The platform can be adjusted in the toolbar by changing x64 to x86:

    enter image description here

  • Modify the code to work with 64-bit code. To build in 64-bit you have to replace INVOKE with CALL and the Windows 64-bit calling convention must be used. You can remove the .STACK, .MODEL, and .386 directives. Modify the definition of the external procedures by declaring them EXTERN with a PROC type. The code could look something like:

    ; 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
    

    The entry point may be different for your environment depending on whether you are creating a GUI or CONSOLE application and whether your project has a C/C++ file present.

    Function names in the 64-bit calling convention are not required to start with an underscore unlike 32-bit Windows code.


推荐阅读