Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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
C 使用指针而不是索引更新数组_C_Arrays_Pointers_Segmentation Fault_Mips - Fatal编程技术网

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

C 使用指针而不是索引更新数组,c,arrays,pointers,segmentation-fault,mips,C,Arrays,Pointers,Segmentation Fault,Mips,我正在做一项作业,我们必须将一段MIPS代码翻译成C(尽管考虑到我用C编写的代码,即使您不知道MIPS,也应该很容易理解这个问题)。我无法联系我的老师,因为我们是一个庞大的班级,我知道他每天收到的电子邮件已经足够多了,这就是为什么我要求助于这里 我试图使用copycodes()函数将text1和text2中每个字符的ascii码复制到list1和list2中,这样就可以通过提供的函数打印它们 我基本上完成了,在我看来,它应该可以工作,但是我不断地得到分段错误(核心转储)-错误,或者它只循环两次,

我正在做一项作业,我们必须将一段MIPS代码翻译成C(尽管考虑到我用C编写的代码,即使您不知道MIPS,也应该很容易理解这个问题)。我无法联系我的老师,因为我们是一个庞大的班级,我知道他每天收到的电子邮件已经足够多了,这就是为什么我要求助于这里

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

我基本上完成了,在我看来,它应该可以工作,但是我不断地得到分段错误(核心转储)-错误,或者它只循环两次,但不打印列表中的任何内容。我一直在浏览我的代码,修改一些小东西,但我整天都在寻找,似乎找不到我的知识有什么缺陷

这个程序是我的老师写的,除了函数copycodes()、函数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);
}
#包括
//汇编代码:
/*
.数据
text1:.asciiz“这是一个字符串。”
text2:“还有一件事。”
.对齐2
列表1:。空间为80
列表2:。空间80
计数:。字0
*/
//C译文:
char*text1=“这是一个字符串。”;
char*text2=“还有一件事。”;
//int*list1;
//int*list2;
int list1[80]//仍然将list1[0]的指针传递给copycodes
int list2[80];
整数计数=0;
无效打印列表(常量int*lst){
printf(“ASCII码和相应字符。\n”);
而(*lst!=0){
printf(“0x%03X'%c',*lst,(char)*lst);
lst++;
}
printf(“\n”);
}
void endian_证明(const char*c){
printf(“\n实验:0x%02x,0x%02x,0x%02x,0x%02x\n”,
(int)*c,(int)*(c+1)、(int)*(c+2)、(int)*(c+3));
}
//汇编代码:
/*
复制码:
循环:
#a0是文本(.asciiz)
#a1是列表(.space)
#a2是计数(.word)
lb$t0,0($a0)#字节t0=来自a0(text1/text2)
beq$t0,$0,完成#分支完成如果(t0==0)
sw$t0,0($a1)#其他单词t0=a1(列表1/列表2)
addi$a0,$a0,1#a0++
addi$a1,$a1,4#a1+4
lw$t1,0($a2)#将单词从a2加载到t1
addi$t1,$t1,1#增加t1 1
sw$t1,0($a2)#将单词从t1存储到a2
j回路#跳到顶部
完成:
jr$ra
*/
无效复制码(字符*a0、整数*a1、整数*a2){
char t0=*a0;//从a0指向t0的位置加载字节)
while(t0!=0)//直到字符串结束
{
//sw$t0,0($a1)//否则单词t0=a1(列表1/列表2)
//t0=*a1;
*a1=t0;//将单词从t0存储到a1指向的位置)
//addi$a0,$a0,1//a0++
//addi$a1,$a1,4//a1+4
a0++;//递增文本指针(a0)
a1+=4;//递增列表(a1)的指针(在mips代码中,此值递增4)
//lw$t1,0($a2)//将字从t1加载到a2
//addi$t1,$t1,1//将t1增加1
//sw$t1,0($a2)//将单词从t1存储到a2
int countValue=*a2;//将countValue设置为指针a2处的值
countValue++;//递增计数器
*a2=countValue;//将计数器(在寄存器a2处)设置为递增的值
}
}
无效工作(){
复制代码(文本1、列表1和计数);
复制代码(文本2、列表2和计数);
}
内部主(空){
工作();
printf(“\nlist1:”);
打印列表(列表1);//[20]);
printf(“\nlist2:”);
打印列表(列表2);//);
printf(“\n计数=%d\n”,计数);
endian_证明((字符*)和计数);
}
我见过类似的问题,比如 但在我看来,他们在指针方面做的基本相同?我想了一会儿,也许我的问题是我增加a0和a1的量,但我还没有找到任何描述这个问题的东西

编辑: 我不妨补充一点,期望的输出是:

清单1:ASCII码和相应字符。0x054“T”0x068“h”0x069“i”0x073“0x020”0x069“i”0x073“0x020”0x061“a”0x020“0x073”0x073“s”0x074“T”0x072“r”0x069“i”0x06E“n”0x067“g”0x02E“

清单2: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


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

非常感谢melpomene和Dmitri发现问题

我确实错误地增加了a1,还忘记了在while循环中更新t0。我最终得到了一个完全没有t0的解决方案

以下是更新后的函数:

void copycodes(char*a0、int*a1、int*a2){
//char t0=*a0;//从a0指向t0的位置加载字节)
while(*a0!=0)//直到字符串结束
{
//sw$t0,0($a1)//否则单词t0=a1(列表1/列表2)
//t0=*a0;
*a1=*a0;//将单词从t0存储到a1指向的位置)
//addi$a0,$a0,1//a0++
//addi$a1,$a1,4//a1+4
a0++;//递增文本指针(a0)
a1++;//递增列表(a1)的指针(在mips代码中,此值递增4)
//lw$t1,0($a2)//将字从t1加载到a2
//addi$t1,$t1,1//将t1增加1
//sw$t1,0($a2)//存储单词fro