Home > Blockchain >  How would I concate 2 strings into a one in assembly? (NASM)
How would I concate 2 strings into a one in assembly? (NASM)

Time:08-17

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.

CodePudding user response:

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.

  • Related