Lost the return value of assembly code after optimization

OP #1353274
Hello,

I made a program mixed with C and asm by GCC.  It works fine if no 
optimization but run away after optimization with switch "-Os".

After traced code, I found asm code didn't return r0 value back to C 
routine....

**********My C code**********

    :
  volatile struct iphdr *ip_hdr;
    :
    :
    :
  ip_hdr->check = ip_fast_csum((unsigned char *)ip_hdr,ip_hdr->ihl);
    :
    :

**********My ASM code**********

    .global  ip_fast_csum
    .text
ip_fast_csum:
    ldr    r3, [r0], #4
    ldr    r4, [r0], #4
    sub    r1, r1, #5
    adds  r3, r3, r4
    ldr    r4, [r0], #4
    adcs  r3, r3, r4
    ldr    r4, [r0], #4
    adcs  r3, r3, r4
_l:    ldr    r4, [r0], #4
    adcs  r3, r3, r4
    tst    r1, #15
    subne  r1, r1, #1
    bne    _l
    adc    r3, r3, #0
    adds  r3, r3, r3, lsl #16
    addcs  r3, r3, #0x10000
    mvn    r3, r3
    mov    r3, r3, lsr #16
    mov    r0,  r3
    mov    pc, lr

I found the result of ip_hdr->check always zero although the content of 
r0 is correct at the end of assembly?

How can I control the optimization to keep r0 return correctly to 
ip_hdr->check?

BRs.

Tim
Either
-fdo-what-i-mean-not-what-i-say
or
-favoid-all-bugs

For more, you should provide a testcase which can be used and tested by 
readers.

And btw: If speed is premium, drop the count and compare R0 to the final 
address (TEQ).
OP
Ah....

You are right.  It is my mistake.  It should be R2 instead of R4.  It is 
running well now.  The problem actually didn't come from 
optimization.... just because I was lucky at "-O0".

You are helpful and thanks.

BRs.

Tim.
Tim Choi wrote:
> How can I control the optimization to keep r0 return correctly to
> ip_hdr->check?

That was wrong-thinking there. If your code breaks when it is optimised, 
it is most likely that your code was flawed in any case, but that 
without optimisation the error was benign.

Th optimiser may reasonably expect your code to comply with the calling 
convention and will then use available resources with that assumption.

Reply

A question of your own? This thread is over 17 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.