Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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
Assembly 如何在MIPS中使用单词索引字节数组?_Assembly_Indexing_Bytearray_Word_Mips - Fatal编程技术网

Assembly 如何在MIPS中使用单词索引字节数组?

Assembly 如何在MIPS中使用单词索引字节数组?,assembly,indexing,bytearray,word,mips,Assembly,Indexing,Bytearray,Word,Mips,好的,我有一个关于在MIPS中寻址数组的问题 比如说,我有这样的东西: lw $t9,0($t5) # $t9 is an alias for some value in memory. Let's call it "var." 假设我想使用“var”索引一个字节数组: 我得到一个算术溢出错误,我假设这是因为我试图将一个字的值添加到数组的地址 这是我的实际代码,如果上面没有意义。我正在尝试将C函数转换为MIPS: for (int i = 0; i < 256; i++) {

好的,我有一个关于在MIPS中寻址数组的问题

比如说,我有这样的东西:

lw $t9,0($t5)   # $t9 is an alias for some value in memory.  Let's call it "var."
假设我想使用“var”索引一个字节数组:

我得到一个算术溢出错误,我假设这是因为我试图将一个字的值添加到数组的地址


这是我的实际代码,如果上面没有意义。我正在尝试将C函数转换为MIPS:

for (int i = 0; i < 256; i++)   {
    pair_table[i] = 0;
    is_closer[i] = false;
}
for (int i = 0; pairs[i]; i += 2)  {
    const char op = pairs[i];
    const char cl = pairs[i+1];
    is_closer[cl] = true;
    pair_table[op] = cl;
 }
for(int i=0;i<256;i++){
配对表[i]=0;
is_closer[i]=false;
}
对于(int i=0;pairs[i];i+=2){
const char op=成对[i];
const char cl=成对[i+1];
is_closer[cl]=真;
配对表[op]=cl;
}
这是我的MIPS翻译。我相信这就是我区分单词和字节的方式,因为如果我使用lb而不是lw,代码将执行。但是,输出是错误的,因为我需要整个单词而不是一个字节。我的问题是如何实际索引单词值作为数组的索引

    la $a0,pair_table          # $a0 will hold address of pair_table.
    addi $a1,$0,256            # $a1 will hold size of tables (256) for both pair_table and is_closer.
    move $t1,$0                # Set $t1 to 0 (serves as counter for number of entries in table; can't have over 256).
    move $t2,$0                # Set $t2 to 0 (serves as counter for number of times branch to filltable has occurred).

filltable: 
    sb $0,0($a0)               # current location in table set to 0 (0 also serving as false in is_closer's case)
    addi $a0,$a0,1             # Increment current address of table by 1.
    addi $t1,$t1,1             # Increment number of entries by 1.
    slt $t3,$t1,$a1            # $t3 = (index total < 256)
    bne $t3,$0,filltable       # If (index total < 256), continue filling table.
    nop 
    bgtz $t2,setpairs         # If $t2's count is greater than 0, both pair_table and is_closer are filled.  Time to fill pairs table.
    nop

    la $a0,is_closer       # $a0 holds address of is_closer.
    move $t1,$0                # Reset $t1 to 0 (counter for number of table entries).
    addi $t2,$t2,1             # Increment filltable counter by 1.
    addi $t4,$0,1              # $t4 will hold the value 1 (true in this case).
    sb $t4,0($a0)              # Store "true" in the first entry of is_closer.
    addi $a0,$a0,1             # Go to next location in is_closer.
    addi $t1,$t1,1             # Increment number of entries by 1.
    b filltable                # Continue filling rest of is_closer table entries with 0 (false).
    nop


setpairs:   
    la $a0,pairs               # Get address of pairs table.
    move $t1,$0                # Start at index 0.
fillpairs: 
    sll $t2,$t1,2              # $t2 = current index * 4
    add $t0,$a0,$t2            # $t0 = address of pair at index
    lb $t9,0($t0)              # Get value of pairs[index].    //op//
 #  lw $t9,0($t0) ####################
    addi $t0,$t0,4             # Get next location in pairs (pairs[index+1]).  
    lb $t3,0($t0)              # Get value of pairs[index+1].  //cl//
#   lw $t3,0($t0) ####################

    la $a1,is_closer           # Get starting address of is_closer table.
    add $t6,$a1,$t3            # Get address of is_closer[cl].
    sb $t4,0($t6)              # Set is_closer[cl] to true (1).
#   sw $t4,0($t6)

    la $a2,pair_table          # Get starting address of pair_table.
    add $t5,$a2,$t9            # Get address of pair_table[op].
    sb $t3,0($t5)              # pair_table[op] = cl
#   sw $t3,0($t5) #####################

    addi $t1,$t1,2             # index += 2

    bnez $t9,fillpairs         # If pairs[i] ($t9) not yet NULL terminated, continue filling the table.
    nop

    jr $ra
    nop
la$a0,pair_表#$a0将保存pair_表的地址。
addi$a1,$0256#$a1将容纳两对表的表大小(256),并且更接近。
移动$t1,$0#将$t1设置为0(用作表中条目数的计数器;不能超过256条)。
移动$t2,$0#将$t2设置为0(用作计数器,指示发生到filltable的分支的次数)。
可填充表格:
sb$0,0($a0)#表中的当前位置设置为0(在is_closer的情况下,0也为假)
addi$a0,$a0,1#将表的当前地址增加1。
addi$t1,$t1,1#将条目数增加1。
slt$t3、$t1、$a1#$t3=(索引总数<256)
bne$t3,$0,填写表格#如果(索引总数<256),继续填写表格。
不
bgtz$t2,setpairs#如果$t2的计数大于0,则pair_表和is_closer都将被填充。是时候填对表了。
不
la$a0,is#u closer#$a0拥有is#u closer的地址。
移动$t1,$0#将$t1重置为0(表条目数计数器)。
addi$t2,$t2,1#将可填充计数器增加1。
addi$t4,$0,1#$t4将保存值1(在本例中为true)。
sb$t4,0($a0)#在is#U closer的第一个条目中存储“true”。
addi$a0,$a0,1#前往距离较近的is#的下一个位置。
addi$t1,$t1,1#将条目数增加1。
b filltable#继续用0(false)填充其余is_closer表条目。
不
集合对:
la$a0,pairs#获取pairs表的地址。
移动$t1,$0#从索引0开始。
填充对:
sll$t2,$t1,2#$t2=当前指数*4
添加$t0、$a0、$t2#$t0=索引处对的地址
lb$t9,0($t0)#获取对的值[索引]//op//
#lw$t9,0($t0)####################
addi$t0,$t0,4#成对获得下一个位置(成对[索引+1])。
lb$t3,0($t0)#获取对的值[索引+1]//氯离子//
#lw$t3,0($t0)####################
la$a1,is#u closer#获取is#u closer表的起始地址。
添加$t6、$a1、$t3#获取is#U closer[cl]的地址。
sb$t4,0($t6)#设置更接近于[cl]真值(1)。
#sw$t4,0($t6)
la$a2,pair_table#获取pair_table的起始地址。
添加$t5、$a2、$t9#获取配对表的地址[op]。
sb$t3,0($t5)#配对表[op]=cl
#sw$t3,0($t5)#####################
addi$t1,$t1,2#索引+=2
bnez$t9,fillpairs#如果pairs[i]($t9)尚未以NULL结尾,则继续填充该表。
不
jr$ra
不

假设您的
是一个4字节的整数数组,如果过于复杂,您的代码看起来是正确的。一个问题是,您正在检查循环体之后的终止条件,但C版本在循环体之前进行测试

至于算术溢出,您应该使用不会产生该错误的
addu
/
addu
。不过,你可能在某个地方遇到了问题


像往常一样,您应该使用调试器/模拟器逐步检查代码,看看哪里出错。

就可以了。当我完成时,我会把我的解决方案代码贴出来,以备任何人需要参考。在MIPS中,我没有看到过很多数组示例,其中您使用索引的方式与我使用它们的方式相同。我正在使用其他变量作为索引。另外,只是补充说明:终止条件应该可以在循环体之后进行检查。它基本上做同样的事情。在代码中,将b fillpairs放回分支,然后检查pairs[i]是否为真,这看起来非常可怕。基本上,我也在做同样的事情。事实上,我认为在MIPS中,在最后检查循环条件是相当标准的;如果条件仍然满足,则进行分支。它没有区别,占用更少的代码行。一般来说,即使终止条件已经为真,是否执行循环体也会有区别。在您的情况下,这可能并不重要,但它仍然不是C代码的转录本。请注意,asm代码将
is_closer[pairs[i+1]
设置为true,其中
i+1
超过终止符。C代码从不使用这个元素,它可能超出范围或包含垃圾。哦,好的,我明白你的意思。我来看看。正如我所说,我将更多地使用代码,但我肯定会为任何其他MIPS新手发布最终产品。谢谢你的评论和帮助!
    la $a0,pair_table          # $a0 will hold address of pair_table.
    addi $a1,$0,256            # $a1 will hold size of tables (256) for both pair_table and is_closer.
    move $t1,$0                # Set $t1 to 0 (serves as counter for number of entries in table; can't have over 256).
    move $t2,$0                # Set $t2 to 0 (serves as counter for number of times branch to filltable has occurred).

filltable: 
    sb $0,0($a0)               # current location in table set to 0 (0 also serving as false in is_closer's case)
    addi $a0,$a0,1             # Increment current address of table by 1.
    addi $t1,$t1,1             # Increment number of entries by 1.
    slt $t3,$t1,$a1            # $t3 = (index total < 256)
    bne $t3,$0,filltable       # If (index total < 256), continue filling table.
    nop 
    bgtz $t2,setpairs         # If $t2's count is greater than 0, both pair_table and is_closer are filled.  Time to fill pairs table.
    nop

    la $a0,is_closer       # $a0 holds address of is_closer.
    move $t1,$0                # Reset $t1 to 0 (counter for number of table entries).
    addi $t2,$t2,1             # Increment filltable counter by 1.
    addi $t4,$0,1              # $t4 will hold the value 1 (true in this case).
    sb $t4,0($a0)              # Store "true" in the first entry of is_closer.
    addi $a0,$a0,1             # Go to next location in is_closer.
    addi $t1,$t1,1             # Increment number of entries by 1.
    b filltable                # Continue filling rest of is_closer table entries with 0 (false).
    nop


setpairs:   
    la $a0,pairs               # Get address of pairs table.
    move $t1,$0                # Start at index 0.
fillpairs: 
    sll $t2,$t1,2              # $t2 = current index * 4
    add $t0,$a0,$t2            # $t0 = address of pair at index
    lb $t9,0($t0)              # Get value of pairs[index].    //op//
 #  lw $t9,0($t0) ####################
    addi $t0,$t0,4             # Get next location in pairs (pairs[index+1]).  
    lb $t3,0($t0)              # Get value of pairs[index+1].  //cl//
#   lw $t3,0($t0) ####################

    la $a1,is_closer           # Get starting address of is_closer table.
    add $t6,$a1,$t3            # Get address of is_closer[cl].
    sb $t4,0($t6)              # Set is_closer[cl] to true (1).
#   sw $t4,0($t6)

    la $a2,pair_table          # Get starting address of pair_table.
    add $t5,$a2,$t9            # Get address of pair_table[op].
    sb $t3,0($t5)              # pair_table[op] = cl
#   sw $t3,0($t5) #####################

    addi $t1,$t1,2             # index += 2

    bnez $t9,fillpairs         # If pairs[i] ($t9) not yet NULL terminated, continue filling the table.
    nop

    jr $ra
    nop