阅读字符串并在MIPS中显示前5个字符
我正在尝试阅读用户输入字符串并显示前5个字符。我能够将字符串重新打回,但是我从界限中获得内存地址最有可能在line 移动$ a0,$ t8
,因为我的分配给> lb $ t8
是错误的。我不能做这项工作。
#data segment
.data
buffer: .space 20 #allocate space for 20 bytes=characters
#text segment
.text
.globl __start
#program start
__start:
#get user input
li $v0,8
#load byte space
la $a0,buffer
#tell the system the max length
li $a1,20
move $t1,$a0
syscall
#display the input
li $v0,4
syscall
#print 5 first characters
li $t6,5
loop: lb $t8,($t1)
li $v0,4
la $a0,buffer
move $a0,$t8
syscall
add $t1,1
#1 less letter to print
sub $t6,1
#have we printed all 5
bne $t6,0,loop
T1是否具有第一个字符串的字节?
赞赏此问题以外的所有帮助和/或任何一般技巧。
I am trying to read a user input string and display the first 5 characters. I am able to get and print the string back, but I get Memory Address out of bounds most possibly in line move $a0,$t8
because my assignment to lb $t8
is wrong. I can not make this work.
#data segment
.data
buffer: .space 20 #allocate space for 20 bytes=characters
#text segment
.text
.globl __start
#program start
__start:
#get user input
li $v0,8
#load byte space
la $a0,buffer
#tell the system the max length
li $a1,20
move $t1,$a0
syscall
#display the input
li $v0,4
syscall
#print 5 first characters
li $t6,5
loop: lb $t8,($t1)
li $v0,4
la $a0,buffer
move $a0,$t8
syscall
add $t1,1
#1 less letter to print
sub $t6,1
#have we printed all 5
bne $t6,0,loop
Does t1 not have the byte of the first string?
All help and/or any general tips outside of this problem is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
移动指令不可能导致内存出异常。 只有加载和存储说明才能引起这一点。
您正在使用Syscall#4打印单个字节,因此SYSCALL试图将
$ A0
中提供的字节作为指针,而作为不匹配,这将是错误的。There is no possibility that a move instruction causes a memory out of bounds exception. Only load and store instructions can cause that.
You are using syscall #4 to print a single byte, so the syscall attempts to dereference the byte provided in
$a0
as a pointer, and as a mismatch, that's going to fault.