Hey there,
I am currently writing a call macro in NASM (elf64), which can be used
like:
dcall FUNCTION, param1, param2, param3, ...
The parameters are handled differently, depending on their type. Numbers
become immediates, strings need to be placed in the .data section, and
are
then referenced by its address (label).
1 | %macro dcall 1-*
|
2 | %define _j %1
|
3 | %assign __pnum 1
|
4 | %assign __stck 0
|
5 | %rep %0-1
|
6 | %ifstr %2
|
7 | section .data
|
8 | %%str: db %2, 0h
|
9 | section .text
|
10 | %if __pnum = 1
|
11 | mov rdi, %%str
|
12 | %elif __pnum = 2
|
13 | mov rsi, %%str
|
14 | %elif __pnum = 3
|
15 | mov rdx, %%str
|
16 | %elif __pnum = 4
|
17 | mov rcx, %%str
|
18 | %elif __pnum = 5
|
19 | mov r8, %%str
|
20 | %elif __pnum = 6
|
21 | mov r9, %%str
|
22 | %else
|
23 | push %%str
|
24 | %assign __stck __stck+8
|
25 | %endif
|
26 | %else
|
27 | %if __pnum = 1
|
28 | mov rdi, %2
|
29 | %elif __pnum = 2
|
30 | mov rsi, %2
|
31 | %elif __pnum = 3
|
32 | mov rdx, %2
|
33 | %elif __pnum = 4
|
34 | mov rcx, %2
|
35 | %elif __pnum = 5
|
36 | mov r8, %2
|
37 | %elif __pnum = 6
|
38 | mov r9, %2
|
39 | %else
|
40 | push %2
|
41 | %assign __stck __stck+8
|
42 | %endif
|
43 | %endif
|
44 | %assign __pnum __pnum+1
|
45 | %rotate 1
|
46 | %endrep
|
47 | xor rax, rax
|
48 | call _j
|
49 | %if __stck != 0
|
50 | add rsp, __stck
|
51 | %endif
|
52 | %endmacro
|
This works fine, unless there is more than one string parameter. In that
case, the local %%str label is not unique any more. Does anyone know how
to deal with this? Is there a way using macro contexts?