0

I am trying to compute the length of a string given by a user. Every time I try to run the code, I get the message "Exception occurred at PC=(a certain address) followed by the message :"Bad address in Data/stack read: (another address). I know that it has something to do with the stack but I can't figure out the problem. The code in MIPS is bello and I am using QtSpim. Your help will be very appreciated.

sentence:   .space 6
Prompt: .asciiz "Enter the sentence. Max 6 characters, plus a terminator  .\n"  

        .text       # Start of code section
main:               # The prompt is displayed.

    li $v0, 4        # system call code for printing string = 4
    la $a0, Prompt  # load address of string to be printed into $a0
    syscall         # call operating system to perform operation;
                    # $v0 specifies the system function called;
                    # syscall takes $v0 (and opt arguments)

##read the string, plus a terminator, into the sentence
    la      $t0,    sentence
    li      $t0,    6
    li      $v0,    8

    add $v0, $zero, $zero   #initialize length to zero
loop:
    lbu $s0, 0($t0)      #load one character of string
    addi $t0,$t0,1          #point to next character
    addi $v0,$v0,1          #increment length by 1
    bne $s0,$zero, loop     #repeat if not null yet
end_loop:
    addi $v0, $v0, -1       #don't count the null terminator

    li $v0, 4               #display the actual length
    syscall

exit: #exit the program 
    li $v0, 10
    syscall
markgz
  • 6,054
  • 1
  • 19
  • 41
T4000
  • 231
  • 1
  • 7
  • 24

1 Answers1

1
##read the string, plus a terminator, into the sentence
    la      $t0,    sentence
    li      $t0,    6

Here you're loading the address of sentence into $t0, and then immediately overwrite $t0 with the value 6. This is probably the root cause of the exception, since the following lbu will attempt to read from address 0x00000006. I suggest that you remove the li.

   li      $v0,    8

    add $v0, $zero, $zero   #initialize length to zero

This li is pointless since you're setting $v0 to zero on the very next line, so this li can also be removed.

sentence:           .space 6
Prompt:     .asciiz "Enter the sentence. Max 6 characters, plus a terminator  .\n" 

You say that the user is allowed to enter up to 6 characters. But you only allocate space for 6 bytes, meaning that the NULL terminator wouldn't fit if the user actually enters 6 characters.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • when I try to get ride of: li $t0, 6, it displays the prompt twice. But when I keep that line, the prompt is displayed only once. However, I still don't have the expected result when trying to remove the two lines. – T4000 Apr 25 '13 at 10:34
  • @T4000: That's to be expected, since you end your code with executing syscall 4 (print_string) again and still have `$a0` pointing to `Prompt`. You've also got no code that actually reads a string from the user. – Michael Apr 25 '13 at 11:18