从C程序调用x86汇编函数时检索到的矩阵值不正确
我正在写一个C程序,将两个矩阵中两个单元格的值相加。在我的C程序中,我调用了一个汇编函数,该函数实现了添加两个单元格的功能。该函数具有以下签名:从C程序调用x86汇编函数时检索到的矩阵值不正确,c,assembly,x86,att,C,Assembly,X86,Att,我正在写一个C程序,将两个矩阵中两个单元格的值相加。在我的C程序中,我调用了一个汇编函数,该函数实现了添加两个单元格的功能。该函数具有以下签名: // Adds two matrix cells together and returns the result int addTwoCells(int **matrixA, int aRows, int aColumns, int cellARow, int cellAColumn, int **matrixB, int bRows,
// Adds two matrix cells together and returns the result
int addTwoCells(int **matrixA, int aRows, int aColumns, int cellARow, int cellAColumn,
int **matrixB, int bRows, int bColumns, int cellBRow, int cellBColumn);
下面是我的C程序(main.C
):我定义了2个动态分配的3x3矩阵,每个矩阵的右下角设置为3。我打算通过函数调用添加右下角:
#include <stdio.h>
#include <stdlib.h>
// Adds two matrix cells together and returns the result
int addTwoCells(int **matrixA, int aRows, int aColumns, int cellARow, int cellAColumn,
int **matrixB, int bRows, int bColumns, int cellBRow, int cellBColumn);
int main(void) {
// Define the dimensions for matrix A
int rA = 3;
int cA = 3;
// Define the double pointer that defines matrix A
int **matrixA;
// Dynamically allocate space for the entire matrix
matrixA = (int**) malloc(rA * sizeof(int*));
// Dynamically allocate sufficient space for each row in the entire matrix
for (int i = 0; i < rA; i++) {
matrixA[i] = (int*) malloc(cA * sizeof(int));
}
matrixA[2][2] = 3;
// Define the dimensions for matrix B
int rB = 3;
int cB = 3;
// Define the double pointer that defines matrix B
int **matrixB;
// Dynamically allocate space for the entire matrix
matrixB = (int**) malloc(rB * sizeof(int*));
// Dynamically allocate sufficient space for each row in the entire matrix
for (int i = 0; i < rB; i++) {
matrixB[i] = (int*) malloc(cB * sizeof(int));
}
matrixB[2][2] = 3;
// Test calling the matrix multiplication function using the static library
// compiled from the Assembly file
printf("%d\n", addTwoCells(matrixA, rA, cA, 2, 2, matrixB, rB, cB, 2, 2));
return EXIT_SUCCESS;
}
问题1:您忘记了矩阵B的起始内存地址。它应该是参数6,您当前所称的参数6-9应该变成7-10 问题2:
int
s在x86中有4个字节宽,但计算单元格地址时,就好像它们只有1个字节宽一样
问题3:您正在将双指针从C传递到addTwoCells
,但您的汇编代码将其视为2D数组。这些类型不兼容。尝试:
int readValuePtr(int行、int列、int单元格行、int单元格列、int**矩阵){
返回矩阵[cellRow][cellCol];
}
int readValueArr(int行、int列、int单元格行、int单元格列、int矩阵[rows][cols]){
返回矩阵[cellRow][cellCol];
}
我认为有两个问题:1)矩阵不是平面的,它是一个行指针数组。2) 程序集使用字节索引,您似乎既不按元素大小也不按指针大小进行缩放
# Function signature:
# int addTwoMatrixCells(int **matrixA, int aRows, int aColumns, int cellARow, int cellAColumn,
# int **matrixB, int bRows, int BColumns, int cellBRow, int cellBColumn)
.data
# Matrix A dimensions
aRows:
.int 0
aColumns:
.int 0
# Coordinate of cell A
cellARow:
.int 0
cellAColumn:
.int 0
# Matrix B dimensions
bRows:
.int 0
bColumns:
.int 0
# Coordinate of cell B
cellBRow:
.int 0
cellBColumn:
.int 0
# The starting memory address of matrix A
matrixAStartingAddress:
.int 0
# The starting memory address of matrix B
matrixBStartingAddress:
.int 0
.text
# Defining a function addTwoMatrixCells
.global addTwoCells
addTwoCells:
# Prologue
push %ebp
movl %esp, %ebp
# Process in parameter 1: the starting memory address of matrix A
movl 8(%ebp), %ecx
movl %ecx, matrixAStartingAddress
# Process in parameter 2: the number of rows in matrix A
movl 12(%ebp), %ecx
movl %ecx, aRows
# Process in parameter 3: the number of columns in matrix A
movl 16(%ebp), %ecx
movl %ecx, aColumns
# Process in parameter 4: the row index of cellA
movl 20(%ebp), %ecx
movl %ecx, cellARow
# Process in parameter 5: the column index of cellA
movl 24(%ebp), %ecx
movl %ecx, cellAColumn
# Process in parameter 6: the number of rows in matrix B
movl 28(%ebp), %ecx
movl %ecx, bRows
# Process in parameter 7: the number of columns in matrix B
movl 32(%ebp), %ecx
movl %ecx, bColumns
# Process in parameter 8: the row index of cellB
movl 36(%ebp), %ecx
movl %ecx, cellBRow
# Process in parameter 9: the column index of cellB
movl 40(%ebp), %ecx
movl %ecx, cellBColumn
# Compute the flattened index of the cell in matrix A
# One of the arithmetic operators needs to be in a CUP register
# In this case, we designate ECX register to store the number of columns
# in matrix A (n) initially, but ECX after the computation will store
# the memory address of cellA in the matrix
movl aColumns, %ecx
imul cellARow, %ecx
addl cellAColumn, %ecx
addl matrixAStartingAddress, %ecx
movl (%ecx), %eax
# Compute the flattened index of the cell in matrix B
# Epilogue
movl %ebp, %esp
pop %ebp
ret