将字符数组传递给外部汇编函数 [英] passing an array of chars to external assembly function
问题描述
所以我的问题很基本,但我很难在互联网上找到任何东西.假设我想用 C 编写一个函数,该函数调用用 x86_64
程序集编写的外部 nasm 函数.
so my question is basic but i had a hard time finding anything on the internet.
lets say i want to write a function in C that calls an external nasm function written in x86_64
assembly.
我想向外部函数传递两个 char*
数字,对这两个数字执行一些算术运算并返回结果的 char*
.我的想法是以某种方式迭代 [rdi] 和 [rsi] 并将结果保存在 rax 中(即添加 rax、[rdi]、[rsi]),但我很难真正做到这一点.遍历每个角色的正确方法是什么?增加 [rsi] 和 [rdi]?而且 - 我只需要移动到 rax 第一个字符的值对吗?
I want to pass to the external function two char*
of numbers, preform some arithmetic operations on the two and return char*
of the result. My idea was to iterate over [rdi] and [rsi] somehow and saving the result in rax (i.e add rax, [rdi], [rsi]) but I'm having a hard time to actually do so. what would be the right way to go over each character? increasing [rsi] and [rdi]? and also- I would only need to move to rax the value of the first character right?
提前致谢!
推荐答案
如果您可以发布汇编/C 代码 - 提出更改建议会更容易.
If you could post assembly/C code - it would be easier to suggest changes.
对于任何程序集,我都会从 C 代码开始(因为我认为在 C 中 :))然后使用编译器转换为程序集,然后根据需要在程序集中对其进行优化.假设您需要编写一个函数,该函数接受两个字符串并将它们相加并以 int 形式返回结果,如下所示:
For any assembly, I would start with a C code(since I think in C :)) and then convert to assembly using a compiler and then optimize it in the assembly as needed. Assuming you need write a function which takes two strings and adds them and returns the result as int like the following:
int ext_asm_func(unsigned char *arg1, unsigned char *arg2, int len)
{
int i, result = 0;
for(i=0; i<len; i++) {
result += arg1[i] + arg2[i];
}
return result;
}
这是汇编(由 gcc https://godbolt.org/g/1N6vBT 生成):
Here is assembly (generated by gcc https://godbolt.org/g/1N6vBT):
ext_asm_func(unsigned char*, unsigned char*, int):
test edx, edx
jle .L4
lea r9d, [rdx-1]
xor eax, eax
xor edx, edx
add r9, 1
.L3:
movzx ecx, BYTE PTR [rdi+rdx]
movzx r8d, BYTE PTR [rsi+rdx]
add rdx, 1
add ecx, r8d
add eax, ecx
cmp r9, rdx
jne .L3
rep ret
.L4:
xor eax, eax
ret
这篇关于将字符数组传递给外部汇编函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!