这是我到目前为止已转换的C代码。它给了我一些错误,我已被包含在以下代码中。我不明白这个c到mips转换的哪个部分有问题?
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:
我的输出:
Runtime exception at 0x00400018: address out of range 0x00000000
答案 0 :(得分:1)
从C代码转换char
类型数组,在MIPS中,您应使用lb
代替lw
。
要打印出来,您需要main:
标签,并且您还应该声明一个类似.byte
或.space
您应该使用syscall 11
打印字符或syscall 4
进行打印
字符串。
我已将上面提到的一些内容添加到您的代码中,希望它有所帮助。
.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: