Windows下Sublime Text 3编写汇编Hello World报错求助
问题:Sublime Text 3编译汇编代码时出现
__init__() got an unexpected keyword argument 'output'错误 环境与相关代码
构建配置代码
{ "cmd": ["nasm", "-fwin32", "${file}"], "file_regex": "^[^/]*\\.asm$", "working_dir": "${file_path}", "output": "${file_path}/${file_base_name}.obj", "selector": "source.assembly" }
汇编代码
section .data hello_msg db 'Hello, World!', 0 section .text global _start extern GetStdHandle, WriteFile, ExitProcess _start: lea ecx, [hello_msg] push 0 push ecx push 13 push 0 push -11 call GetStdHandle mov ebx, eax call WriteFile xor eax, eax call ExitProcess
报错信息
__init__() got an unexpected keyword argument 'output' [cmd: ['nasm', '-fwin32', 'C:\\Users\\unknown\\Documents\\ASSEMBLY-CODES\\codes.asm']] [dir: C:\Users\unknown\Documents\ASSEMBLY-CODES] [path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\msys64\mingw64\bin;C:\Program Files (x86)\zig\zig-windows-x86_64-0.10.1;C:\Users\unknown\AppData\Local\Programs\Julia-1.9.2\bin;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files (x86)\NAG\EFBuilder 7.1\bin;C:\Users\unknown\AppData\Local\bin\NASM;C:\msys64\mingw64\bin] [Finished]
报错原因
Sublime Text的构建系统不支持output这个配置项,你错误地在构建配置中添加了这个无效参数,导致Sublime初始化构建流程时抛出参数错误。
NASM的输出文件路径不需要在Sublime配置里单独指定,而是要通过NASM的命令行参数-o来指定输出目标文件。
修正后的构建配置
移除output项,同时修改cmd参数,添加-o指定输出的obj文件:
{ "cmd": ["nasm", "-fwin32", "${file}", "-o", "${file_path}/${file_base_name}.obj"], "file_regex": "^[^/]*\\.asm$", "working_dir": "${file_path}", "selector": "source.assembly" }
额外提示
你的汇编代码还有两处问题需要修正才能正常运行:
WriteFile的参数压栈顺序错误:Windows API采用stdcall调用约定,参数需要从右往左压栈,你当前的压栈顺序不符合要求,会导致调用失败。- 缺少接收实际写入字节数的变量:
WriteFile需要一个指针参数来接收实际写入的字节数,需要在.data段定义对应变量。
修正后的汇编代码示例:
section .data hello_msg db 'Hello, World!', 0 bytes_written dd 0 section .text global _start extern GetStdHandle, WriteFile, ExitProcess _start: ; 获取标准输出句柄 push -11 ; STD_OUTPUT_HANDLE call GetStdHandle mov ebx, eax ; 保存句柄到ebx ; 调用WriteFile push 0 ; lpOverlapped = NULL push bytes_written ; lpNumberOfBytesWritten push 13 ; nNumberOfBytesToWrite push hello_msg ; lpBuffer push ebx ; hFile call WriteFile ; 退出程序 push 0 ; 退出码0 call ExitProcess
内容的提问来源于stack exchange,提问作者Bishop
相关产品推荐
相关产品推荐

