c++ - 关于strcmp()的一种实现算法的疑问
PHPz
PHPz 2017-04-17 13:36:16
[C++讨论组]

在《See MIPS Run》的Chapter 9 Reading MIPS Assembly Language中,提到到这样一段代码:

We’ll use the same example as in Chapter 8: an implementation of the C library function strcmp(). But this time we’ll include essential elements of assembly syntax and also show some hand-optimized and -scheduled code. The algorithm shown is somewhat cleverer than a naïve strcmp() function; we’ll start with this code—still in C—in a form that has all the operations separated out to make them easier to play with, as follows:

int strcmp (char *a0, char *a1)
{
    char t0, t1;
    while (1) {
        t0 = a0[0];
        a0 += 1;
        t1 = a1[0];
        a1 += 1;
        if (t0 == 0)
            break;
        if (t0 != t1)
            break;
    }
    return (t0 - t1);
}

图片版的原文在此:

不是很懂 t0 = a0[0];t1 = a1[0];这个为什么放在while循环里。。。这样难道不是每次循环都从首字符开始了么。。。那么怎么完成字符串的比较呢?

还要一个疑问是。。。if (t0 == 0)难道不应该是if (t0 =='\0')么。。。

PHPz
PHPz

学习是最好的投资!

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号