子 - ç内联汇编code [英] substring -- c inline assembly code

查看:118
本文介绍了子 - ç内联汇编code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个code,它获得与海湾合作委员会内联汇编字符串的子字符串。但总是问题,当我想要得到的长度为8这里是code的子

 静态内嵌的char * asm_sub_str(字符* DEST,字符* SRC,诠释s_idx,诠释edix)
{
    __asm​​__ __volatile __(CLD \\ n \\ t的
                         REP \\ n \\ t的
                         MOVSB​​
                         :
                         :S(SRC + s_idx),D(DEST),C(edix - s_idx + 1)
                         );
    返回DEST;
}INT主(INT ARGC,CHAR *的argv [])
{    烧焦my_string [stringsize的] =ABC defghij
    烧焦asm_my_sub_string [stringsize的];    INT SIDX,eidx;    SIDX = 0;
    eidx = 5;
    字符* D1 = asm_sub_str(asm_my_sub_string,my_string,SIDX,eidx);
    的printf(D1 [%D-%D]。:%S \\ n,SIDX,eidx,D1);    SIDX = 0;
    eidx = 7;
    D1 = asm_sub_str(asm_my_sub_string,my_string,SIDX,eidx);
    的printf(D1 [%D-%D]。:%S \\ n,SIDX,eidx,D1);    SIDX = 0;
    eidx = 9;
    D1 = asm_sub_str(asm_my_sub_string,my_string,SIDX,eidx);
    的printf(D1 [%D-%D]。:%S \\ n,SIDX,eidx,D1);}

下面是输出

  D1 [0-5]:ABC德
D1 [0-7]:ABC DEFG?
D1 [0-9]:ABC defghi

任何想法?????

感谢您的答复。这里是子字符串的C code和我忘了空终止字符串。感谢仙人掌bbonev!祝别人可以从这个线程学习。

 静态内嵌的char * sub_str(字符* DEST,字符* SRC,诠释s_idx,诠释edix)
{
    INT长度= edix - s_idx + 1;
    INT I;    对于(i = 0; I<长度;我+ +)
    {
        *(DEST + I)= *(SRC + s_idx + I);
    }
    *(DEST +长度)='\\ 0';    返回DEST;
}


解决方案

我想它不工作,因为大会code不终止0的结果缓冲区。

我总是preFER与起始位置和数量,而不是两个位置的子字符串语义。人们在这样的术语想象的要容易一些。

有没有必要从该函数返回任何值

 静态内嵌无效asm_sub_str(字符* DEST,字符* SRC,诠释s_idx,诠释计数)
{
    __asm​​__ __volatile __(CLD \\ n
                         REP \\ n
                         MOVSB​​ \\ n
                         XOR %%人,人%% \\ n
                         STOSB \\ n
                         :
                         :S(SRC + s_idx),D(DEST),C(计数)
                         );
}

编辑:请注意,这个实现是非常不理想的,虽然汇编语言编写的。对于特定的架构内存对齐和字的大小是速度和可能进行复制的最佳方式重要的是通过对齐的机器尺寸的话。首先登场的一个复制到字长-1字节​​的,则该字符串的很大一部分复制的话,终于完成最后达到字长-1字节​​。

我以问题为内联汇编和传递参数的练习吧,而不是复制的字符串的最佳方式。随着现代C编译器的预计,随着-O2快code将会产生。

I write a code that get substring of a string with gcc inline assembly. but always get problem when I want to get the substring whose length is 8. here is the code

static inline char * asm_sub_str(char *dest, char *src, int s_idx, int edix)
{
    __asm__ __volatile__("cld\n\t"
                         "rep\n\t"
                         "movsb"
                         :
                         :"S"(src + s_idx), "D"(dest), "c"(edix - s_idx + 1)
                         );
    return dest;
}

int main(int argc, char *argv[])
{

    char my_string[STRINGSIZE] = "abc defghij";
    char asm_my_sub_string[STRINGSIZE];

    int sidx,eidx;

    sidx = 0;
    eidx = 5;
    char *d1 = asm_sub_str(asm_my_sub_string, my_string, sidx, eidx);
    printf("d1[%d-%d]: %s\n",sidx, eidx, d1);

    sidx = 0;
    eidx = 7;
    d1 = asm_sub_str(asm_my_sub_string, my_string, sidx, eidx);
    printf("d1[%d-%d]: %s\n",sidx, eidx, d1);

    sidx = 0;
    eidx = 9;
    d1 = asm_sub_str(asm_my_sub_string, my_string, sidx, eidx);
    printf("d1[%d-%d]: %s\n",sidx, eidx, d1);

}

here is the output

d1[0-5]: abc de
d1[0-7]: abc defg?
d1[0-9]: abc defghi

any idea?????

Thanks for reply. Here is the c code of substring and I forgot to null terminate the string. Thank cactus and bbonev! Wish someone else can learn from this thread.

static inline char * sub_str(char *dest, char *src, int s_idx, int edix)
{
    int length = edix - s_idx + 1;
    int i;

    for(i = 0; i < length; i++)
    {
        *(dest + i) = *(src + s_idx + i);
    }
    *(dest + length) = '\0';

    return dest;
}

解决方案

I suppose its not working because the assembly code does not 0 terminate the result buffer.

I would always prefer the substring semantics with starting position and count, instead of two positions. People think little easier in such terms.

There is no need to return any value from this function.

static inline void asm_sub_str(char *dest, char *src, int s_idx, int count)
{
    __asm__ __volatile__("cld\n"
                         "rep\n"
                         "movsb\n"
                         "xor %%al,%%al\n"
                         "stosb\n"
                         :
                         :"S"(src + s_idx), "D"(dest), "c"(count)
                         );
}

EDIT: Note that this implementation is quite suboptimal although written in assembly. For a particular architecture memory alignment and word size are important for speed and probably the best way to do the copy is by aligned machine size words. First copy up to word size-1 bytes one by one, then copy the big part of the string in words and finally finish the last up to word size-1 bytes.

I take the question as an excersize in inline assembly and passing parameters, not as the best way to copy strings. With modern C compilers its expected that with -O2 faster code will be generated.

这篇关于子 - ç内联汇编code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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