x86-64 Linux NASM.在C ++文件中声明为函数的int数组类型的函数参数传递 [英] x86-64 Linux NASM. Function parameter passing of type int array declared as a function in C++ file

查看:117
本文介绍了x86-64 Linux NASM.在C ++文件中声明为函数的int数组类型的函数参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此建议解决此问题

I tried using this advice for this problem

对于Linux编程arr [],n, & a,& b是通过RDI,RSI,RDX和RCX传递的.

并且程序的输出不能正确地对数组中的整数求和.它输出大量的数字,这显然是错误的.

and the output of the program doesn't sum up the integers in the array properly. It outputs a large number that is obviously wrong.

下面找到的两个文件是从此处找到的原始32位版本修改而来的. http://mcs.uwsuper.edu/sb/224/Intro/c_asm. html

The two files found below were modified from the original 32-bit version found here. http://mcs.uwsuper.edu/sb/224/Intro/c_asm.html

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

What I want is to compile an assembly file that calls a function parameter in a C++ file called array.cpp and then link the resulting object file array.o with g++.

我必须解决的问题是将适当的寄存器传递到堆栈上,或者可能是要为rsi寄存器上的每个偏移量添加的字节数(我使用8,因为每个堆栈元素为64位).

The problem I'm having has to do with either the passing of the proper registers onto the stack or maybe the number of bytes to add for each offset on the rsi register ( I used 8 since each stack element is 64 bits).

也可能是rbp寄存器未正确加载到数组地址元素数量在数组中的正确偏移处.

It could also be that the rbp register isn't properly loaded at the correct offsets of the array address and number of elements in the array.

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

无论如何,这是array.cpp文件,在它下面是nasm文件,我称之为nasm_cpp.asm.

Anyways, here's the array.cpp file and below it is the nasm file, I called it 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

This is nasm_cpp.asm below

;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+24]   ; array length
       mov rsi, [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文件与上面的相同.

I followed the suggestions in the comments posted below the question and it works now, the cpp file is the same as above.

;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, [rsi]      ; fetch an array element
       add rsi, 4         ; move to another element
       loop lp             ; loop over all elements    

       pop rbp

       ret                 ; return to caller

这篇关于x86-64 Linux NASM.在C ++文件中声明为函数的int数组类型的函数参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆