Search code examples
assemblyx86masmmasm32irvine32

How to handle error on concatenation two string in Assembly?


For my assignment, I want to concatenate two string. This is my code so far. the result should be helloworld , but it only displays hello :-(

How should I add source string at the end of destination ?

What I'm missing ?

;--------------------In main.CPP

    extern "C" void __stdcall StrCatAsm(char[], char[]);

    ;main
              char str1[] = {'h','e','l','l','o',0};
              char str2[] = {'w','o','r','l','d',0};
              StrCatAsm(str1,str2)
              string1 = 'h','e','l','l','o','w','o','r','l','d',0
    ;------------------In main.asm

    Concat PROC  uses eax edi ecx esi,
                        destination:DWORD,
                        source:DWORD

        mov al,0             ;looking for zero terminated
        mov ecx,100          ;number of possible loop
        mov edi, source    
        repne scasb           ;look for end of source
        not ecx               ;ecx is 6
        mov esi, destination  
        rep movsb            ;loop for copy ESI into EDI

        ret
    Concat ENDP

Solution

  • How should I add source string at the end of destination ?

    If this is what you need then you should look for the terminating zero in the destination (and not in the source as your code does!).
    Once the terminating zero is found, your EDI register will be past its location and thus you need to back up 1 position!

    mov  al, 0             ;looking for zero terminated
    mov  ecx, 100          ;any large number will do!
    mov  edi, DESTINATION
    repne scasb            ;look for end of DESTINATION
    DEC  EDI
    mov  ecx, 6            ;length of SOURCE {'w','o','r','l','d',0};
    mov  esi, SOURCE  
    rep movsb              ;loop for copy ESI into EDI