I'm trying to write a program that concatenates two null-terminated strings.
(I declared the strings and buffer in the dad section)
.data
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.
Thank you.

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.

Recommended Answers

All 3 Replies

It's pretty rudimentary stuff in any language
to pair two strings together.
The target buffer is the length of the two strings
excluding the NULL characters +1 for the NULL terminator.
Indexing is fundamental to an array of operations and
this is one of them, maintain an index into the
current position of your array and use it to copy
a character at a time the first string, updating
the index for each byte, and reuse the index for
the second string.

I don't know what your target architecture is so
I'll give a more medium level example, this
is C code:
/* don't know how good my C is but dest must be
large enough to hold both strings and a null
terminator, function returns pointer to destination
buffer. */

char * strcat(char *dest, char *str1, char *str2) {
  int x = 0, y = 0;
  while(str1[x] != 0)
   dest[y++] = str1[x++]; /* auto-increment after the var, means after evaluated */   
  x = 0;
  while(str2[x] != 0);
   dest[y++] = str2[x++];
  return dest;
  }

Thanks. In this class I am using MIPS.
QTSpim to run the code if assembly language.

How do I implement it in actually assembly language?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.