在nasm程序集中声明字符串OUTSIDE .data段

时间:2011-12-15 21:30:25

标签: linux gcc assembly intel nasm

有没有办法在汇编代码的.text部分中定义字符串指针?

SECTION .text          

        global main             


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

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

1 个答案:

答案 0 :(得分:1)

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

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文档。