【问题标题】:Print hello in 64-bit masm在 64 位 masm 中打印 hello
【发布时间】:2022-12-09 10:13:36
【问题描述】:

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

 Assembling: hello.asm

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

【问题讨论】:

  • 如果您自己从脚本运行这些命令会怎样?您是否收到任何错误消息或其他输出?
  • 此外,messenge 应使用 db 声明,而不是 dqmov rcx, messenge 不是将标签地址放入寄存器的正确方法。在 32 位代码中,您将使用 mov ecx, offset message(或 lea ecx, message),但我不知道 64 位代码是否有任何特殊注意事项(例如,rip-相对寻址)。
  • 有效!我将 dq 更改为 db,并将 mov rcx, messenge 更改为 mov rcx, offset message。非常感谢。

标签: assembly x86-64 masm


【解决方案1】:

我只用 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

基本上是迈克尔所说的。

【讨论】:

  • 我无法从 ml64 中获取 exe 文件,我想我没有正确使用它。所以我仍然使用我的脚本。即使没有库,这段代码也运行良好。谢谢。
  • 你记得从你的 ml64 行中删除 /c 吗?你应该只是做ml64 hello.asm。使用 gcc 并没有什么错,如果您使用汇编程序附带的链接器,它只会更干净一些。
  • 是的,我试过ml64 hello.asm。没有输出。
【解决方案2】:
猜你喜欢
  • 2015-05-04
  • 1970-01-01
  • 2021-03-02
  • 2012-03-07
  • 2017-06-02
  • 1970-01-01
  • 2017-06-22
  • 2012-04-30
  • 2021-03-24
相关资源
最近更新 更多