1

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

DATA SEGMENT
    STR1 DB "ENTER FIRST STRING HERE ->$"
    STR2 DB "ENTER SECOND STRING HERE ->$"
    STR3 DB "CONCATEnatedD STRING :->$"
    STR11 DB "FIRST STRING : ->$"
    STR22 DB "SECOND STRING: ->$"

    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

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: $"

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
user3848412
  • 115
  • 3
  • 11
  • 1
    Why do you keep comparing the character being copied to `31h` as the termination condition for the copy loop? `31h` represents the ASCII for the numeral `1`. – lurker Aug 08 '16 at 12:32
  • I'm voting to close for I believe this is an homework assignment and I can't see how OP made any effort to solve their problem. Maybe they were encouraged by their [first successful attempt for a cheap solution](https://stackoverflow.com/questions/38824670/assembly-reverse-a-string)? – Margaret Bloom Aug 08 '16 at 12:59
  • This is not a homework, just a hobby, and I ask here as a last resort when I don't find the solution. And who has this kind of homework in the summer? Those who study assembly do so in college during the normal school time not August. – user3848412 Aug 08 '16 at 13:44
  • 1
    @user3848412 Glad to know you enjoy programming. :) But then, why you don't want to learn how to use a debugger? That's just as essential as the assembler is. – Margaret Bloom Aug 08 '16 at 14:02
  • I use the debugger but I don't always find my mistake. – user3848412 Aug 08 '16 at 14:18
  • `REP SCASB`(for zero term) and `REP MOVSB` could be your friends ;-) – zx485 Aug 08 '16 at 14:43
  • Post answers as answers; don't edit them into the question. Post your solution as an answer and roll back the edit to the question. – Peter Cordes Aug 08 '16 at 17:38
  • If nobody's forcing you to learn 16-bit DOS system calls as well as 16-bit assembly, I'd suggest learning 32-bit or 64-bit in the first place. You don't have to worry about segments or anything, and the system calls / library functions are the same ones you use from normal programs in C. See http://stackoverflow.com/a/34918617/224132, and also learning resources in the [x86 tag wiki](http://stackoverflow.com/tags/x86/info). – Peter Cordes Aug 08 '16 at 17:41

1 Answers1

1

In your first code, you have to set si to point to the beginning of string s3 right before label l5: (because si was pointing to the end of s3 after loop l4:) :

.
.
.
lea dx,str3
mov ah,09h
int 21h

lea si,s3          ;◄■■■■■■■■■■■  
l5: mov dl,[si]
cmp dl,32h
jz l6
.
.
.