Copy String to heap in MIPS Assembly

0

I hava=e been having much trouble with MIPS in the past weeks and it has gotten worse from being assigned harder projects. for this specific one I have create a linked list to have a procedure/method to compute the string length and one to copy it to the heap. my code is shown below.

.data 
 buff: .space 32
 head: .word  0
 nl:    .asciiz "\n"

 .text 
  main:

  li $a1, 30
  la $a0, buff
  li $v0, 8
   syscall

  li $v0, 4
  la $a0, buff
    syscall



   la $a0, buff
   jal strlen


   move $a0, $v0
   li $v0, 1
    syscall

    la $a0, buff
    jal strdup

    no_print:

    move $a0, $v0
    li $v0, 4
     syscall

    li $v0, 10
  syscall

  #strlen:

 #  li $v0, 0
 #loop:
  #lb $t1, 0($a0) #load the next character to t0
  #  beqz $t1, exit #end loop if null character is reached
# addi $a0 $a0, 1 #load increment string pointer
#  addi $v0, $v0, 1 #increment count

 #   j loop # return to top of loop
  #exit: 


  # jr $ra


strlen:
move    $v0,$a0                 # remember base address


strlen_loop:
lb      $t1, 0($a0)              # get the current char
addi    $a0,$a0,1               # pre-increment to next byte of string
bnez    $t1,strlen_loop         # is char 0? if no, loop

subu    $v0,$a0,$v0             # get length + 1
subu    $v0,$v0,1               # get length (compensate for pre-increment)
jr      $ra    

 strdup:

 #copys necessary elements to stack
  addiu $sp, $sp, -8
 sw $a0, 0($sp)
 sw $ra, 8($sp)


 jal strlen #compute the string's length

  #Compute the amount of space needed to store the string onto heap
  addiu $v0, $v0, 1

  andi $a0, $v0, 0xfff9

li $v0, 9
syscall 

 lw $ra, 8($sp)
  lw $a0, 0($sp)
 addiu $sp, $sp, 8
  #returns stack to normal
 b strcpy.test


 strcpy.loop:
sb $v0, 0($t0)          # store byte in destination string
addi $t0, $t0, 1        # increment both addresses
addi $a0, $a0, 1

 strcpy.test:
  lbu $t0, 0($a0)         # load a byte from source string
 bnez $a0, strcpy.loop   # stop when null is copied

strcpy.exit:

jr $ra

When I run this code, I get these two errors

exception at PC = 0x004000bc

and

bad address in data/ stack at PC = 0x00000048

I am using SPIM to compile BTW.

Feel free to chime in below with any helpful comments. everything is appreciated since I really don't know where to go from here.

linked-list
mips
qtspim

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0