C语言 循环遍历阵列 MIPS 程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14916686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:21:58  来源:igfitidea点击:

Loop through an array MIPS Assembly

carraysassemblywhile-loopmips

提问by user2079483

I'm working on a program which loops through an array of 10 numbers. The first 9 elements have values higher than 0, the 10th has a value of 0. The loop should break when the 0 is encountered.

我正在开发一个程序,它循环遍历 10 个数字的数组。前 9 个元素的值大于 0,第 10 个元素的值为 0。当遇到 0 时循环应该中断。

i=0;
while(A[i]!=0)
{
    A[i]=A[i]+1;
    i++;
}

I know I can use 'beq' to break the loop if the value of the register is equal to 0. However I don't know enough about manipulating values in the memory.

我知道如果寄存器的值等于 0,我可以使用 'beq' 来中断循环。但是我对操作内存中的值知之甚少。

It's my first time using MIPS and you'll see it's a mess. If you can't fix it for me, can you give me some pointers?

这是我第一次使用 MIPS,你会发现它很混乱。如果你不能为我修复它,你能给我一些指点吗?

.data  #by default, the "data segment" starts at address 0x10010000
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0

.text #instructions start below

# MIPS assembly code

lui  $a0, 0x1001           # $a0 = 0x10010000
addi $a1, $zero, 0         # i = 0
jal increment              # call the procedure

Here's where I'm most lost:

这是我最迷失的地方:

increment:
lui $a0, 0x1001           # $a0 = 0x10010000
beq $a0, $zero, else      # if $a0 holds 0 goto 'else'
addi $a0, $a0, 2          # +2
addi $a1, $zero, 1        # i = i + 1

jr $ra                   #jump to caller

$v0 should hold the sum of all the incremented values.

$v0 应该保存所有递增值的总和。

else: 
add $a0, $v0, $zero #copy result as input to syscall
addi $v0,$zero,1 #service 1 for syscall is print integer
syscall

Finishes with an infinite loop.

以无限循环结束。

infinite: j infinite

回答by MByD

To load a value from a memory, you need to call one of the load instructions, (lw, lhor lbfor word, half-word and byte). for example:

要从内存中加载一个值,您需要调用加载指令之一, ( lwlh或者lb对于字、半字和字节)。例如:

lw $a1, 0($a2) # load a word from the address in $a2 + offset 0 to $a1

to write a value in memory, you use one of the store commands, for example:

要在内存中写入值,请使用 store 命令之一,例如:

sw $a1, 0($a2) # store the word in $a1 into the address in $a2 + offset

loading an address into a register is done using la, for example

例如,使用 la 将地址加载到寄存器中

la $a2, label_of_array # load the address of the label 'label_of_array' into $a2

Now, to manipulate the value in the array, you need to combine the three instructions from above:

现在,要操作数组中的值,您需要结合上面的三个指令:

la $a1, label_of_array   # load the address of the array into $a1
lb $a2, 0($a1)           # load a byte from the array into $a2
addi $a2, $a2, 1         # increment $a2 by 1
sb $a2, 0($a1)           # store the new value into memory
addi $a1, $a1, 1         # increment $a1 by one, to point to the next element in the array

And another point:

还有一点:

You wrote addi $a1, $zero, 1 # i = i + 1but this is wrong. What you did is to store the result of $zero + 1which is 1into $a1. In order to increment $a1, you need to write addi $a1, $a1, 1which is "store the result of $a1 + 1into $a1.

你写了,addi $a1, $zero, 1 # i = i + 1但这是错误的。你所做的是存储结果,$zero + 1这是1进入$a1。为了递增$a1,您需要编写addi $a1, $a1, 1which 是“将结果存储$a1 + 1$a1.