2016-03-01 211 views
-2

我有一個需要被翻譯成MIPS下面的C代碼:轉換C代碼到MIPS

#include <stdio.h> 

int main() 
{ 
    // Two strings, 100 bytes allocated for each 
    char s1[100]; 
    char s2[100]; 

    printf("Enter string 1: "); 
    scanf("%s", s1); 

    printf("Enter string 2: "); 
    scanf("%s", s2); 

    int index = 0; 
    while (1) 
    { 
     // Load characters from s1 and s2 
     char c1 = s1[index]; 
     char c2 = s2[index]; 

     // Current character is greater for s1 
     if (c1 > c2) 
     { 
      printf("s1 > s2\n"); 
      break; 
     } 

     // Current character is greater for s2 
     if (c1 < c2) 
     { 
      printf("s1 < s2\n"); 
      break; 
     } 

     // End of strings reached 
     if (c1 == 0) 
     { 
      printf("The strings are equal\n"); 
      break; 
     } 

     // Compare next character 
     index++; 
    } 
} 

這是我到目前爲止有:

.data 
s1: .space 100 
s2: .space 100 
enters1: .asciiz "Enter string 1: " 
enters2: .asciiz "\nEnter string 2: " 
equalprint: .asciiz "The strings are equal\n" 
s2lessthans1: .asciiz "\ns1 > s2\n" 
s1lessthans2: .asciiz "\ns1 < s2\n" 

.text 
.globl main 

main: 
li $v0,4   
la $a0,enters1 
syscall 

li $v0,8 
la $a0,s1 
addi $a1,$zero,20 
syscall   

li $v0,4   
la $a0,enters2 
syscall 

li $v0,8 
la $a0,s2 
addi $a1,$zero,20 
syscall   

la $a0,s1    
la $a1,s2      
jal compare 

beq $v0, $0, equalprint 
slt $t0, $v0, $0 
bne $t0, $0, str1smallerprint 
j str2smallerprint 

end: 
    addi $v0, $0, 10 
    syscall 

compare: 
    add $t1, $s0, $t0 
    lbu $t2, 0($t1) 
    add $t1, $s1, $t0 
    lbu $t3, 0($t1) 
    slt $t4, $t2, $t3 
    bne $t4, $0, str1smaller 
    beq $t2, $0, equal 
    addi $a0, $t2, 0 
    li $v0, 11 
    syscall 
    addi $t0, $t0, 1 
    j compare 

str1smaller: 
    addi $v0, $0, -1 
    jr $ra 

str2smaller: 
    addi $v0, $0, 1 
    jr $ra 

equal: 
    addi $v0, $0, 0 
    jr $ra 

equalstrings: 
    la $a0, equalprint 
    li $v0, 4 
    syscall 
    j end 

str1smallerprint: 
    la $a0, s1lessthans2 
    li $v0, 4 
    syscall 
    j end 

str2smallerprint: 
    la $a0, s2lessthans1 
    li $v0, 4 
    syscall 
    j end 

當我組裝和火星上運行MIPS,它說組裝成功,但線16

lbu $t2, 0($t1) 

有錯誤:

Error in line 47: Runtime exception at 0x00400078: address out of range 0x00000000 

我不知道我錯了哪裏或要修復什麼以擺脫此錯誤,有什麼幫助?

編輯:固定碼

.data 
s1: .space 100 
s2: .space 100 
enters1: .asciiz "Enter string 1: " 
enters2: .asciiz "\nEnter string 2: " 
equalprint: .asciiz "The strings are equal\n" 
s2lessthans1: .asciiz "\ns1 > s2\n" 
s1lessthans2: .asciiz "\ns1 < s2\n" 

.text 
.globl main 

main: 
li $v0, 4 
la $a0, enters1 
syscall 

li $v0, 8 
la $a0, s1 
addi $a1, $0, 100 
syscall 

li $v0, 4 
la $a0, enters2 
syscall 

li $v0, 8 
la $a0, s2 
addi $a1, $0, 100 
syscall 

loop: 
    lb $t1,s1($t0) 
    lb $t2,s2($t0) 
    blt $t1,$t2,s2lts1 
    bgt $t1,$t2,s1lts2 
    beq $t1,$0,s1eqs2 
    addi $t0,$t0,1 
    j loop 

s1lts2: 
    li $v0, 4 
    la $a0, s1lessthans2 
    syscall 
    j end 

s2lts1: 
    li $v0, 4 
    la $a0, s2lessthans1 
    syscall 
    j end 

s1eqs2: 
    li $v0, 4 
    la $a0, equalprint 
    syscall 

end: 
    li $v0, 10 
    syscall 
+0

使用交叉編譯器,例如一個適當配置的[GCC](http://gcc.gnu.org/) –

回答

1

如果跟蹤通過代碼,此刻的你打的lbu,既不t0也不s0已經初始化,如果火星上初始化的寄存器爲0,然後是啊,你的t1將結束一個NULL指針。

+0

我編輯了代碼,現在我有一個運行時異常在第47行,這是'lbu $ t2,0($ t1)' – dms94

+0

您是否已步入通過調試器中的代碼?順便說一句,你不能像你那樣連續放兩個'jal'。我不確定你爲什麼在那裏開始使用它,但'jal'有一個延遲插槽,它應該包含一個非延遲插槽指令。 –

+0

這是複製代碼中的錯誤。我嘗試了一下代碼,但是我對MARS並不熟悉,所以我不確定當我步入 – dms94