visual-studio - 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:
Why am I getting these errors and how can I fix them?
解决方案
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
tox86
:Modify the code to work with 64-bit code. To build in 64-bit you have to replace
INVOKE
withCALL
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 themEXTERN
with aPROC
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.
推荐阅读
- php - PHP在收到表单后不重定向
- postgresql - 在 Postgres 存储过程中传递表值参数
- symfony - Sulu 大规模搜索包异常
- typescript - 用 RxJS 依次执行 observable,concat vs concatMap
- python-3.x - 在 python tkinter 的弹出窗口中放大图像
- java - 通过 Scala Spark 读取动态 Yaml 文件
- wildfly - keycloak rest api查询是否有Not条件
- angular - SCSS 中的 Angular Image 引用被复制到 dist 的根目录
- symfony - EasyAdminBundle - 按角色和组列出的用户列表
- encryption - IIS 10 - TLS 1.2(服务器没有偏好)