CSAPP - 反编译 string_length

虽然先前已经把 phase_1 和 phase_2 做出来了, 但其实是参考了网络上的答案, 仅仅是大概知道了关键汇编代码。但其实并没有真的懂。为啥呢?因为很多模棱两可的地方是靠猜测的,而猜测是脆弱的。

重新看 phase_1, 第一个门槛是 string_length 函数。尝试逐句翻译回 C 代码。

前提条件: 知道 eax 存储返回值, 知道 rdi 存储函数第一个参数。使用Intel风格的汇编。

bash 复制代码
(gdb) disassemble string_length
Dump of assembler code for function string_length:
   0x000000000040131b <+0>:     cmp    BYTE PTR [rdi],0x0
   0x000000000040131e <+3>:     je     0x401332 <string_length+23>
   0x0000000000401320 <+5>:     mov    rdx,rdi
   0x0000000000401323 <+8>:     add    rdx,0x1
   0x0000000000401327 <+12>:    mov    eax,edx
   0x0000000000401329 <+14>:    sub    eax,edi
   0x000000000040132b <+16>:    cmp    BYTE PTR [rdx],0x0
   0x000000000040132e <+19>:    jne    0x401323 <string_length+8>
   0x0000000000401330 <+21>:    repz ret 
   0x0000000000401332 <+23>:    mov    eax,0x0
   0x0000000000401337 <+28>:    ret    
End of assembler dump.

尝试反汇编为 C 代码:

1)返回值类型:看到了对 eax 寄存器的操作。基本上是 int 类型。C代码为:

c 复制代码
int string_length()
{
    ...
}

2)cmp BYTE PTR [rdi], 0x0: 这句是 rdi 寄存器里的值表示的内存地址里的值,和0作比较。用C代码表示为:

c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
}
  1. je 0x401332 <string_length+23>: 和上一句连在一起的, return 0.
  2. mov rdx, rdi: 把函数第一个参数,赋值到一个新的变量里头,大概是:
c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    const char* ptr = str;
}
  1. add rdx, 0x1: 新赋值的变量加1:
c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    const char* ptr = str;
    ptr += 1;
}
  1. mov eax, edx: 把刚刚加1的变量,放到 eax 寄存器, 也就是和返回值有关系。没法直接写C代码。继续看。
  2. sub eax, edi: 让 eax 寄存器减掉 edi 寄存器。C代码:
c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    const char* ptr = str;
    ptr += 1;
    int ret = ptr - str;
    return ret;
}
  1. cmp BYTE PTR [rdx], 0x0: 把 rdx 寄存器里的值对应的内存地址处的值, 和0比较。看不出来C代码。继续看汇编.
  2. jne 0x401323 <string_length+8>: 如果刚刚的比较结果不相等,也就是说 [rdx] != 0, 那么跳转到 add rdx, 0x1 这句。代码:
c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    const char* ptr = str;
    int ret;
hello:
    ptr += 1;
    ret = ptr - str;
    if (*ptr != '\0')
    {
        goto hello;
    }
    return ret;
}
  1. repz ret: 没啥高深的,就是跳转到 ret

经过上面这段梳理,写出来的C代码很混乱。goto 和 if 的组合,基本上是等价于 while 循环:

c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    const char* ptr = str;
    int ret;

    do {
        ptr += 1;
        ret = ptr - str;
    } while (*ptr != '\0')
    return ret;
}

再进一步, 感觉 ret 的赋值做了重复计算, do while 也不如 while 直接:

c 复制代码
int string_length(const char* str)
{
    if (*str == '\0')
    {
        return 0;
    }
    const char* ptr;
    for (ptr = str + 1; ptr != '\0'; ptr++);
    return ptr - str;
}
相关推荐
杨福瑞3 小时前
数据结构:双向链表(3)
c语言·数据结构·链表
热心网友俣先生5 小时前
2025年APMCM亚太数学建模C题AI+人工精翻版本+数据收集方式介绍+数据分享
c语言·人工智能·数学建模
闲猿类5 小时前
嵌入式第九天学习
linux·c语言·学习·算法·嵌入式
程序员与背包客_CoderZ5 小时前
C/C++版LLM推理框架Llama.cpp——入门与编码实战
c语言·开发语言·网络·c++·人工智能·语言模型·llama
Creeper.exe6 小时前
【C语言】函数
c语言·开发语言
赖small强7 小时前
【Linux C/C++开发】第20章:进程间通信理论
linux·c语言·c++·进程间通信
赖small强7 小时前
【Linux C/C++开发】第24章:现代C++特性(C++17/20)核心概念
linux·c语言·c++·c++17/20
SundayBear7 小时前
嵌入式操作系统进阶C语言
c语言·开发语言·嵌入式
yuuki23323310 小时前
【数据结构&C语言】排序大汇总
c语言·数据结构·后端·排序算法
雨落在了我的手上10 小时前
C语言入门 (二十):指针(6)
c语言