以字符串形式打印x char(MIPS) [英] Printing the x char in a string (MIPS)
问题描述
我的程序应该执行以下操作: -连续获取用户(x)的整数, -在字符串的x位置打印字符. -当用户输入0时,程序退出.
My program is supposed to do the following: -Getting continiously an integer from the user (x), -printing the character at the x position in the string. -The program exits when the user inputs 0.
.text
.globl __start
__start:
li $s3,20 #string length
start: li $v0,5
syscall
move $s0,$a0 #integer now in $a0
beq $s0,$zero,exit
li $s1,0 #counter is 0
la $s2,str #address of string now is $s2
loop:lbu $t1,0($s2) #choosing char of string
addi $s1,1 #increment counter by 1
addi $s2,1 #next char
beq $s1,$s0,print #is the char at the position we entered?
j loop
print: lbu $a0,0($t1) #<------------#
li $v0,11
syscall
j start
exit: li $v0,10
syscall
.data
str: .asciiz "abcdefghijklmnopqrst"
当我尝试运行标记的行时,我一直得到:在PC = 0x00400034处发生异常"和在数据堆栈中读取的地址错误:0x ...".
I keep getting: "Exception occured at PC=0x00400034" and "Bad address in data stack read: 0x..." exactly when i try to run the line i marked.
推荐答案
$t1
在执行lbu $a0,0($t1)
时不包含有效地址. $t1
中的内容是退出loop
循环之前从字符串读取的最后一个字符.
$t1
does not contain a valid address at the point where you do lbu $a0,0($t1)
. What you've got in $t1
there is the last character read from the string before you exited your loop
loop.
我真的看不到循环的意义是什么.您说您有一个字符串和一个整数X,并且想要将字符打印在字符串的偏移量X处.因此,只需阅读该字符即可完成操作:
I really don't see what the point of the loop is. You say that you have a string and and integer X, and you want to print the character at offset X in the string. So just read that character and you're done:
la $a1,string
addu $a1,$a1,$s0 # $a1 = &str[x]. assumes x is in $s0
lbu $a0,($a1) # read the character
li $v0,11
syscall # and print it
这篇关于以字符串形式打印x char(MIPS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!