将C循环的逻辑分解为我可以在MIP中实现的东西吗? (数组中计数数字出现)

发布于 2025-01-28 18:44:26 字数 532 浏览 2 评论 0原文

嗨,我将我的C代码

for (int i=0;i<count;i++)
  {
        h[a[i]]++;
  }

转换

MIP

b [] = $ t2

li $s1,0

for:
bgt $s1,[size of array],end

lw $t3,($t1)
la $t1,4($t3)
lw $t4,($t2)
la $t2,4($t2)
sw $t3,0($t4)

add $s1,$s1,1
j for

我知道这是错误的。但是在MIPS中,它给我错误的地址错误和异常7和4错误


,以及如何将数组长度放入MIPS寄存器中,例如如何C可以计算您的元素数量而无需硬编码吗?

int arr[]={1,3,5,7,9};
int n=sizeof(arr)/sizeof(arr[0]);

Hi all i am converting my C code to MIPS but problem is here i couldn't make correct logic for this

for (int i=0;i<count;i++)
  {
        h[a[i]]++;
  }

as far i make my own logic that is wrong

Assume That

a[]=$t1
b[]=$t2

li $s1,0

for:
bgt $s1,[size of array],end

lw $t3,($t1)
la $t1,4($t3)
lw $t4,($t2)
la $t2,4($t2)
sw $t3,0($t4)

add $s1,$s1,1
j for

i know this is wrong .. but in MIPS it gives me bad address error and exception 7 and 4 error


Also, how to get the array length into a MIPS register, like how C can calculate the number of elements for you without having to hard-code it?

int arr[]={1,3,5,7,9};
int n=sizeof(arr)/sizeof(arr[0]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-02-04 18:44:26

您需要了解正在增加的内容。&nbsp;获得这种理解的一种方法是分解:可以使用

然后,建议分解以下内容:

    h[a[i]]++;

如下:

    int av = a[i];
    h[av]++;

在这种方法中,我们将各种操作分开&amp;表达式的元素成其自己的代码行,并使用引入的变量重新连接它们,也就是临时或短期变量。

我们可以继续分解:

    int av = a[i];

    // decompose h[av]++;
    int *hp = h + av; 
    *hp = *hp + 1;

但仍然:

    int av = a[i];

    int *hp = h + av;

    // decompose *hp = *hp + 1;
    int  hv = *hp;
    hv++;
    *hp = hv;

这是尽可能分解的。&nbsp;希望您可以(1)将其转化为MIPS组装,(2)了解分解技术。


请注意,在c语言中,当数组a具有int元素时,例如a + ih + jsize> sizef(int),ie 4,称为索引的隐藏乘法(ij)在添加之前发生的缩放 - 此比例缩放在C中必须在组装中明确进行。


控制结构,IE结构化语句也可以分解:

for (i = 0; i < count; i++) {
    ???
}

第一次分解:

i = 0;
while (i < count) {
    ???
    i++;
}

进一步介绍IF-Goto-Label样式:

    i = 0;
loop1:
    if (i >= count) goto endLoop1;
    ???
    i++;
    goto loop1;
endLoop1:

注意循环退出条件如何反转(否定) - 这是因为在If-Goto-Label中,我们正在写何时写信退出循环,而不是何时继续循环,如/wher中的c中,这完全相反。

(并非总是如此:一个事时条件也说何时继续进行循环,在组装中,因为这是将循环下一次迭代放置向后分支的自然场所 - 而if-goto-label版本的条件意义也相同 - 它们不是对立的 - 而a 在pascal中重复直到使用相反的条件意义。)

另外反转/否定/对面的&nbsp; &nbsp; &lt;&nbsp; &nbsp; is&nbsp; &nbsp; &gt; =&nbsp;。


分解是通过逻辑等价的转换来简化的。&nbsp;只要我们保持逻辑等效性,分解就会与原始的相同。

You need to understand what is being incremented.  One approach to gaining this understanding is decomposition: expressions can be decomposed using an approach like Three-Address Code.

Suggest then, to decompose this:

    h[a[i]]++;

as follows:

    int av = a[i];
    h[av]++;

In this approach, we separate various operations & elements of the expression into their own lines of code, and reconnect them using an introduced variable, aka a temporary or short-lived variable.

and we can continue decomposition:

    int av = a[i];

    // decompose h[av]++;
    int *hp = h + av; 
    *hp = *hp + 1;

and yet still:

    int av = a[i];

    int *hp = h + av;

    // decompose *hp = *hp + 1;
    int  hv = *hp;
    hv++;
    *hp = hv;

That's about as decomposed as can be.  Hopefully, you can (1) translate that into MIPS assembly and (2) learn of the technique of decomposition.


Note that in the C language, when an array, a has int elements, in an expression such as a + i and h + j there is a hidden multiplication of the index (i and j) by sizeof(int), i.e. 4, called scaling that occurs before the addition — this scaling, automatic in C, must be explicitly done in assembly.


Control structures, i.e. structured statements can also be decomposed:

for (i = 0; i < count; i++) {
    ???
}

first decomposition:

i = 0;
while (i < count) {
    ???
    i++;
}

and further into if-goto-label style:

    i = 0;
loop1:
    if (i >= count) goto endLoop1;
    ???
    i++;
    goto loop1;
endLoop1:

Notice how the loop exit condition is inverted (negated) — this because in if-goto-label, we are writing when to exit the loop rather than when to continue the loop as in C for/while, which represents the exact opposite.

(This is not always the case: a do-while condition says when to continue the loop as well, and in assembly, since that is the natural place to put the backward branch for the next iteration of the loop, the condition test for do-while and if-goto-label versions will have the same condition sense — they are not opposites — whereas a repeat-until in Pascal uses the opposite condition sense.)

Also NB: the inversion/negation/opposite of   <   is   >= .


Decomposition is simplification by transformations of logical equivalence.  As long as we maintain logical equivalence, the decomposition will run the same as the original.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文