Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 循环通过阵列mips_Arrays_Loops_Mips - Fatal编程技术网

Arrays 循环通过阵列mips

Arrays 循环通过阵列mips,arrays,loops,mips,Arrays,Loops,Mips,我正在做一个家庭作业,要求我计算数组中一系列十六进制值的灰度值。这是我理解的部分。我需要遍历这个值数组,直到遇到-1为止。以下是我得到的: # -------------------------------- # Below is the expected output. # # Converting pixels to grayscale: # 0 # 1 # 2 # 34 # 5 # 67 # 89 # Finished. # -- program is finished running

我正在做一个家庭作业,要求我计算数组中一系列十六进制值的灰度值。这是我理解的部分。我需要遍历这个值数组,直到遇到-1为止。以下是我得到的:

 # --------------------------------
# Below is the expected output.
# 
# Converting pixels to grayscale:
# 0
# 1
# 2
# 34
# 5
# 67
# 89
# Finished.
# -- program is finished running --
#---------------------------------

.data 0x0
    startString:    .asciiz "Converting pixels to grayscale:\n"
    finishString:   .asciiz "Finished."
    newline:    .asciiz "\n"
    pixels:     .word   0x00010000, 0x010101,   0x6,        0x3333, 
                0x030c,     0x700853,   0x294999,   -1

.text 0x3000

main:
    ori $v0, $0, 4              #System call code 4 for printing a string
    ori $a0, $0, 0x0            #address of startString is in $a0
    syscall                 #print the string

LOOP:   ori $a0, $0, 0x0 
    lw $t1, 48($a0)
    beq $t1 -1, exit
    addi $t4, $0, 3
    sll $t2, $t1, 8
    srl $s1, $t2, 24    #$s1 becomes red value
    sll $t2, $t1, 16
    srl $s2, $t2, 24    #$s2 becomes green value
    sll $t2, $t1, 24
    srl $s3, $t2, 24    #$s3 become blue value
    add $t1, $s1, $s2
    add $t1, $t1, $s3
    div $t1, $t4
    mflo $s4        #$s4 becomes grayscale value
    or $a0, $0, $s4
    ori $v0, $0, 1
    syscall
    ori $v0, $0, 4
    ori $a0, $0, 43
    syscall
    j LOOP

exit:

    ori $v0, $0, 4              #System call code 4 for printing a string
    ori $a0, $0, 33             #address of finishString is in $a0; we computed this
                        #  simply by counting the number of chars in startString,
                        #  including the \n and the terminating \0

    syscall                 #print the string

    ori $v0, $0, 10             #System call code 10 for exit
    syscall                 #exit the program

我知道对于循环的每次迭代,48需要增加4,我只是不知道如何在MIPS中做到这一点。非常感谢您的帮助

您应该使用一些寄存器来保存正在使用的数组的索引值,并在每次迭代中使用4进行增量注册

将常量放在数组所在的位置也是个坏主意,因为如果以后更改数组在内存中的位置,也必须更新该常量。取而代之的是,使用一个标签,让汇编器找出实际位置

假设我们使用寄存器$a1来保存索引。然后,我们只需要对您的代码进行一些小的更改:

    ori $a1, $0, 0x0   # Initialize index with 0
LOOP:  
    lw $t1, pixels($a1)  # We use the label name instead of the actual constant
         ...
         ...
    addi $a1, $a1, 4   # Increment index by 4
    j LOOP

我意识到用48而不是像素是不好的做法,我们的老师只是告诉我们现在就这样做哈哈。谢谢你的帮助!我知道现在该怎么办了。