简体   繁体   English

MIPS 汇编为什么这些寄存器加载相同的数组值?

[英]MIPS Assembly why are these registers loading the same array values?

I'm trying to load in array values in the first and second position but for some reason it's loading in only the value in the first position?我正在尝试在第一个和第二个 position 中加载数组值,但由于某种原因它只加载第一个 position 中的值? Been trying to figure it out for an hour but no luck.一直试图弄清楚一个小时,但没有运气。 This is the code I have.这是我的代码。 I set $t1 to 0 and $t3 to 4 so I should be loading in the first and second value, yet it only loads in the first value for both $t1 and $t3?我将 $t1 设置为 0,将 $t3 设置为 4,所以我应该加载第一个和第二个值,但它只为 $t1 和 $t3 加载第一个值?

 addi $t1, $zero, 0 
 addi $t3, $zero, 4
 
 
 
 FindLessThenLoop:
 la $t1, myArray # Load first number
 addu $t1, $zero, $t1 # add offset and base together
 lw $t1, ($t1)      # fetch the data 
 
 la $t3, myArray # Load second number
 addu $t3, $zero, $t3 # add offset and base together
 lw $t3, ($t3)      # fetch the data 

The code for the first and the second are essentially the same except using a different register, so, we would expect the same results: the value from array position/index 0.第一个和第二个的代码除了使用不同的寄存器外基本相同,因此,我们期望得到相同的结果:数组位置/索引 0 的值。

To get the second element, you can do either:要获取第二个元素,您可以执行以下任一操作:

la $t3, myArray     # Load second number
addiu $t3, $t3, 4   # add offset and base together
lw $t3, ($t3)       # fetch the data 

-or- -或者-

la $t3, myArray     # Load second number
lw $t3, 4($t3)      # fetch the data with offset 4

You'll prefer a variation of the first form if you're going to do variable indexing (eg a[i]), say in a loop, whereas the second form is for constant indexing (eg a[1]).如果您要进行变量索引(例如 a[i]),例如在循环中,您会更喜欢第一种形式的变体,而第二种形式用于常量索引(例如 a[1])。

If you have an integer index, then the following is useful:如果您有一个 integer 索引,那么以下内容很有用:

# usually done outside of and before a loop:
la $t3, myArray     # Load second number

# this part done inside a loop:
sll $t4, $t2, 2     # scale the integer index by 4
addu $t4, $t3, $t4  # add base and offset
lw $t1, ($t4)       # fetch the data 

Note the following:请注意以下事项:

  1. there is pre-loop setup of $t3$t3的预循环设置
  2. inside the loop we preserve $t3 so it can be used each iteration在循环中,我们保留$t3以便每次迭代都可以使用它
  3. we scale the simple integer index by size of the elements (4x)我们按元素的大小缩放简单的 integer 索引 (4x)

An alternative is pointers, which are fairly easy in assembly, but suggest to convert your algorithm to pointers in C first, then take to assembly.另一种方法是指针,它在汇编中相当容易,但建议先将您的算法转换为 C 中的指针,然后再进行汇编。

A pointer combines into one variable the notion of base + scaled offset.指针将基数 + 缩放偏移量的概念组合到一个变量中。 A pointer can be incremented to refer to the next element.指针可以递增以指向下一个元素。


Here's a simple illustrative loop that does max value of array, assuming elements are positive:这是一个简单的说明性循环,假设元素为正,它计算数组的最大值:

    li $t0, array # t0 is the base array address: &array[0]
    li $t1, 0     # t1 is min value variable, initially zero
    li $t2, 0     # t2 is the index, aka loop control variable call it "i"
    li $t3, 10    # where to stop with "i"

loop1:
    beq $t2, $t3, loop1End   # if "i" == 10 we're done

    sll $t4, $t2, 2          # scale index by 4 due to word-sized elements
    add $t4, $t0, $t4        # add base and scaled offset
    lw $t4, 0($t4)           # fetch element from a[i]

    ble $t4, $t1, if1End
    move $t1, $t4            # capture the new max, if larger
if1End:

    addi $t2, $t2, 1         # next index position
    j loop1

loop1End:
    ...                      # t1 holds max value from the array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM