已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。
17小时前关闭
Improve this question
需要帮助将此C代码转换为x86汇编来执行矩阵乘法。无法使用数据节,必须为结果矩阵动态分配空间。
我根据这些规则写了这个C代码。然而,将其转换为C代码是一个挑战。有什么建议告诉我该怎么做吗?
C代码是:
//use this program to convert to assembly
#include <stdio.h>
#include <stdlib.h>
int** matMult(int **a, int num_rows_a, int num_cols_a, int** b, int num_rows_b, int num_cols_b){
//function describes a matrix multiplication code
int **result;
result = (int**)malloc(sizeof(int*)*num_rows_a);
for (int i = 0; i < num_rows_a; i++) {
result[i] = malloc(sizeof(int)*num_cols_b);
for (int j = 0; j < num_cols_b; j++) {
// Initialize the element to zero.
result[i][j] = 0;
for (int k = 0; k < num_cols_a; k++) {
// Accumulate the result
result[i][j] += a[i][k] * b[k][j];
}
}
}
return 0;
}
汇编代码(到目前为止):
.global matmult
.text
matmult:
prologue: # describes code that prepares the stack before executing the function
push %ebp # pushes base pointer onto the stack, sets up the stack frame
movl %esp, %ebp # moves value of stack pointer (esp) into base pointer (ebp)
subl $20, %esp # subtracts 20 bytes from the stack pointer which allocates space on the stack
# .equ is used to assign values to symbols
.equ a, 4
.equ num_rows_a, 8
.equ num_cols_a, 12
.equ b, 16
.equ num_rows_b, 20
.equ num_cols_b, 24
.equ i, -4
.equ j, -8
.equ k, -12l
.equ C, -16
.equ result, -20
movl num_rows_a(%ebp), %eax
movl $4, %ecx
epilogue: # trims stack to fixed allocated size
movl %ebp, %esp
pop %ebp # pops value from top of stack into pointer
ret # returns control from function to calling node
done:
nop
1条答案
按热度按时间llmtgqce1#
需要帮助将此C代码转换为x86汇编
我最喜欢的/默认也为这个工作是gcc。如果你不想为它设置管道,Compiler Explorer将在浏览器中转换它。
如果被要求,它甚至会为您优化代码,使其成为最有效的程序集,并提供与您的代码相同的结果: