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.
CodePudding user response:
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.
lb $t8,($t1)
li $v0,4
la $a0,buffer # putting buffer address in $a0
move $a0,$t8 # overwriting the buffer address with byte value in $t8
syscall