this is a project for class so please dont ask me why Im doing it this way or suggest doing it differently.
Here is the C function I am given:
unsigned strcspn(const char *s1, const char *s2)
{
const char *s = s1;
const char *c;
while(*s1)
{
for(c = s2; *c; c++)
{
if(*s1 == *c)
break;
}
if(*c)
break;
s1++;
}
return s1 - s;
}
here is the assembly code that it compiles to.
.file "function.c"
.text
.align 2
.global strcspn
.type strcspn, %function
strcspn:
@ args = 0, pretend = 0, frame = 0
@ frame_needed = 0, uses_anonymous_args = 0
stmfd sp!, {r4, lr}
mov lr, r0
ldrb r0, [r0, #0] @ zero_extendqisi2
cmp r0, #0
mov r4, r1
movne ip, lr
ldrneb r1, [r1, #0] @ zero_extendqisi2
ldmeqfd sp!, {r4, pc}
.L5:
cmp r1, #0
beq .L9
cmp r1, r0
beq .L12
mov r3, r4
b .L7
.L6:
cmp r2, r0
beq .L15
.L7:
ldrb r2, [r3, #1] @ zero_extendqisi2
cmp r2, #0
add r3, r3, #1
bne .L6
.L9:
ldrb r0, [ip, #1]! @ zero_extendqisi2
cmp r0, #0
bne .L5
.L15:
rsb r0, lr, ip
ldmfd sp!, {r4, pc}
.L12:
rsb r0, lr, ip
ldmfd sp!, {r4, pc}
.size strcspn, .-strcspn
.ident "GCC: (GNU) 4.1.2"
here is my question. The third instruction is
ldrb r0, [r0, #0] @ zero_extendqisi2
I belive that this loads the first character in s1 into r0. Was s1 passed into this function in r0? Or would I say the address of the first character in s1 was passed in in r0? Now what happens to r0? What was the purpose of this? As far as I can see all this did was cut off the next three characters in s1.