MIPS 中的二维阵列
我在网上和这个网站上进行了搜索,但找不到在 MIPS 中实现 2D 数组的好例子。我希望能够看到一个示例,说明如何遍历数组以便将数据放置在特定索引处以及如何打印数组,如下所示。
例如 5x5 数组,其中 $ 是每个索引中的数据。
a b c d e
1 $ $ $ $ $
2 $ $ $ $ $
3 $ $ $ $ $
4 $ $ $ $ $
5 $ $ $ $ $
I've searched online and on this site and I can not find a good example of implementing a 2D Array in MIPS. I would like to be able to see an example of how to go through the array in order to place data at a specific index and how to print the array out like shown below.
Such as a 5x5 array where $ would be the data in each index.
a b c d e
1 $ $ $ $ $
2 $ $ $ $ $
3 $ $ $ $ $
4 $ $ $ $ $
5 $ $ $ $ $
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于二维数组您需要了解的一切:
要分配您需要计算(#row X #column) X #byte required
有关字节数您需要 1 个字符、4 个整数、4 个单精度浮点数、8 个双精度浮点数。
例如:
要动态分配 150 个双精度元素的数组,例如 15 行和 10 列:
要获取 索引 (3,4) 的地址:
旁注:我使用逻辑左移而不是乘法因为 MIPS 中的移位 (
sll
) 需要 1 个时钟周期,而mul
指令需要 33 个时钟周期。从而,提高了代码的效率。更新/编辑(距离我写这篇文章已经过去 3 年多了)答案,所以我会改进我的答案):
以行主格式迭代整数(不是双精度)的二维矩阵的伪代码如下:
但是,迭代的伪代码列优先格式的整数(非双精度)二维矩阵如下:
注意:正如我们所见,循环的结构保持不变,但地址保持不变计算部分略有改变。现在实现上述伪代码很简单。我们需要 2 个嵌套循环。假设:
将值读入行优先矩阵的实现:
将值读入列优先矩阵的实现:
All you need to know about 2 Dimensional arrays:
To allocate you you need to calculate ( #row X #column ) X #byte needed
regarding number of bytes you need 1 for char, 4 integer, 4 single precision float, 8 for double precision float.
For example:
To dynamically allocate array of 150 double precision elements such that 15 rows and 10 column :
To get address of index (3,4) :
Side note: I used shift left logical instead of multiply because shifting (
sll
) in MIPS takes 1 clock cycle butmul
instruction takes 33 clock cycles. Thus, improving efficiency of the code.Update / Edit (it has been over 3 years past since I wrote this answer, so I will improve my answer):
The pseudo-code to iterate through 2 dimensional matrix of integers (not doubles) in row-major format is the following:
However, pseudo-code to iterate through 2 dimensional matrix of integers (not doubles) in column-major format is the following:
Note: As we can see, the structure of the loop stays the same but the address calculation part has been slightly changed. Now implementing the above pseudo-codes are straightforward. We need 2 nested loops. Assuming:
Implementation of reading values into row-major matrix:
Implementation of reading values into column-major matrix:
您可以根据一维数组设置二维数组。您只需将元素从一维数组正确映射到二维数组即可。该网站有图片:
http:// www.plantation-products.com/Webster/www.artofasm.com/Windows/HTML/Arraysa2.html#1010609
您可以使用标准格式来寻址每个单元格。例如:
您应该能够看到该模式:) 一般来说,如果有 M 列和 N 行,则可以在点 i * M + j - 1 处访问第 i 行、第 j 列(零索引)的单元格
You can set up a 2D array in terms of a 1D array. You just need to correctly map elements from the 1D array to the 2D array. This site has pictures:
http://www.plantation-productions.com/Webster/www.artofasm.com/Windows/HTML/Arraysa2.html#1010609
You can use a standard format for addressing each cell. For example:
You should be able to see the pattern :) In general, if there are M columns and N rows, the cell at row i, column j (zero-indexed) can be accessed at point i * M + j - 1