使用指针更新数组而不是索引

时间:2017-04-23 20:55:56

标签: c arrays pointers segmentation-fault mips

我正在完成一项任务,我们必须将一段MIPS代码翻译成C语言(尽管如果您不了解MIPS,应该很容易理解这个问题,考虑到代码I& #39;用C写的。我无法联系我的老师,因为我们是一个庞大的班级,我知道他每天都收到足够的电子邮件,这就是我转向这里的原因。

我试图使用函数copycodes()来将text1和text2中每个字符的ascii代码复制到list1和list2中,以便可以通过提供的函数打印它们。

我基本上完成了,在我看来它应该可以工作,但是我一直在 Segmentation fault(core dumped) -error,或者它只循环两次但是没有打印任何东西从列表中。我不断检查我的代码并改变一些小事情,但我一整天都在寻找,而我似乎无法找到我的知识存在缺陷的地方。

该程序由我的老师编写,除了函数copycodes(),function work()和顶部的公共变量。所有评论,它们出现的地方(也在mips代码中)也是由我写的。

如上所述,我还提供了MIPS代码,代表了如何实现解决方案,该代码已包含在我的代码中的相应位置的评论中。我试图接近MIPS代码,因此copycodes()中的变量具有汇编代码使用的寄存器的名称。

我是这样做的:

#include <stdio.h>

//Assembly code:
/*
.data


text1:    .asciiz "This is a string."
text2:    .asciiz "Yet another thing."

.align  2
list1:  .space 80   
list2:  .space 80   
count:  .word  0    
*/

//C translation:

char* text1 = "This is a string.";
char* text2 = "Yet another thing.";


//int* list1;
//int* list2; 
int list1 [80]; //Still passes the pointer of list1[0] to copycodes
int list2 [80];

int count = 0;


void printlist(const int* lst){
  printf("ASCII codes and corresponding characters.\n");
  while(*lst != 0){
    printf("0x%03X '%c' ", *lst, (char)*lst);
    lst++;
  }
  printf("\n");
}

void endian_proof(const char* c){
    printf("\nEndian experiment: 0x%02x,0x%02x,0x%02x,0x%02x\n", 
        (int)*c,(int)*(c+1), (int)*(c+2), (int)*(c+3));

}




//Assembly code:
/*
copycodes:
loop:

    #   a0 is text (.asciiz)
    #   a1 is list (.space)
    #   a2 is count (.word)

    lb  $t0,0($a0)  # byte t0 = from a0 (text1/text2) 
    beq $t0,$0,done # branch done if (t0 == 0)
    sw  $t0,0($a1)  # else word t0 = a1 (list1/list2) 

    addi    $a0,$a0,1   # a0++
    addi    $a1,$a1,4   # a1+4 

    lw      $t1,0($a2)  # load word from a2 into t1
    addi    $t1,$t1,1   # increment t1 by 1
    sw      $t1,0($a2)  # store word from t1 to a2
    j       loop        # jump to top
done:
    jr  $ra
*/

void copycodes(char* a0, int* a1, int* a2){


    char t0 = *a0; //load byte from where a0 is pointing into t0)

    while(t0 != 0) //until end of string
    {

        //sw        $t0,0($a1)      // else word t0 = a1 (list1/list2) 

        //t0 = *a1;
        *a1 = t0; //store word from t0 to where a1 is pointing )



        //addi      $a0,$a0,1       // a0++
        //addi      $a1,$a1,4       // a1+4 

        a0++;       //increments pointer of text (a0)
        a1 += 4;    //increments pointer of list (a1) (in the mips code this is incremented by 4)


        //lw        $t1,0($a2)      // load word from t1 into a2
        //addi      $t1,$t1,1       // increment t1 by 1
        //sw        $t1,0($a2)      // store word from t1 to a2

        int countValue = *a2; //set countValue equal to value at pointer a2
        countValue++;         //increment counter
        *a2 = countValue;     // Set counter (at register a2) to the incremented value

    }


}
void work(){

    copycodes(text1,list1,&count);
    copycodes(text2,list2,&count);

}
int main(void){
    work();

    printf("\nlist1: ");
    printlist(list1);   //[20]);
    printf("\nlist2: ");
    printlist(list2);   //);
    printf("\nCount = %d\n", count);

  endian_proof((char*) &count);
}

我见过类似问题,例如Homework: Making an array using pointers 但它看起来好像他们对指针做了根本相同的事情?我想了一段时间,也许我的问题是我增加a0和a1的数量,但我还没有找到描述这个问题的任何方法。

修改 我还可以补充说,所需的输出是:

list1:ASCII码和相应的字符。 0x054&#39; T&#39; 0x068&#39; h&#39; 0x069&#39;我&#39; 0x073&#39; s&#39; 0x020&#39; &#39; 0x069&#39;我&#39; 0x073&#39; s&#39; 0x020&#39; &#39; 0x061&#39; a&#39; 0x020&#39; &#39; 0x073&#39; s&#39; 0x074&#39; t&#39; 0x072&#39; r&#39; 0x069&#39;我&#39; 0x06E&#39; n&#39; 0x067&#39; g&#39; 0x02E&#39;。&#39;

list2:ASCII码和相应的字符。 0x059&#39; Y&#39; 0x065&#39; e&#39; 0x074&#39; t&#39; 0x020&#39; &#39; 0x061&#39; a&#39; 0x06E&#39; n&#39; 0x06F&#39; o&#39; 0x074&#39; t&#39; 0x068&#39; h&#39; 0x065&#39; e&#39; 0x072&#39; r&#39; 0x020&#39; &#39; 0x074&#39; t&#39; 0x068&#39; h&#39; 0x069&#39;我&#39; 0x06E&#39; n&#39; 0x067&#39; g&#39; 0x02E&#39;。&#39;数= 35

Endian实验:0x23,0x00,0x00,0x00

1 个答案:

答案 0 :(得分:0)

非常感谢melpomene和Dmitri发现问题!

我确实错误地增加了a1并且忘记了在while循环中更新t0。我完全没有t0的解决方案。

这是更新的功能:

&#13;
&#13;
void copycodes(char* a0, int* a1, int* a2){


    //char t0 = *a0; //load byte from where a0 is pointing into t0)

    while(*a0 != 0) //until end of string
    {

        //sw        $t0,0($a1)      // else word t0 = a1 (list1/list2) 

        //t0 = *a0;
        *a1 = *a0; //store word from t0 to where a1 is pointing )



        //addi      $a0,$a0,1       // a0++
        //addi      $a1,$a1,4       // a1+4 

        a0++;       //increments pointer of text (a0)
        a1++;    //increments pointer of list (a1) (in the mips code this is incremented by 4)


        //lw        $t1,0($a2)      // load word from t1 into a2
        //addi      $t1,$t1,1       // increment t1 by 1
        //sw        $t1,0($a2)      // store word from t1 to a2

        int countValue = *a2; //set countValue equal to value at pointer a2
        countValue++;         //increment counter
        *a2 = countValue;     // Set counter (at register a2) to the incremented value

    }


}
&#13;
&#13;
&#13;