Mips,如何读取数组并打印它们?

发布于 2024-09-02 20:11:17 字数 286 浏览 1 评论 0原文

好吧,C++ 和 java 我学习没有任何问题 当谈到 mips 时,这就像地狱一样

,好吧,我想学习如何读取数组并打印所有元素,

这是我写的一个简单数组,

int[] a = new int[20];

for(int i=0; i<a.length; i++){
  a[i]=1;
}

for(int j=0; j<a.length; j++){
  System.out.Println(a[i])
}

你如何在 mips 中做到这一点

okay, C++ and java i have no problem learning or what so ever
when it comes to mips it is like hell

okay i wanna learn how to read in the an array and print all the element out

here is a simple array that i wrote

int[] a = new int[20];

for(int i=0; i<a.length; i++){
  a[i]=1;
}

for(int j=0; j<a.length; j++){
  System.out.Println(a[i])
}

how do you do it in mips

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

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

发布评论

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

评论(1

葬﹪忆之殇 2024-09-09 20:11:17

假设您的数组地址位于寄存器 $a1 处,您可以执行以下操作:

    li $t0, 1
    move $t1, $a1
    addi $t2, $a1, 80
loop1:
    sw $t0, ($t1)
    addi $t1, $t1, 4
    bne $t1, $t2, loop1

move $t1, $a1

loop2:
    lw $t0, ($t1)
    li $v0, 1
    move $a0, $t0
    syscall
    addi $t1, $t1, 4
    bne $t1, $t2, loop2

此代码应该产生与 java 代码相同的结果,除了您使用 println (它将在新行中打印每个元素)并且此代码将在同一行中打印数组的所有元素。

我不知道你是否注意到了,但是你的Java代码和这段代码将打印全1,如果你想打印从1到19的数字,你将不得不在loop1内增加$t0

Assuming that you have your array address at register $a1, you can do the following:

    li $t0, 1
    move $t1, $a1
    addi $t2, $a1, 80
loop1:
    sw $t0, ($t1)
    addi $t1, $t1, 4
    bne $t1, $t2, loop1

move $t1, $a1

loop2:
    lw $t0, ($t1)
    li $v0, 1
    move $a0, $t0
    syscall
    addi $t1, $t1, 4
    bne $t1, $t2, loop2

This code should produce the same result as your java code, except that you used println (which will print each element in a new line) and this code will print all the elements of the array in the same line.

I don't know if you have noticed, but your Java code and this code will print all 1s, if you want to print numbers from 1 to 19, you will have to increment $t0, inside loop1

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