Linux的装配反转字符串 [英] linux assembly reverse a string

查看:143
本文介绍了Linux的装配反转字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作在C扭转这一字符串的内容并把颠倒字符串中的一个新的字符数组写一个内联汇编的功能,但我得到额外字符添加到我的颠倒字符串的结尾。

SO I am working on writing an inline assembly function in c that reverses that contents of a string and puts the reversed string in a new character array, but I am getting extra characters added to the end of my reversed string.

int main(int argc, char **argv) {
    char *new_str;
    char old_str[20] = "Hello, World!";
    mystrrev(new_str, old_str, strlen(old_str));
    printf("New string: %s\n", &new_str);
    return 0;
}

议会功能

char *mystrrev(char *dest, const char *src, int size) {
        int d0, d1, d2, d3;
      __asm__ (
                    "add %%ebx, %%esi\n\t"          /*Move to end of string*/
                    "std\n\t"                       /*Decrement esi after load*/
                    "lodsb\n\t"                     /*Load esi into al*/
                    "sub $1, %%ebx\n\t"
                    "mov %%al, %%cl\n\t"            /*mov contents of al to cl*/
                    "1:\tstd\n\t"                   /*Begin loop, decrement esi after load*/
                    "lodsb\n\t"                     /*Load esi into al*/
                    "cld\n\t"                       /*Clear flg*/
                    "stosb\n\t"                     /*Store al in edi*/
                    "sub $1, %%ebx\n\t"             /*subtract 1 from strlenght counter*/
                    "cmp $0, %%ebx\n\t"             /*Compare ebx to 0*/
                    "jne 1b\n\t"
                    "mov %%ecx, %%edi\n\t"          /*Add null terminating char to new str*/
                    : "=&S"(d0), "=&D"(d1), "=&a"(d2), "=&b" (d3)   /*output*/
                    /*** &S --> ESI,  &D --> EDI,  &a --> eax ***/
                    : "0" (src), "1" (dest), "3" (size)     /*input*/
                    : "memory");                            /*clobber registers*/
      return dest;
    }

在这种情况下的Hello,World!被打印出来[!dlroW,东海生日贺]但也有在末尾添加多余的字符,我想不通为什么。
有什么想法?

In this case Hello, World! gets printed out [!dlroW, olleH] but there are extra characters added at the end and I cannot figure out why. Any thoughts??

推荐答案

mov %ecx, %edi       /*Add null terminating char to new str*/

指令不写什么记忆;它只是复制一个零到寄存器%EDI 本身。对于存储器写入正确的语法,如果我没记错,是一样的东西。

instruction does not write anything to memory; it just copies a zero into the register %edi itself. The correct syntax for a memory write, if I remember correctly, is something like

movb %cl, (%edi)

但它是真的毫无意义,精心保存在拷贝过程中的 CL% 0终结,因为你知道这将是一个零反正。因此,只要

But it is really pointless to meticulously store the zero terminator in %cl during the copying, because you know it's going to be a zero anyway. So just

movb $0, (%edi)

应该工作一样好。

ought to work just as well.

(还可以,但不相关的字符串指令实际上比单独的负载/存储和递增/递减操作相当于组合慢得多。至少这曾经是各地的486或早期的Pentium时代的情况,这让我感到吃惊如果它不是仍然如此 - 尤其是如果你需要反正每次操作方向标志)

(Also, but unrelated, the string instructions are actually slower than equivalent combinations of separate load/store and increment/decrement operations. At least this used to be the case around the 486 or early Pentium era, and it would surprise me if it's not still true -- especially if you need to manipulate the direction flag each time anyway).

((也也,这似乎是相当没有意义的申报,你不反正用一切的输出变量。是什么的地步,即使你需要硬code使用<$ C的$ C> EAX 字符串操作,可以列出EAX在撞列表是非常不是强迫它变成一个虚拟输出变量)更清晰)。

((Also also, it appears to be rather pointless to declare output variables for everything that you don't use anyway. What's the point of that. Even if you need to hardcode the use of eax for the string instructions, simply listing "eax" in the clobber list is immensely clearer than forcing it into a dummy output variable)).

这篇关于Linux的装配反转字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆