-1
str fp,[sp, -4]!
add fp, sp, #0
sub sp, sp, #12
str r0, [fp, #-8]
str r1 [fp, #-12]

L5:
ldr r3, [fp, #-8]
ldrb r3, [r3]
cmp r3, #0
beq .L2
ldr r3, [fp, #-12]
ldrb r3, [r3]
cmp r3, #0
beq .L2
ldr r3, [fp, #-8]
ldrb r2, [r3]
ldr r3, [fp, #-12]
ldrb r3, [r3]
cmp r2, [r3]
bne .L8
ldr r3, [fp, #-8]
add r3, r3, #1
str r3, [fp, #-8]
ldr r3, [fp, #-12]
add r3, r3, #1
str r3, [fp, #-12]
b .L5

.L8
nop

.L2
ldr r3, [fp, #-8]
cmp r3, #0
bne .L6
ldr r3, [fp, #-12]
cmp r3, #0
bne .L6
mov r3, #0
b .L7

.L6
ldr r3, [fp, #-8]
ldrb r3, [r3]
mov r3, r3
ldr r3, [fp, #-12]
ldrb r3, [r3]
sub r3, r2, r3

.L7
mov r0,r3
add sp, fp, #0
ldr fp, [sp], #4
bx lr
  • please at least format the code better so it's easier to follow – Paweł Łukasik Sep 16 at 18:00
  • Thank you! will keep that in mind. I'm new to RE, could you please help me understand the above code – Bobby Sep 16 at 23:42
  • Have you tried stepping through it with a debugger? – julian Sep 17 at 0:26
  • yes, since there isn't any proper structure to the code, debuggers weren't of much help to me. I understand that there is some kind of if/if-else/switch happening but can't really put my finger on it – Bobby Sep 17 at 0:51
  • 1
    Very low effort question. You should at least provide some context and what you already discovered. – Igor Skochinsky Sep 17 at 19:02
2

I think this is a strcmp function which was compiled without optimization and is really inefficient.

Here is why:

  • The function only uses r0 and r1 which are first and the second parameter.
  • Both parameters are pointer because they are dereferenced
  • All memory access are byte long
  • Read bytes are compared against '\0'
  • Read bytes are compared using the instruction cmp (subtraction without modifying the destination register)
  • When the byte differs, the returned value is both bytes subtracted (.L6)
  • If both read byte are equal, pointers are incremented by one and it branches back to the comparison block
  • They are extra copies on the stack, it's useless and typical from non-optimized code

Not the answer you're looking for? Browse other questions tagged or ask your own question.