RE4B Challenge 7

2017-10-22

Challenge

What does this code do? This is one of the simplest exercises I made, but still this code can be served as useful library function and is certainly used in many modern real-world applications.

Optimizing GCC 4.8.2:

<f>:
   0:                movzx  edx,BYTE PTR [rdi]
   3:                mov    rax,rdi
   6:                mov    rcx,rdi
   9:                test   dl,dl
   b:                je     29 
   d:                nop    DWORD PTR [rax]
  10:                lea    esi,[rdx-0x41]
  13:                cmp    sil,0x19
  17:                ja     1e 
  19:                add    edx,0x20
  1c:                mov    BYTE PTR [rcx],dl
  1e:                add    rcx,0x1
  22:                movzx  edx,BYTE PTR [rcx]
  25:                test   dl,dl
  27:                jne    10 
  29:                repz ret

Reslove

  • 关于repz ret: http://repzret.org/p/repzret/
  • sil: the least 8 bit of rsi. work on x64.

    sil -> rsi; dil -> rdi;

    spl -> rsp; bpl -> rbp;

  • arg1: char *

将字符串中的大写字符转换为小写,返回值为字符串本身。

函数默认字符串内容范围为[a-zA-Z],不进行边界检查。

char *f(char *str){
    char *s = str;

    while(*str != '\0'){
        if(*str - 0x41 > 0x19){
            str += 1;
        }else{
            *str = *str + 0x20;
            str += 1;
        }
    }

    return s;
}