我正在做一个工作,我们必须将一段MIPS代码转换为C(尽管鉴于我用C编写的代码,即使您不知道MIPS,也应该很容易理解这个问题)。我无法与我的老师联系,因为我们班级很多,我知道他每天都收到足够多的电子邮件,这就是为什么我转向这里。

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

我基本上已经完成了,在我看来它应该可以工作,但是我不断出现Segmentation Fault(核心转储)错误,或者它仅循环两次,但不打印列表中的任何内容。我一直在检查代码并更改一些小东西,但是我整天都在寻找并且似乎找不到我的知识存在缺陷的地方。

该程序由我的老师编写,除了函数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'T'0x068'h'0x069'i'0x073's'0x020''0x069'i'0x073's'0x020''0x061'a'0x020''0x073's'0x074't'0x072'r'0x069 'i'0x06E'n'0x067'g'0x02E'。'

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

字节序实验:0x23,0x00,0x00,0x00

最佳答案

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

我确实确实不正确地增加了a1,也忘记了在while循环内更​​新t0。我最终得到了一个完全没有t0的解决方案。

这是更新的功能:



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

    }


}

08-19 13:12