这里是逐行注释的说明
sub_FFFF7B38
PUSH {LR} ; save LR (link register) on the stack
ADDS R2, R0, #0 ; R2 = R0 + 0 and set flags (could just have been MOV?)
LDRB R3, [R2] ; Load R3 with a single byte from the address at R2
CMP R3, #0 ; Compare R3 against 0...
BEQ loc_FFFF7B52 ; ...branch to end if equal
SUBS R1, #1 ; R1 = R1 - 1 and set flags
BCC loc_FFFF7B52 ; branch to end if carry was clear which for subtraction is
; if the result is not positive
loc_FFFF7B46:
ADDS R0, #1 ; R0 = R0 + 1 and set flags
LDRB R3, [R0] ; Load R3 with byte from address at R0
CMP R3, #0 ; Compare R3 against 0...
BEQ loc_FFFF7B52 ; ...branch to end if equal
SUBS R1, #1 ; R1 = R1 - 1 and set flags
BCS loc_FFFF7B46 ; loop if carry set which for subtraction is
; if the result is positive
loc_FFFF7B52:
SUBS R0, R0, R2 ; R0 = R0 - R2
POP {R1} ; Load what the previously saved value of LR into R1
; Presumably the missing next line is MOV PC, R1 to
; return from the function.
所以在非常基本的 C 代码中:
void unknown(const char* r0, int r1)
{
const char* r2 = r0;
char r3 = *r2;
if (r3 == '\0')
goto end;
if (--r1 <= 0)
goto end;
loop:
r3 = *++r0;
if (r3 == '\0')
goto end;
if (--r1 > 0)
goto loop;
end:
return r0 - r2;
}
添加一些控制结构以摆脱gotos:
void unknown(const char* r0, int r1)
{
const char* r2 = r0;
char r3 = *r2;
if (r3 != '\0')
{
if (--r1 >= 0)
do
{
if (*++r0 == '\0')
break;
} while (--r1 >= 0);
}
return r0 - r2;
}
编辑:现在我对进位位和SUBS 的困惑已经消除,这更有意义。
简化:
void unknown(const char* r0, int r1)
{
const char* r2 = r0;
while (*r0 != '\0' && --r1 >= 0)
r0++;
return r0 - r2;
}
换句话说,这是在r0指向的字符串指针的第一个r1字符中找到第一个NUL的索引,如果没有,则返回r1。