Hi,
Below is a portion of the code having bug.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//main()
main()
{
Initialize();
0x003F2463:
SuperLoop();
}//end of main()
//Functions
Initialize()
{
InitSystem1();
0x003F24F1:
InitSystem2();
InitSystem3();
}//end of Initialize()
SuperLoop()
{
while(1)
{
Process();
}
}//end of SuperLoop()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Case1:
-----------
InitSystem1()
{
- - -
- - -
s_RxMaxPower = ULONG_MAX / (((859 * (Uint32)s_RxPowMonSlope) >> 11) + 1); ------ (Eq1)
- - -
- - -
}
Case2(Numerator of Eq1 replaced with a variable):
----------------------------------------------------------------------
InitSystem1()
{
- - -
- - -
s_RxMaxPower = Temp / (((859 * (Uint32)s_RxPowMonSlope) >> 11) + 1); ------ (Eq2)
- - -
- - -
}
Given Conditions:
-----------------------------------------------------------------------------------------------------------
#define ULONG_MAX 4294967295 /* MAX VALUE FOR UNSIGNED LONG */
static int32 s_RxMaxPower;
static Uint16 s_RxPowMonSlope = 0x0800;
Uint32 Temp = ULONG_MAX;
Stack Size: 0x0400
Opt Level: Register ( -o0)
--------------------------------------------------------------------------------------------------------
For the given values of variables, the Denominator of Eq1 will become 0x000035C.
Problem facing:
In Case1, after each reset we were never getting into SuperLoop(). We were thrown into Illeagal Interrupt ISR Vector
causing a watchdog reset. But in Case2, everything was all right.
While debugging we observed:
-1-
After the execution of Eq1, the next-old RPC value in the stack was getting over-written with the denominator value of Eq1,
i.e., 0x000035c in our case rather than 0x003F2463(Starting Address of SuperLoop()). This caused the program to behave
ubnormally and whenever it met with an invalid/trap instruction, it fired Illegal Interrupt. However it comeback to
InitSystem2()(0x003F24F1) because this value retained in RPC.
-2-
This doesnot happen in Case2. Because two seperate locations(two 16-bit words) are left in the stack to store the denominator of Eq2.
The Debug-Disassembly of the code follows:
---------- ---------- ---------- ---------- ----------
Case1 Disassembly:
l1 : s_RxMaxPower = ULONG_MAX / (((859 * (Uint32)s_RxPowMonSlope) >> 11) + 1);
l2 : MOVW DP,#0x0242
l3 : MOV @T,#0x035B
l4 : CLRC SXM
l5 : MPYXU ACC,T,@3
l6 : SFR ACC,11
l7 : ADDB ACC,#1
l8 : MOVL *-SP[2],ACC
l9 : MOVB ACC,#0
l10: SUBB ACC,#1
l11: FFC XAR7,#UL$$DIV
l12: MOVL @8,ACC
At l2:
SP = 0x0408
RPC = 0x3F24F1
Stack Values:
0x0406 2463 //Address of SuperLoop()
0x0407 003F
0x0408 24F1 //Address of InitSystem2()
0x0409 003F
After l8:
SP = 0x0408
RPC = 0x3F24F1
Stack Values:
0x0406 03C5 //Address of SuperLoop() gets over-written with the dinominator value of Eq1
0x0407 0000
0x0408 24F1
0x0409 003F
---------- ---------- ---------- ---------- ----------
Case2 Disassembly:
l1 : s_RxMaxPower = Temp / (((859 * (Uint32)s_RxPowMonSlope) >> 11) + 1);
l2 : MOVW DP,#0x0242
l3 : MOV @T,#0x035B
l4 : CLRC SXM
l5 : MPYXU ACC,T,@3
l6 : SFR ACC,11
l7 : ADDB ACC,#1
l8 : MOVL *-SP[2],ACC
l9 : MOVB @ACC,XAR1
l10: SUBB ACC,#1
l11: FFC XAR7,#UL$$DIV
l12: MOVL @8,ACC
At l2:
SP = 0x040A
RPC = 0x3F24F1
stack Values:
0x0406 2463
0x0407 003F
0x0408 FFFF
0x0409 0000
0x040A 24F1
0x040B 003F
After l8:
SP = 0x040A
RPC = 0x3F24F1
stack Values:
0x0406 2463 //Address of SuperLoop() retains
0x0407 003F
0x0408 035C
0x0409 0000
0x040A 24F1
0x040B 003F
---------- ---------- ---------- ---------- ----------
1. What is the problem with Case1? Or how the problem could be well-defined and well-solved?
2. How the assembler behaves to a #defined constant and a local variable while performing Division?
3. Stack size is defined 0x0400, but SP shows a value higher than this. Does this have any sence?
(I could not find any option to 'Attach File'. Otherwise I could attach some screen shots of debug windows)
Any help is appreciated.
-------------
-Regards
-Sinoj