Listy L
Listy L

Reputation: 52

Converting C code to mips gives error

This is the C code that i have been converted so far. it gives me some error that i have been included in the following code. i don't understand which part is wrong in this c to mips conversion?

 char ch[10];
 int i;
 for (i = 0; i != 10; i++)
 ch[i] = ch[i] – 32


.data

.text
li  $v0 4
syscall

#$s1 = i, $s0  base address of ch

addi $s1, $0, 0                #i =0
addi $t0, $0, 10               #t0 = 10
loop:   beq  $t0, $s1, end       
add  $t1, $s1, $s0
lw   $t2, 0($t1)
addi $t2, $t2, -32
sw   $t2, 0($t1)
addi $s1, $s1, 1
j   loop
end:

My output:

Runtime exception at 0x00400018: address out of range 0x00000000

Upvotes: 1

Views: 86

Answers (1)

Adam
Adam

Reputation: 846

From the C code you are converting a char type array and in MIPS you should use lb instead of lw.

In order to print out you need a main: label and also you should declare an array like .byte or .space

You should usesyscall 11 to print a character or syscall 4 to print string.

I have added some of these mentioned above to your code hope it helps.

    .data
     #ch: .byte 'a','b','c','d','e'
     ch: .space 10
    .text
     main:


            li $v0, 8      #read character 
            li $a1, 10     #load the space
            la $a0, ch     
            syscall

            li $v0,11      #print character
            syscall

            li $v0,10      # exit program
            syscall

            addi $s1, $0, 0     #i = 0
            addi $t0, $0, 10    # $t0 = 10
    loop:   beq  $t0, $s1, end  
            add  $t1, $s1, $s0  
            lb   $t2, ch($t1)   
            addi $t2, $t2, -32
            sb   $t2, ch($t1)
            addi $s1, $s1, 1
            j   loop
    end:

Upvotes: 1

Related Questions