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
Do you have any compiler warnings about implicitly declared functions? Take those very seriously, if the implied type does not 100% match the actual type then such argument passing bugs are happening. And the implicitly deduced types rarely match the declared ones, ....
Daniel
Hi Daniel,
Thanks for your answer. Yes, the "implicitly declared function" warning was there. After I put in function prototypes, then the problem goes away. I was wondering if it is possible to make this into an error instead of warning? Mismatch function prototype should be considered a very serious mistake and should be regarded as error to make sure the programmer fix it before going on.
The other problem should be just regarded as the debugger watch window problem which should not affect the normal operation of the code.
I think this should be an error as well. If I remember correctly, declaring functions without a prototype is considered an obsolete part of the C language by the standard committee, and they will likely remove it from the language should there ever be a new standard released.
You can set the C compiler options to regard "implicitly declared function" warnings as an error instead:
option -Wpd in the C compiler options.
Also available in the drop down menu Edit>XXXX Settings>Compiler for HCXX>Messages>Error for implicit parameter declaration
(default is unclicked and disabled)