EmbDev.net

Forum: ARM programming with GCC/GNU tools Lost the return value of assembly code after optimization


Author: Tim Choi (hardwing)
Posted on:

Rate this post
0 useful
not useful
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

Author: A. K. (prx)
Posted on:

Rate this post
0 useful
not useful
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).

Author: A. K. (prx)
Posted on:

Rate this post
0 useful
not useful
You violated the procedure calling standard. R4 must be preserved across 
function calls. See 
http://infocenter.arm.com/help/topic/com.arm.doc.i....

Author: Tim Choi (hardwing)
Posted on:

Rate this post
0 useful
not useful
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.

Author: Clifford Slocombe (clifford)
Posted on:

Rate this post
0 useful
not useful
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.

Author: Jim Kaz (ancaritha)
Posted on:

Rate this post
0 useful
not useful
A. K. wrote:
> Either
> -fdo-what-i-mean-not-what-i-say
> or
> -favoid-all-bugs
>


Best compiler flags ever.  I use them in just about every project I work 
on :P

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.