I am attempting to write a code with several different segment, such as combine two differents strings, compare two strings, find the length of a string. Right now, I am working on the part to get the string length, but when I run the code I have so far on Mars and QTSpim, instead of getting a length of 1, I am getting 0 to the first power displayed as the answer. What have I done wrong?
.data
S1: .asciiz "I "
S2: .asciiz "love "
S3: .asciiz "assembly"
string: .asciiz "Our three strings are:\n"
S4: .asciiz "\nThe three strings combined are: "
S5: .asciiz "\nWhen string 3 is copied into string 5, string 5 is: "
S6: .asciiz "\nWhen string 4 is copied into string 6, string 6 is: "
L1: .asciiz "\nThe length of the first string is: "
L2: .asciiz "\nThe length of string 4 is: "
.text
main:
#display all three strings first
li $v0, 4
la $a0, string
syscall
la $a0, S1
syscall
la $a0, S2
syscall
la $a0, S3
syscall
la $a0, S1 #load address of string
jal strlen #call string length procedure
jal print
addi $a1, $a0, 0 #move address of string to $a1
addi $v1, $v0, 0 #move length of string to $v1
addi $v0, $0, 11 #syscall code for message
la $a0, L1 #address of message
syscall
li $v0, 10
syscall
strlen:
addi $t0, $t0, 1 #initialize count to start with 1 for first character
loop:
lb $t0, 0($a0) #load the next character to t0
addi $a0, $a0, 1 #load increment string pointer
addi $t0, $t0, 1 #increment count
beqz $t1, exit #end loop if null character is reached
j loop # return to top of loop
exit: jr $ra
print:
li $v0, 4
la $a0, L1
syscall
li $v0, 1
move $a0, $t1
syscall
jr $ra