I'm trying to write a program that concatenates two null-terminated strings.
(I declared the strings and buffer in the dad section)
string1: .asciiz "In a hole in the ground”
string2: .asciiz " there lived a hobbit."
buffer : .space 512
The program copies the first string into the buffer and then concatenates the second string onto the end of that string so that the buffer will look like:
"In a hole in the ground there lived a hobbit."
I'm assuming I would use a loop to do this, "using a pointer" may be a better way to say it.
Would I use code similar to what is below or do I need to format it much more?
I'm not sure exactly how to incorporate the strings or use the pointer exactly. I believe this is a start but still needs work.
## Registers: ## $9 -- pointer to the char ## $10 -- the char (in low order byte) .text .globl main # Initialize main: lui $9,0x1000 # point at first char # while not ch==null do loop: lbu $10,0($9) # get the char sll $0,$0,0 # branch delay beq $10,$0,done # exit loop if char == null sll $0,$0,0 # branch delay j loop sll $0,$0,0 # branch delay slot # finish done: sll $0,$0,0 # target for branch .data string1: .asciiz "In a hole in the ground” string2: .asciiz " there lived a hobbit." buffer : .space 512
Here are some requirements but I think once I get the concept down this will be relatively easy.
I need to write the program so that it works with ANY null-terminated strings of any reasonable size. The resulting string in the buffer should end with a null character and that there is no null in the middle of it. Make the buffer long enough to fit the resulting string. It is OK to make the buffer much larger than needed.
Method: Transfer characters one by one from string1 to the buffer until you hit a null in string1. Keep track of where the last character was copied to in the buffer. Then copy string2 into the buffer starting at that location. End the copy with a null. You don’t have to clear out the characters beyond the null in the string buffer.