Why this Concatenate function in ASM x64 doesn't work as desired?

40 Views Asked by At

I've created the following Concat function in GAS x64 assembly, i expect it to get two pointers to two strings in rdi and in rsi, and output pointer to concatenated string of this two in rax.

    1 .intel_syntax noprefix
    2 .section .text
    3 .global Concat
    4 Concat:
    5     mov r8, rsp
    6     Concat_First_Loop:
    7         mov al, [rdi]
    8         test al, al
    9         jz Concat_Second_Loop
    10         push rax
    11         inc rdi
    12         jmp Concat_First_Loop
    13     Concat_Second_Loop:
    14         mov al, [rsi]
    15         test al, al
    16         jz Concat_Ret
    17         push rax
    18         inc rsi
    19         jmp Concat_Second_Loop
    20     Concat_Ret:
    21         push rax
    22         mov rsp, r8
    23         inc r8
    24         mov rax, r8
    25         ret

I created two pointers to strings "aa" and "bb" and tried displaying them along with concatenation of them.

    void Exit(int code);

    void Print_String(char* str);

    char* Concat(char* str1, char* str2);
 
    void _start() {
        char* str1 = "aa";
        char* str2 = "bb";
        Print_String(str1);
        Print_String(str2);
        Print_String(Concat(str1, str2));
        Exit(0);
    }

But it prints this instead:

aa bb ��ʰU

the third being a random string of random length

0

There are 0 best solutions below