用64位掩码打印hello



我是编程高手
我想写一个程序,用64位masm表示你好
我将VS代码与ml64.exe和gcc一起使用
以下是我所写的内容:

;; file name: hello.asm
printf proto
.data
messenge dq "hello", 0
.code
main proc
sub rsp, 40h
mov rcx, messenge
call printf
add rsp, 40h
ret
main endp
end

我写了一个脚本来组装、链接和执行:

@:: file name: run.cmd
@ml64 /c hello.asm
@gcc -o hello.exe hello.obj
@del *.obj
@hello.exe

它是这样的:

C:codeMASM>run.cmd
Microsoft (R) Macro Assembler (x64) Version 14.25.28614.0
Copyright (C) Microsoft Corporation.  All rights reserved.
Assembling: hello.asm

它没有输出hello字符串
我该如何修复它?

我只使用ml64 hello.asm(没有gcc(来构建它。

;; file name: hello.asm
printf proto
includelib msvcrt.lib
includelib legacy_stdio_definitions.lib
.data
messenge db "hello", 13, 0
.code
main proc
sub rsp, 40h
mov rcx, offset messenge
call printf
add rsp, 40h
ret
main endp
end

基本上就是迈克尔说的。

我在Windows 11中使用Visual Studio 2022。以下代码在我的机器上运行正常。

步骤1:创建一个包含以下内容的文件hello.asm

includelib ucrt.lib
includelib legacy_stdio_definitions.lib
includelib msvcrt.lib
option casemap:none
.data
; , 10 means line feed character (LF)
; , 0 means adding an terminating '' to the string
fmtStr byte 'Hello', 10, 0
.code
externdef printf:proc
externdef _CRT_INIT:proc
externdef exit:proc
main proc
call _CRT_INIT
push rbp
mov rbp, rsp
sub rsp, 32
lea rcx, fmtStr  ; lea: load the address of a variable into a register
call printf
xor ecx, ecx ; the first argument for exit() is setting to 0
call exit
main endp
end

步骤2:打开";x64本机工具VS 2022命令提示符";windows,汇编并链接代码

此步骤使用MASM汇编程序ml64.exe以及链接选项生成hello.exe:

ml64 hello.asm /link /subsystem:console /entry:main

步骤3:运行可执行文件

hello.exe

Forml64你好.asm要工作,您需要在命令行上将LIB环境变量设置为:

set LIB=C:\Program Files(x86(\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\LIB\x64;C: \Program Files(x86(\Windows Kits\10\Lib\10.0.18362.0\um\x64;C: \Program Files(x86(\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64

您可以使用文件资源管理器查找libcmt.lib、kernel32.lib和ucrt.lib的x64版本,并将它们替换为上述lib设置。

ml64.exe在C: \Program Files(x86(\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64您需要将其添加到PATH变量中。

然后:ml64你好.asm

它将创建可以运行的hello.exe。

最新更新