微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

x86-64 Linux NASM 在C ++文件中声明为函数的int型数组的函数parameter passing

我试着用这个build议来解决这个问题

对于Linux编程arr [],n,&a,&b在RDI,RSI,RDX和RCX中传递。

并且程序的输出没有正确地总结数组中的整数。 它输出一个很明显错误的数字。

下面find的两个文件是从这里find的原始的32位版本修改的。 http://mcs.uwsuper.edu/sb/224/Intro/c_asm.html

我想要的是编译一个程序集文件,该文件在名为array.cpp的C ++文件调用函数参数,然后将生成的目标文件array.o与g++链接起来。

我遇到的问题是要么将正确的寄存器传递到堆栈上,要么就是为rsi寄存器上的每个偏移量添加的字节数(由于每个堆栈元素都是64位,所以我使用了8个字节)。

也可能是因为rbp寄存器未正确加载到arrays地址和arrays 中元素数量的正确偏移处。

mov rcx,[rbp+24] ; array length mov rsi,[rbp+16] ; array address

无论如何,这里是array.cpp文件,下面是nasm文件,我把它叫做nasm_cpp.asm 。

他们编译,链接和运行

nasm -f elf64 nasm_cpp.asm -o array.o g++ -m64 array.cpp array.o ./a.out #include <iostream> using namespace std; extern "C" int array(int a[],int length); // external ASM procedure int main() { int a[] = { 10,10}; // array declaration int array_length = 2; // length of the array int sum = array(a,array_length); // call of the ASM procedure cout << "sum=" << sum << endl; // displaying the sum }

这是下面的nasm_cpp.asm

;nasm -f elf64 nasm_cpp.asm -o array.o ;g++ -m64 array.cpp array.o ;./a.out global array ; required for linker and NASM section .text ; start of the "CODE segment" array: push rbp mov rbp,rsp ; set up the rBP push rcx ; save used registers push rdi push rsi mov rcx,[rbp+16] ; array address xor rax,rax ; clear the sum value lp: add rax,[rsi] ; fetch an array element add rsi,8 ; move to another element loop lp ; loop over all elements pop rsi ; restore used registers pop rdi pop rcx pop rbp ret ; return to caller

我按照下面提出的意见,在问题的意见,现在的作品,cpp文件是一样的上面。

;nasm -f elf64 nasm_cpp.asm -o array.o ;g++ -m64 array.cpp array.o ;./a.out global array ; required for linker and NASM section .text ; start of the "CODE segment" array: push rbp mov rbp,rsp ; set up the rBP mov rcx,rsi ; array length mov rsi,rdi ; array address xor rax,rax ; clear the sum value lp: add eax,4 ; move to another element loop lp ; loop over all elements pop rbp ret ; return to caller

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐