- Category: other --> MCS51
Summary: mcs51, --xstack: the result of a call is moved to A, then a register saved on the external stack is
restored through A (movx a,@r0), and the conditional jump tests A: the call's result is lost.
Version: SDCC 4.6.0 #16555 (MINGW64), Windows x64 (sdcc-4.6.0-x64-setup.exe).
Command: sdcc -mmcs51 --model-large --stack-auto --xstack -c repro.c
Also with --model-small --stack-auto --xstack, --model-large --xstack, --opt-code-size.
Not with --model-large --stack-auto (no --xstack).
repro.c:
unsigned char digits[4] = { 0, 0, 5, 7 };
unsigned char digit_at(unsigned char k)
{
return digits[k];
}
unsigned char first(void)
{
unsigned char i;
for (i = 0; i < 3; i++) {
unsigned char d = digit_at(i);
if (d)
break;
}
return i;
}
Expected: first() returns 2. The generated code:
mov dpl, r7
lcall _digit_at
mov a, dpl ; d in A
mov r0,_spx
dec r0
movx a,@r0 ; i restored from the external stack through A: d lost
mov r7,a
dec _spx
jnz 00103$ ; tests i, not d
So the loop leaves with i = 1 (the first i that is not 0), whatever digit_at returns. The same happens with the call
in the loop's condition (while (i < 3 && !digit_at(i)) i++;).