ARM汇编中的矩阵乘法 [英] matrix multiplication in ARM assembly
问题描述
我最近在我的大学开始了一门ARM汇编课程,或者做的任务是创建一个NxM * MxP矩阵乘法程序,该程序从C代码中调用.
A course of ARM assembly recently started at my university, and or assignment is to create an NxM * MxP matrix multiplication programm, that is called from C code.
现在我对旁听者的知识非常有限,但是我非常愿意学习.我想知道的是:
Now I have a fairly limited knowledge in assambler, but i'm more than willing to learn. What I would like to know is this:
- 如何将2D数组从C读取/传递给ASM?
- 如何将2D数组输出回C?
我在想,我可以自己解决其余的问题,但这2点是我发现的困难.
I'm thinking, that i can figure the rest of this out by myself, but these 2 points are what I find difficult.
我在Ubuntu的qemu上的ARM汇编中使用此代码,它不在任何特定设备上使用.
I am using ARM assembly on qemu, on Ubuntu for this code, it's not going on any particular device.
推荐答案
C数组只是指针,因此当您将C数组作为assemply函数的参数传递时,您将获得一个指向内存区域的指针是数组的内容.
C arrays are merely just pointers, so when you pass a C array as an argument to an assemply function, you will get a pointer to an area of memory that is the content of the array.
要检索参数,取决于您使用的调用约定. ARM EABI 规定:
For retrieving the argument, it depends on what calling convention you use. The ARM EABI stipulates that:
前四个寄存器r0-r3(a1-a4)用于将参数值传递到子例程中并返回结果 函数的值.它们也可以用于在例程中保存中间值(但通常,仅 在子程序调用之间).
The first four registers r0-r3 (a1-a4) are used to pass argument values into a subroutine and to return a result value from a function. They may also be used to hold intermediate values within a routine (but, in general, only between subroutine calls).
对于简单函数,应根据函数签名在r0到r4中找到指向数组的指针.否则,您会在堆栈上找到它.准确找出ABI的一种好方法是反汇编调用汇编函数的C代码的目标文件,并在调用汇编函数之前检查其功能.
For simple functions, them, you should find the pointer to your array in r0 to r4 depending on your function signature. Otherwise, you will find it on the stack. A good technique to find out exactly what the ABI is would be to disassemble the object file of the C code that calls your assembly function and check what it does prior to calling your Assembly function.
例如,在Linux上,您可以在名为testasm.c
的文件中编译以下C代码:
For instance, on Linux, you can compile the following C code in a file called testasm.c
:
extern int myasmfunc(int *);
static int array[] = { 0, 1, 2 };
int mycfunc()
{
return myasmfunc(array);
}
然后使用以下代码进行编译:
Then compile it with:
arm-linux-gnueabi-gcc -c testasm.c
最后使用以下命令进行拆卸:
And finally get a disassembly with:
arm-linux-gnueabi-objdump -S testasm.o
结果是:
testasm.o: file format elf32-littlearm
Disassembly of section .text:
00000000 <mycfunc>:
0: e92d4800 push {fp, lr}
4: e28db004 add fp, sp, #4
8: e59f000c ldr r0, [pc, #12] ; 1c <mycfunc+0x1c>
c: ebfffffe bl 0 <myasmfunc>
10: e1a03000 mov r3, r0
14: e1a00003 mov r0, r3
18: e8bd8800 pop {fp, pc}
1c: 00000000 andeq r0, r0, r0
您可以看到通过将参数放入寄存器r0
中来调用单参数函数myasmfunc
. ldr r0, [pc, #12]
的含义是将pc+12
处的内存地址的内容加载到r0
中".那就是指向数组的指针的存储位置.
You can see that the single-parametered function myasmfunc
is called by putting the parameter into register r0
. The meaning of ldr r0, [pc, #12]
is "load into r0
the content of the memory address that is at pc+12
". That is where the pointer to the array is stored.
这篇关于ARM汇编中的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!