I used to be an assembler programmer until I saw the light and 'learned' (i'm still learning) to program C, I stopped getting the headaches but there is still something inside me that looks at the code produced and wants to produce something that is efficient as possible... probably verging on OCD or actually is considering the problem.
Today I wrote and compiled this statement
loadCurrent = (((int16_t)loadCurrent - LOAD_STEP) > 0) ? loadCurrent - LOAD_STEP : 0;
loadCurrent and LOAD_STEP are uint8_t variables stored in RAM and loadCurrent was accessed by the preceeding statements and placed in R24.
The code produced becomes
loadCurrent = (((int16_t)loadCurrent - LOAD_STEP) > 0) ? loadCurrent - LOAD_STEP : 0; 4b9e: 28 2f mov r18, r24 4ba0: 30 e0 ldi r19, 0x00 ; 0 4ba2: 80 91 e6 09 lds r24, 0x09E6 4ba6: 28 1b sub r18, r24 4ba8: 31 09 sbc r19, r1 4baa: 37 ff sbrs r19, 7 4bac: 02 c0 rjmp .+4 ; 0x4bb2
4bae: 20 e0 ldi r18, 0x00 ; 0 4bb0: 30 e0 ldi r19, 0x00 ; 0 4bb2: 20 93 64 04 sts 0x0464, r18
The code produced is not unreasonable but I would have written:
mov r18, r24 ;stick loadCurrent in to temp lds r24, 0x09E6 ;get LOAD_STEP sub r18, r24 ;temp -= LOAD_STEP brnc NOT_MINUS ;check if rolled over ldi r18, 0x00 ;if so temp = 0 NOT_MINUS: sts 0x0464, r18 ;save load current
Is there a way to write this expression to regain my lost processing time or should I just forget about it and accept it as a trade off for the lack of dull aches in my head? Also as a result of promoting loadCurrent to an int16 R19 is cleared at the end for no effect.