定义内联字节注册到NASM中;将数据库字符串放入 .data 并通过一个源代码行获取指向它的指针?

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

有什么方法可以这样传递字节吗?

mov ecx, byte ["mybytes",0xa,0]

而不是:

section .data
    mybytes db "mybytes",0xa,0

section .text
    global main

main:
    mov ecx, mybytes
assembly x86 macros byte nasm
1个回答
1
投票

基于 NASM 手册中的示例,我创建了一个完美的宏:

%macro print 1+
        [section .rodata]        ; switch sections without updating the macro
                %%data db %1          ; expand all the macro args
                %%dataLen equ $- %%data
               ; db 0    ; optional 0 terminator outside the calculated length
                         ; only useful if you need an implicit-length C string 
        __?SECT?__               ; switch back to the user's  section
                mov eax, 4
                mov ebx, 1
                mov ecx, %%data
                mov edx, %%dataLen
                int 0x80
%endmacro

现在我可以简单地

print "string"
并且它有效!

参数的

1+
数量允许它像
print "string", 0xa
一样使用以包含换行符。


要将指针放入寄存器中,您可以编写类似的宏

%macro string_ptr 2+
        [section .rodata]
                %%data db %2
               ; db 0    ; optional 0 terminator baked into the macro
        __?SECT?__
                mov %1, %%data   ; or lea %1, [rel %%data]  for 64-bit
%endmacro

; use like this:
  string_ptr  ecx, "hello", 0xa, "world", 0
  push ecx
  call puts

对于 32 位堆栈参数调用约定,更高级的宏可能允许

push %%data
避免首先将其放入寄存器而浪费指令。或者一个单独的宏来推动,而不是制作一个灵活但复杂的宏。

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