Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using data instead of xdata significantly reduce code space

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?

like image 245
user968722 Avatar asked Oct 01 '22 17:10

user968722


1 Answers

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.

like image 152
Phil Wetzel Avatar answered Oct 05 '22 11:10

Phil Wetzel