0

I will be short.

I am making a program in MIPS which intake Strings of 15 chars from user. I am unable to save the string on stack. Note the I am using a 2D Matrix [20][15] where 20 are string and each string have 15 character.

Please guide me. I have been trying this over the past 10 hours.

Loop:
bgt $t2,20,XYZ

li $v0,8        #take in input
la $a0, buffer  #load byte space into address
li $a1, 15      # allot the byte space for string
syscall

move $t3,$a0    #save string to t0


#transfering the data onto stack!

#num = $t2
#$base address of Matrix = $t1
#colums of Matrix = 15

mul $s0,$t2,15      #num * colums
li $s1,4            #String have 4 bit!
mul $s0,$s0,$s1 
add $s0,$s0,$t1     #$t1 is the base address!

#storing the data onto the stack!
sw $t3,0($s0)

add $t2,$t2,1
add $s0,$s0,-15 
j Loop
Michael
  • 57,169
  • 9
  • 80
  • 125
user4618280
  • 3
  • 1
  • 5

1 Answers1

0

You're storing the address of the string on stack, not the string itself

t3 set by:

la $a0, buffer  #load byte space into address
move $t3,$a0    #save string to t0

Storing instruction:

sw $t3,0($s0)

This next instruction assumes 15 bytes were written:

add $s0,$s0,-15 

you only wrote 4 bytes with SW $t2,0($s0). This also gets destroyed in the next loop anyway as you recalculate and overwrite S0 based on T2. making the add $s0,$s0,-15 redundant.

You need a string copy routine like

#A0=Dest, A1=Source
copy_string:
   lbu v0,(a1)    
   addiu a1,a1,#1   
   sb v0,(a0)
   addiu a0,a0,#1
   bnez v0, copy_string
PaulHK
  • 269
  • 1
  • 9