Exception encountered when trying to display the length of a string given by a user. MIPS -
i trying compute length of string given user. every time try run code, message "exception occurred @ pc=(a address) followed message :"bad address in data/stack read: (another address). know has stack can't figure out problem. code in mips bello , using qtspim. appreciated.
sentence: .space 6 prompt: .asciiz "enter sentence. max 6 characters, plus terminator .\n" .text # start of code section main: # prompt displayed. li $v0, 4 # system call code printing string = 4 la $a0, prompt # load address of string printed $a0 syscall # call operating system perform operation; # $v0 specifies system function called; # syscall takes $v0 (and opt arguments) ##read string, plus terminator, sentence la $t0, sentence li $t0, 6 li $v0, 8 add $v0, $zero, $zero #initialize length 0 loop: lbu $s0, 0($t0) #load 1 character of string addi $t0,$t0,1 #point next character addi $v0,$v0,1 #increment length 1 bne $s0,$zero, loop #repeat if not null yet end_loop: addi $v0, $v0, -1 #don't count null terminator li $v0, 4 #display actual length syscall exit: #exit program li $v0, 10 syscall
##read string, plus terminator, sentence la $t0, sentence li $t0, 6
here you're loading address of sentence
$t0
, , overwrite $t0
value 6
. root cause of exception, since following lbu
attempt read address 0x00000006. suggest remove li
.
li $v0, 8 add $v0, $zero, $zero #initialize length 0
this li
pointless since you're setting $v0
0 on next line, li
can removed.
sentence: .space 6 prompt: .asciiz "enter sentence. max 6 characters, plus terminator .\n"
you user allowed enter 6 characters. allocate space 6 bytes, meaning null terminator wouldn't fit if user enters 6 characters.