title: assembly concatenate two strings
body: <p>I want to concatenate two strings but in my output instead of getting the final concatenated string, I get a line of weird characters and spaces, maybe someone could help me a bit. I want to save the result in s3. Here is the code</p>
  
  <pre><code>DATA SEGMENT
      STR1 DB "ENTER FIRST STRING HERE -&gt;$"
      STR2 DB "ENTER SECOND STRING HERE -&gt;$"
      STR3 DB "CONCATEnatedD STRING :-&gt;$"
      STR11 DB "FIRST STRING : -&gt;$"
      STR22 DB "SECOND STRING: -&gt;$"
  
      s1 DB 20 DUP("$")
      s2 DB 20 DUP("$")
      s3 db 40 dup(?)
      NEWLINE DB 10,13,"$"
  
  DATA ENDS
  
  CODE SEGMENT
  
      ASSUME DS:DATA,CS:CODE
  START:
  
      MOV AX,DATA
      MOV DS,AX
  
  
  lea dx,str1
  mov ah,09h
  int 21h
  
  lea si,s1
  
  l1: mov ah,01h
  int 21h
  
  mov [si],al
  inc si
  cmp al,31h
  jne l1
  
  dec si
  
  lea dx,str2
  mov ah,09h
  int 21h
  
  lea di,s2
  
  l2: mov ah,01h
  int 21h
  
  mov [di],al
  inc di
  cmp al,32h
  jnz l2
  
  lea si,s3
  lea di,s1
  
  
  lea si,s3
  lea di,s1
  l3: mov al,[di]
  mov [si],al
  inc si
  inc di
  cmp al,31h
  jnz l3
  
  dec si
  
  lea di,s2
  
  l4: mov al,[di]
  mov [si],al
  inc si
  inc di
  cmp al,32h
  jnz l4
  
  lea dx,str3
  mov ah,09h
  int 21h
  
  l5: mov dl,[si]
  cmp dl,32h
  jz l6
  mov ah,02h
  int 21h
  inc si
  jmp l5
  
  
  l6:MOV AH,4CH
      INT 21H
  
  
  CODE ENDS
  END START
  </code></pre>
  
  <p>I have found the solution, hope it is a good one without any mistake:
  data segment
      msg db 0Dh, 0Ah, "String1: $"
      msg2 db 0Dh, 0Ah, "String2: $"
      rev db 0Dh, 0Ah, "Result: $"</p>
  
  <pre><code>buffer label byte  
  buffer2 label byte
  str_maxlen db 255
  str_length db 0
  
  str_string db 255 dup(0)
  str_string2 db 255 dup(0)
  result db 255 dup('$')
  num db 0
  data ends
  
  code segment
  assume cs:code, ds:data
  start:
  mov ax,data
  mov ds,ax
  
  lea dx,msg
  mov ah,09h
  int 21h
  
  lea dx,buffer
  mov ah,0Ah
  int 21h
  
  mov cl,str_length
  mov bh,cl
  mov ch,0
  lea si,str_string
  lea di,result
  
  op1:    mov bl,[si]
  mov [di],bl
  inc di
  inc si
  loop op1
  
  mov cl,bh
  op3:    dec si
  loop op3
  
  lea dx,msg2
  mov ah,09h
  int 21h
  
  lea dx,buffer2
  mov ah,0Ah
  int 21h
  mov cl,str_length
  
  lea si,str_string
  mov cl,str_length
  
  mov ax,0
  op2:    mov al,[si]
  mov [di],al
  inc di
  inc si
  loop op2
  
  lea dx,rev
  mov ah,09h
  int 21h
  
  lea dx,result
  mov ah,09h
  int 21h
  
  mov ah,4Ch
  int 21h
  code ends
  
  end start
  </code></pre>