I working on a mips code as extra credit for my Computer Organization and assembly class. The instructions are:
Write a program) that will define the following string manipulation functions. Each is analogous to the
corresponding C++ string function.
STRLEN( Str &, Len &) Sent the address of a string, it returns the length in a call-by-reference param.
STRCPY( Dest &, Source & ) Sent the address of the Source string, it places a copy of it in the Destination
string.
STRCAT( Dest &, Source & )Sent the address of the Source String, a copy of it is attached to the end of
the Destination string.
The test program will do at least the following:
S1 = “I ”
S2 = “love “
S3 = “assembly “
S4 = S1 + S2 + S3 (using STRCAT)
S5 = S3 (using STRCPY)
S6 = S4 (using STRCPY )
L1 = STRLEN( S1)
L4 = STRLEN( S4)
However, when I attempted to get the string length for S1, instead of getting 1, I get 0 raised to the first power. So, I stopped on that one and attemped to copy S3 into S5, but when I did, I got errors. What am I doing wrong? Also, how would I go about S4? I've looked online and seen how to combine two strings, but I don't know how to concatenate three strings. Any help will be appreciated, as I have to submit this for bonus points by midnight on sunday.
.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, S3
jal StrCpy
la $v0, 4
la $a0, S5
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
StrCpy:
add $t0, $zero, $zero
loop: add $t1, $a1, $t0
lb $t2, 0($t1)
beq $t2, $zero, out
add $t3, $a0, $t0
sb $t0, 0($t3)
addi $t0, $t0, 1
j loop
out:
add $t3, $a0, $t0
sb $zero, 0($t3)
jr $ra
strlen:
addi $t0, $t0, 1 #initialize count to start with 1 for first character
loop1:
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, exit1 #end loop if null character is reached
j loop1 # return to top of loop
exit1: jr $ra
print:
li $v0, 4
la $a0, L1
syscall
li $v0, 1
move $a0, $t1
syscall
jr $ra