confused by assembly produced by CW

View: New views
2 Messages — Rating Filter:   Alert me  

confused by assembly produced by CW

by Frank-185 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a C function that depending on how it's written will be compiled into assembly that does two different things. Below are the functions and the assembly produced. I'm using CodeWarrior 5.9. Can anyone tell me why it produces such different code (TransmitStart being the one that is doing what I intend)?

static uint16 transmitCnt_;
static uint8 *transmitBuf_;

static void TransmitStart(void)
{
  uint8* buf = transmitBuf_;
  transmitCnt_ = 1;
  BDR = buf[0];
}

0000 de00         [3]     LDX   transmitBuf_
0002 c601         [1]     LDAB  #1
0004 87           [1]     CLRA  
0005 5c00         [2]     STD   transmitCnt_
0007 e600         [3]     LDAB  0,X
0009 7b0000       [3]     STAB  _DLCBDR
000c 3d           [5]     RTS  

static void TransmitStart2(void)
{
  transmitCnt_ = 1;
  BDR = transmitBuf_[0];
}

0000 c601         [1]     LDAB  #1
0002 87           [1]     CLRA  
0003 5c00         [2]     STD   transmitCnt_
0005 e6fb0000     [6]     LDAB  [transmitBuf_,PCR]
0009 7b0000       [3]     STAB  _DLCBDR
000c 3d           [5]     RTS



Re: confused by assembly produced by CW

by Edward Karpicz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"Frank" wrote:

>I have a C function that depending on how it's written will be compiled
>into assembly that does two different things. Below are the functions and
>the assembly produced. I'm using CodeWarrior 5.9. Can anyone tell me why it
>produces such different code (TransmitStart being the one that is doing
>what I intend)?

Codes are equivalent. Why they are not the same? Only CW engineers can tell
you why their compiler and optimiser didn't generate exactly the same code.
However in your first routine you are requesting a read from transmitBuf_
pointer, then write to transmitCnt_, then read from data pointed by
transmitBuf_. In your second routine you write to transmitCnt_ first, then
read data pointed by transmitBuf_. No wonder different code is generated.
Are you asking why did TransmitStart2 produce PC relative instruction? Maybe
compiler gives priority to code that overwrites less registers? More chances
for reusage of values loaded to registers? I don't know, specific
optimization strategy, specific solution.

Edward


>
> static uint16 transmitCnt_;
> static uint8 *transmitBuf_;
>
> static void TransmitStart(void)
> {
>  uint8* buf = transmitBuf_;
>  transmitCnt_ = 1;
>  BDR = buf[0];
> }
>
> 0000 de00         [3]     LDX   transmitBuf_
> 0002 c601         [1]     LDAB  #1
> 0004 87           [1]     CLRA
> 0005 5c00         [2]     STD   transmitCnt_
> 0007 e600         [3]     LDAB  0,X
> 0009 7b0000       [3]     STAB  _DLCBDR
> 000c 3d           [5]     RTS
>
> static void TransmitStart2(void)
> {
>  transmitCnt_ = 1;
>  BDR = transmitBuf_[0];
> }
>
> 0000 c601         [1]     LDAB  #1
> 0002 87           [1]     CLRA
> 0003 5c00         [2]     STD   transmitCnt_
> 0005 e6fb0000     [6]     LDAB  [transmitBuf_,PCR]
> 0009 7b0000       [3]     STAB  _DLCBDR
> 000c 3d           [5]     RTS
>
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>