将 C 代码转换为 mips 会出错
Converting C code to mips gives error
这是我目前转换的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, [=10=], 0 #i =0
addi $t0, [=10=], 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
在 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, [=10=], 0 #i = 0
addi $t0, [=10=], 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:
这是我目前转换的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, [=10=], 0 #i =0
addi $t0, [=10=], 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
在 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, [=10=], 0 #i = 0
addi $t0, [=10=], 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: