我想使用堆栈反转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/