PIC18 weird variable assignment bug

microchipmicrocontrollerpic

I'm debugging a code for the Microchip PIC18F4520 using the PROTEUS ISIS. The code was compiled with the C18 compiler.
There is a very crazy bug in the code that I don't know whether it is my fault or an ISIS/PIC problem.
The imagem below shows the problem.

At the function 'free', before the statement at address 1004 is executed the variable 'i' has the value 0. When the instruction is executed, the variable 'i' magically receives the value 125.

The 'heap_status' is a global variable that has no binding with the variable 'i'.

Why assigning a value to 'heap_status' affects the variable 'i'?

PS.: When the function is executed for the first time, this doesn't happen. The problem arises when the function executes for the second time.

debug

Best Answer

Without seeing your code it's difficult to give advice (your screenshot is too small to read). What you see can be typically attributed to dynamic memory or auto variables when at certain point the debugger will (erroneously) show you the stack location previously occupied by a variable defined inside a function. Move the code inside a function to see if it makes a difference.