Reverse Engineering challenge #7.

Tags: MIPS L1 X64 ASM ARM64 ARM .

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

Optimizing GCC 4.9.3 for ARM64:


<f>:
0:           ldrb    w1, [x0]
4:           mov     x3, x0
8:           cbz     w1, 2c 
c:           sub     w2, w1, #0x41
10:          add     w1, w1, #0x20
14:          uxtb    w2, w2
18:          cmp     w2, #0x19
1c:          b.hi    24 
20:          strb    w1, [x3]
24:          ldrb    w1, [x3,#1]!
28:          cbnz    w1, c 
2c:          ret

(ARM) Optimizing Keil 5.05 (ARM mode):


f PROC
        MOV      r1,r0
|L0.4|
        LDRB     r2,[r1,#0]
        CMP      r2,#0
        BXEQ     lr
        SUB      r3,r2,#0x41
        CMP      r3,#0x19
        ADDLS    r2,r2,#0x20
        STRBLS   r2,[r1,#0]
        ADD      r1,r1,#1
        B        |L0.4|
        ENDP

(ARM) Optimizing Keil 5.05 (Thumb mode):


f PROC
        MOVS     r1,r0
        B        |L0.18|
|L0.4|
        MOVS     r3,r2
        SUBS     r3,r3,#0x41
        CMP      r3,#0x19
        BHI      |L0.16|
        ADDS     r2,r2,#0x20
        STRB     r2,[r1,#0]
|L0.16|
        ADDS     r1,r1,#1
|L0.18|
        LDRB     r2,[r1,#0]
        CMP      r2,#0
        BNE      |L0.4|
        BX       lr
        ENDP

Optimizing GCC 4.4.5 for MIPS:


f:
        lb      $5,0($4)
        nop
        beq     $5,$0,$L9
        move    $2,$4

        move    $3,$4
        andi    $5,$5,0x00ff
$L8:
        addiu   $6,$5,-65
        andi    $6,$6,0x00ff
        sltu    $6,$6,26
        beq     $6,$0,$L3
        addiu   $5,$5,32
        sb      $5,0($3)
$L3:
        addiu   $3,$3,1
        lb      $5,0($3)
        nop
        bne     $5,$0,$L8
        andi    $5,$5,0x00ff
$L9:
        j       $31
        nop

More challenges: challenges.re; about solutions: challenges.re/#Solutions.