1

I have recently started learning assembly. I am trying to concate two 32 byte strings into a final one that is preallocated as 64 bytes.

section .data
     string1 db "static string",0
section .bss
     user_inputed_string resb 32
     concated_string resb 64

I am trying to achieve the strings concated in a way the user inputted one goes first and the static one second: concated_string = user_inputed_string + string1

I have looked the internet for solutions and none even seems to be valid syntax for NASM.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
urula
  • 15
  • 4

1 Answers1

2

First copy the user inputted string to the output buffer. I assume it is a zero-terminated string.

  mov edi, concated_string       ; Address of output buffer
  mov esi, user_inputed_string   ; Address of input buffer
more1:
  mov al, [esi]
  inc esi
  mov [edi], al
  inc edi
  cmp al, 0
  jne more1

Then copy the static string but do overwrite the terminating zero from the first copy:

  dec edi                        ; Removes the zero
  mov esi, string1
more2:
  mov al, [esi]
  inc esi
  mov [edi], al
  inc edi
  cmp al, 0
  jne more2                      ; This zero needs to stay

You can replace mov al, [esi] inc esi by lodsb, and you can replace mov [edi], al inc edi by stosb.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 2
    @urula: As an optimization on this, you have extra space reserved right after your `user_inputed_string` buffer, so you could append to it right there, instead of copying it first. Also, since the string you're appending is a constant, you could use its length to memcpy it (with `rep movsb`) instead of its zero-terminator to strcpy it with a lodsb/stosb loop. – Peter Cordes Aug 17 '22 at 19:46