部件。扭转一句话

时间:2015-11-02 20:34:57

标签: assembly

我想在asm中反转一个单词,使用堆栈(例如Testing = gnitseT)。我试图自己做,但我的程序返回相同的字符串。帮助

我的代码:

#include <stdio.h>
int main(void) {
    char *x = "Teststring";

    asm (
        ".intel_syntax noprefix;"
        "mov eax, %1;"
        "xor bx,bx;"
        "xor ecx,ecx;"
        "pushloop:"
            "mov cl, [eax];"
            "cmp cl, 0;"
            "jz poploop;"
            "inc bx; "
                "push eax;"
                "inc eax;"
                "jmp pushloop;"
        "xor eax,eax;"
        "poploop:"
            "cmp bx, 0;"
            "jz end;"
                "pop eax;"
                "inc eax;"
                "dec bx;"
                "jmp poploop;"
        "end:"

            "mov %0, eax;"

        ".att_syntax prefix;"
     : "=r" (x)
     : "r" (x)
     : "eax","cl"
   );
    printf("x=%s", x);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

"pushloop:"
 "mov cl, [eax];"
 "cmp cl, 0;"
 "jz poploop;"
 "inc bx; "
 "push eax;"
 "inc eax;"
 "jmp pushloop;"

在此循环中,您想要推送字符,因此请按ECX寄存器。不是EAX中的地址。

de poploop中的相同问题。但是这里有效地反转你需要从头开始重新启动字符串的任何东西。所以写:

 "mov eax, %1;"
 "mov edx, %1;"

并使用:

 ;;;;;;;;;;;;  "xor eax,eax;"
"poploop:"
 "cmp bx, 0;"
 "jz end;"
 "pop ecx;"
 "mov [edx], cl;"
 "inc edx;"
 "dec bx;"
 "jmp poploop;"
"end:"