Have a look at this:
Code: Select all
unsigned int MyVariable;
void VeryStrangeFunction(void)
{
MyVariable = 259;
if (MyVariable < 7)
{
Saveable.HeroEnergy = 473;
}
}Alright, but here it is, right in the top left corner of my screen: This is what cc65 created out of it:
Code: Select all
.segment "CODE"
.proc _VeryStrangeFunction: near
.segment "CODE"
ldx #$01
lda #$03
sta _MyVariable
stx _MyVariable+1
bcs L0681
lda #$D9
sta _Saveable+13
stx _Saveable+13+1
L0681: rts
.endprocIf I change the code to MyVariable < 100, it's still the same.
O.k., maybe the compiler already optimizes the comparison since the variable always has the same value. But then, how is it possible that the HeroEnergy variable is ever set by this function?
Also, please note that the function does and doesn't produce the strange behavior, dependent on where I call it. I haven't really found out the reason, but if I have code where I do several things and then I call this function here, the question whether the hero's energy is changed is dependent on where I call the function. Sometimes it changes the energy, sometimes it doesn't.
Also, when I assign a value less than 256 to the variable, the compiler immediately optimizes the condition away:
Code: Select all
void VeryStrangeFunction(void)
{
MyVariable = 250;
if (MyVariable < 7)
{
Saveable.HeroEnergy = 473;
}
}Code: Select all
.segment "CODE"
.proc _VeryStrangeFunction: near
.segment "CODE"
ldx #$00
lda #$FA
sta _MyVariable
stx _MyVariable+1
rts
.endprocDoes anybody have any idea what's going on here?