Henry Yiu

Function does not get the correct passed argunment

Discussion created by Henry Yiu on Dec 24, 2010
Latest reply on Jan 3, 2011 by Daniel Lundin

Hi expert,


I have a simple program running in HCS08AC128 simulation mode.  I found that the argunment passed to a function cannot be correctly received by the function.  In main, I call this function SD_CLKDelay (8).  This is the function:



  void SD_CLKDelay (byte Frames)
      while (Frames--)
          SPI_Send_byte (0xFF);



Now, if I single step [F5] into this function, the variable Frames should show 8, but instead it shows 0x21 in the variable window.  Here is assembly for main calling:



  0014 a608   [2]  LDA #8

  0016 cd0000 [6]  JSR SD_CLKDelay



but the logic of the function is still correct when looking at the assembly




  0000 2008   [3]  BRA LA ;abs = 000a

  0002 L2:

  0002 aeff   [2]  LDX #-1

  0004 8c     [1]  CLRH

  0005 87     [2]  PSHA

  0006 cd0000 [6]  JSR SPI_Send_byte

  0009 86     [3]  PULA

  000a LA:

  000a 97     [1]  TAX

  000b 4a     [1]  DECA

  000c 5d     [1]  TSTX

  000d 26f3   [3]  BNE L2 ;abs = 0002



but when I single step and goes to inside the SPI_Send_byte() function, the variable is wrong and the actual behavior is also wrong.  Here is what happen in the assembly:



  0000 87       [2]             PSHA 
  0001          L1:    
  0001 0b00fd   [5]             BRCLR 5,_SPI1S,L1 ;abs = 0001
  0004 b600     [3]             LDA   _SPI1S
  0006 95       [2]             TSX  
  0007 f6       [3]             LDA   ,X
  0008 b700     [3]             STA   _SPI1D
  000a          LA:    
  000a 0f00fd   [5]             BRCLR 7,_SPI1S,LA ;abs = 000a
  000d b600     [3]             LDA   _SPI1D
  000f 8a       [3]             PULH 
  0010 81       [6]             RTS  

Now it is obvious that the argunment 0xFF passed to the function is lost.



So there are two problems:

1. Why the compiler compile the code incorrectly?

2. Why the debugger simulator showing the incorrect variable values?



I am using the latest CodeWarrior Development Studio just downloaded from Freescale, and using all the default build configuration.



BR - Henry