linux - 在 nasm 程序集中的 .data 段之外声明字符串

标签 linux gcc assembly intel nasm

有没有办法像这样在汇编代码的.text部分定义一个字符串指针?

SECTION .text          

        global main             


main:                        
        fmt:    dd      "%s", 10, 0 

或者可能构造了字符串并有一个指向它的寄存器,将所有这些都可以在 .text 部分中完成?

最佳答案

汇编程序非常愚蠢,你必须显式地编写所有内容,如下所示:

SECTION .text          

        global main             


main:                        
        ; Some code here, you don't want to execute data.

        mov     ebx, fmt ; ebx points to fmt[0] ('%')
        mov     eax, dword [pfmt] ; eax also points to fmt[0] ('%')

        ; Some more code here.

        pfmt    dd fmt ; pfmt is a constant pointer to fmt[0] ('%')
        fmt     db "%s", 10, 0 ; fmt is a constant string

您可以使用宏来简化编码:

%macro  LoadRegWithStrAddr 2+

        jmp     %%endstr
  %%str:        db      %2
  %%endstr:
        mov     %1, %%str

%endmacro

SECTION .text          

        global main             


main:                        
        LoadRegWithStrAddr ebx, "%s", 10, 0 ; ebx points to "%s\n"

LoadRegWithStrAddr ebx, "%s", 10, 0 扩展为:

        jmp     %%endstr
  %%str:        db      "%s", 10, 0
  %%endstr:
        mov     ebx, %%str

请参阅 NASM 文档。

关于linux - 在 nasm 程序集中的 .data 段之外声明字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526879/

相关文章:

linux - openSUSE 上的 OTRS 安装错误

使用 SSE 计算到多个字符串的汉明距离

c++ - 关于 C++ 编译器的几个问题 : GCC, MSVC、Clang、Comeau 等

c - 链接问题 : i386:x86-64 architecture of input file *. o 与 i386 输出不兼容

linux - SSH 连接到 Ubuntu 打开 SSH 服务器需要先通过密码登录(物理)服务器

Linux 服务器,定位只包含 4 个特定行的文件

c++ - 与子构造函数同名的继承类成员

assembly - nasm 程序集 : Can't find valid values for all labels after 1004 passes

assembly - ROR 打开溢出标志

linux - 如何在 vi 中使用 wq~ 时删除在我的其他 linux 目录中意外创建的 ~