2017-04-23 60 views
0

我正在處理一個作業,我們必須將一段MIPS代碼轉換爲C(儘管即使您不知道MIPS也應該很容易理解此問題,給出我用C編寫的代碼)。我無法聯繫我的老師,因爲我們是一個龐大的班級,我知道他每天都收到足夠多的電子郵件,這就是我轉向這裏的原因。使用指針而不是索引更新數組

我試圖使用函數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); 
} 

我已經看到了類似的問題,如Homework: Making an array using pointers 但在我看來,彷彿他們正在做的根本就指針是一回事嗎?我想了一會兒,也許我的問題是增加a0和a1的數量,但我還沒有找到任何描述這個問題的方法。

編輯: 我可能藏漢添加所需的輸出是:

列表1:ASCII代碼和相應的字符。 0x054'T'0x068'h'0x069'i'0x073''0x020''0x069'i'0x073''0x020''0x061'a'0x020''0x073'''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

Endian的實驗:0x23,0x00,0x00,0x00

+3

'A1 + = 4;'應'A1 + = 1;'。 – melpomene

+3

't0'需要在while循環中更新,否則它將始終保持不變(字符串中的第一個字符)。或者,只需使用'* a0'。 – Dmitri

+0

@melpomene然而它說'addi $ a1,$ a1,4 // a1 + 4'你究竟是什麼意思? – synchronizer

回答

0

非常感謝墨爾波墨涅和查找問題德米特里!

我確實增加了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 
 

 
    } 
 

 

 
}

+1

使用有意義的變量名稱有助於理解代碼。也許'void copycodes(const char * a0_text,int * a1_list,int * a2_count)'? – chux

相關問題