ARM assembly memcpy equivalent

Viewed 2303

I am viewing the assembly output of this function:

extern void write(char * buff);

void test(int x)
{
    // copy "EXAMPLE\0\0\0\0\0..."
    char buff[16] = "EXAMPLE";

    // set byte 10 to '0'+x
    buff[10] = '0' + x;

    // write
    write(buff);
}

And it looks like this:

test:
        push    {r4, lr}
        ldr     r2, .L4
        mov     r3, r0
        ldm     r2, {r0, r1}
        sub     sp, sp, #16
        mov     r2, sp
        adds    r3, r3, #48
        stm     r2, {r0, r1}
        movs    r4, #0
        mov     r0, r2
        strd    r4, r4, [sp, #8]
        strb    r3, [sp, #10]
        bl      write
        add     sp, sp, #16
        pop     {r4, pc}
.L4:
        .word   .LANCHOR0
        .cfi_endproc
.LFE0:
        .size   test, .-test
        .section        .rodata
        .align  2
        .set    .LANCHOR0,. + 0
        .ascii  "EXAMPLE\000"
        .space  8
        .text

I am completely puzzled on where the copying from .L4 to the stack happens?

I see the stack pointer getting moved by 16B, and I see the adds instruction for '0'+x, but which instruction copies the data?

Sorry for the newbie question, thanks!

2 Answers

The instructions that do the copy are interleaved with other instructions.

        ldr     r2, .L4
        ldm     r2, {r0, r1}
        mov     r2, sp
        stm     r2, {r0, r1}
        movs    r4, #0
        strd    r4, r4, [sp, #8]

Since the compiler knows the contents of buff, it can store the 0's directly rather than copying them.

The generated code depends on the optimization type: size optimized:

    push    {r0, r1, r2, r3, r4, lr}
    mov     r2, #8
    ldr     r1, .L3
    mov     r4, r0
    mov     r0, sp
    bl      memcpy
    mov     r3, #0
    add     r4, r4, #48
    mov     r0, sp
    str     r3, [sp, #8]
    str     r3, [sp, #12]
    strb    r4, [sp, #10]
    bl      write
    add     sp, sp, #16
    pop     {r4, pc}

-O3

    str     lr, [sp, #-4]!
    sub     sp, sp, #20
    mov     r2, sp
    mov     ip, #0
    ldr     r1, .L4
    add     r3, r0, #48
    ldm     r1, {r0, r1}
    stm     r2, {r0, r1}
    mov     r0, r2
    str     ip, [sp, #8]
    str     ip, [sp, #12]
    strb    r3, [sp, #10]
    bl      write
    add     sp, sp, #20
    ldr     pc, [sp], #4
Related