以 64 位 masm 打印 hello

问题描述 投票:0回答:3

我是编程小白。
我想写一个程序在 64 位 masm 中显示 hello。
我将 VS code 与 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:\code\MASM>run.cmd
Microsoft (R) Macro Assembler (x64) Version 14.25.28614.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: hello.asm

它没有输出 hello 字符串。
我该如何解决它?

assembly x86-64 masm
3个回答
4
投票

我只用

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

基本上就是迈克尔说的。


1
投票

我在 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 '\0' 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 Native Tools Command Prompt for VS 2022”窗口,汇编并链接代码

此步骤使用 MASM 汇编器

ml64.exe
以及链接选项来生成
hello.exe
:

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

步骤3:运行可执行文件

hello.exe

1
投票

为了

ml64 hello.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"

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

ml64.exe
位于:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\Hostx64\x64

您需要将其添加到 PATH 变量中。

然后:

ml64 hello.asm

它将创建您可以运行的

hello.exe

© www.soinside.com 2019 - 2024. All rights reserved.