集会。反转词

标签 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;
}

最佳答案

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

关于集会。反转词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33486374/

相关文章:

assembly - 在 assembly 中使用鼠标在屏幕上绘制 (emu8086)

assembly - 插入新的线路组件8086

c - 初始化 char[] 失败,esi 包含错误值

security - 使用单独的寄存器来存储返回地址?

c - 为什么 GCC 会产生以下 asm 输出?

assembly - 对 X86 分段感到困惑

assembly - 尝试在汇编程序中打印变量时 GDB 显示错误消息

math - 如何在程序集8086中找到位数?

c - x86 汇编中的操作数大小冲突

C 寄存器调用约定