I have tried searching for an answer to this but can not find a definitive reason.
I am trying to optimise some 8051 C code to reduce code space. I made the following change..
xdata unsigned char a, b;
to
data unsigned char a, b;
... and saw my Code size reduce by 39Bytes(feels like christmas).
From: Program Size: data=9.0 ...code=10509
to: Program Size: data=11.0 ... code=10468
Question: Why has the codespace reduced by so much for such a minor change?
It depends on how you're using (and how many times you're using) those variables. xdata requires 16 bit addressing, and that takes more space. Just as an example, loading into the accumulator a value at a fixed address takes twice as much code space with xdata as it does with data:
Load accumulator with value at 30h (data):
MOV A, 30h ; 2 bytes
Load accumulator with value at 1230h (xdata):
MOV DPTR, #1230h ; 3 bytes
MOVX A, @DPTR ; 1 byte
Copying from data to data takes three bytes (MOV direct, direct
), copying from xdata to xdata could take eight bytes (MOV DPTR, #addr1; MOVX; MOV DPTR #addr2; MOVX
). If you are accessing these variables many times and the compiler couldn't optimize their use with registers, it can add up quickly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With