有什么方法可以这样传递字节吗?
mov ecx, byte ["mybytes",0xa,0]
而不是:
section .data
mybytes db "mybytes",0xa,0
section .text
global main
main:
mov ecx, mybytes
基于 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
避免首先将其放入寄存器而浪费指令。或者一个单独的宏来推动,而不是制作一个灵活但复杂的宏。