Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optimizing a line of C code for 8 bit processor

I'm working on a 8bit processor and have written code in a C compiler, now more than 140 lines of code are taking just 1200 bytes and this single line is taking more than 200 bytes of ROM space. eeprom_read() is a function, there should be a problem with this 1000 and 100 and 10 multiplication.

romAddr = eeprom_read(146)*1000 + eeprom_read(147)*100 +
          eeprom_read(148)*10 + eeprom_read(149);

Processor is 8-bit and data type of romAddr is int. Is there any way to write this line in a more optimized way?

like image 295
Farrukh Avatar asked Mar 11 '26 07:03

Farrukh


1 Answers

It's possible that the thing that uses the most space is the use of multiplication. If your processor lacks an instruction to do multiplication, the compiler is forced to use software to do it step by step, which can require quite a bit of code.

It's hard to say, since you don't specify anything about your target processor (or which compiler you're using).

One way might be to somehow try to reduce inlining, so the code to multiply by 10 (which is used in all four terms) can be re-used.

To know if this is the case at all, the machine code must be inspected. By the way, the use of decimal constants for an address calculation is really odd.

like image 196
unwind Avatar answered Mar 13 '26 21:03

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!