内存地址的二维数组运算(MIPS汇编code) [英] 2d array computing of memory address(MIPS assembly code)

查看:2958
本文介绍了内存地址的二维数组运算(MIPS汇编code)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解如何计算二维阵列的存储器地址的问题。
(对于MIPS汇编code)

这是我在网上找到,计算存储器地址二维数组:
INT基地[X] [Y]
地址=基[索引1] [索引2]
存储器地址=(基地址)+(索引1 * X *整型尺寸(INT))+(索引2 *整型尺寸(INT))

让我们说我有一个int数组[5] [5],基地址为0x100。
欲查找是存储器地址为[4] [3]和[2] [5]

任何人都可以告诉我它是怎么样的一个例子吗?还是有什么替代方案来计算的内存地址?但请不要告诉我的它是如何被计算的例子。

感谢。


解决方案

  

让我们说我有一个int数组[5] [5],基地址为0x100。欲查找是存储器地址为[4] [3]和[2] [5]


让我们先对安置值一些假设。因为你的语言是组装,还有你会如何存储阵列没有限制。可以按行存储它们或按列,也可以是从0或1或基于17为主。它们甚至可以潜在锯齿(内部指针数组)。但是,我们假设最自然的情况下 - 二维数组按行存储0为主。这就是C约定,并装配最容易实现的。

也就是说,你的行的大小为5 *的sizeof(int)的= 20个字节。因此,对于元件的偏移[4] [3]将基地+的sizeof(行​​)* 4 +的sizeof(int)的* 3 = 0x100的+ 20 * 4 + 4 * 3 = 256 + 80 + 12 = 348。

类似地,对于偏移[2] [5]将0x100的+ 20 * 2 + 4 * 5 = 316,除了[2] [5]是用词不当 - 在大小5的从零开始的数组最上面的法律索引是4 [2] [5]的相同[3] [0]

编辑:的sizeof(int)的是最现代化的系统4个字节。 MIPS拥有64位模式,但它是颇为奇特的;你刚才提到,如果你会一直有针对性的。

的sizeof(行​​):数组有5个元素的行,每一个元素是一个int。问题的措词强烈暗示阵列是廉政局的。

I have problems understanding how to compute the memory address of a 2-dimensional array. (For MIPS assembly code)

This is what I found online to calculate the memory address for 2d array: int base[x][y] address = base[index1][index2] Memory Address = (base address) + (index1 * x * sizeOf(int)) +(index2 * sizeOf(int))

Let's say I have a int array [5][5], base address 0x100. I want to find what is the memory address for [4][3] and [2][5].

Anyone can show me an example of how is it like? or are there any alternatives to calculate the memory address? but please do show me an example of how is it being computed.

Thanks.

解决方案

Let's say I have a int array [5][5], base address 0x100. I want to find what is the memory address for [4][3] and [2][5].

Let's make some assumptions regarding value placement first. Since your language is assembly, there's no limitations on how you would store your arrays. You can store them by rows or by columns, they can be 0-based or 1-based or 17-based. They can even be potentially jagged (arrays of pointers internally). But we'll assume the most natural case - 2D arrays are stored by row, 0-based. That's the C convention, and the easiest to implement in assembly.

That said, the size of your row is 5*sizeof(int) = 20 bytes. So the offset for element [4][3] would be base + sizeof(row)*4 + sizeof(int)*3 = 0x100 + 20*4 + 4*3 = 256 + 80 + 12 = 348.

Similarly, the offset for [2][5] would be 0x100 + 20*2 + 4*5 = 316, except [2][5] is a misnomer - in a zero-based array of size 5 the topmost legal index is 4. [2][5] is the same as [3][0].

EDIT: sizeof(int) is 4 bytes on most modern systems. MIPS has a 64-bit mode, but it's rather exotic; you would've mentioned if you would've targeted that.

sizeof(row): the array has rows of 5 elements, each element is an int. The wording of the question strongly suggests that the array is that of int's.

这篇关于内存地址的二维数组运算(MIPS汇编code)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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