Hallo,
gibt es eine einfachere Möglichkeit einen 24 bit signed Wert, der von
einer Peripherie gelesen wird in einen 32 bit signed Wert umwandelt,
damit man normale Rechenoperationen und Vergleiche auch mit negativen
Zahlen ausführen kann?
Ich mache es bisher immer so:
long int funktion(){
long int value;
value=Read24();
if((value & 0x00800000)) value |= 0xFF000000;
return value;
}
Bei der umgekehrten Variante kann man ja einfach die 8 höchstwertigsten
Bit weglassen.
Die Abfrage mit >= erzeugt weniger Code, wie das Beispiel hier zeigt.
Und wenn die Variable local, statt wie bei mir global ist, dann noch
weniger
240: if((value & 0x00800000)) value |= 0xFF000000;
+000002AC: 01DC MOVW R26,R24 Copy register pair
+000002AD: 01CB MOVW R24,R22 Copy register pair
+000002AE: E167 LDI R22,0x17 Load immediate
+000002AF: 95B6 LSR R27 Logical shift right
+000002B0: 95A7 ROR R26 Rotate right through
carry
+000002B1: 9597 ROR R25 Rotate right through
carry
+000002B2: 9587 ROR R24 Rotate right through
carry
+000002B3: 956A DEC R22 Decrement
+000002B4: F7D1 BRNE PC-0x05 Branch if not equal
+000002B5: 7081 ANDI R24,0x01 Logical AND with
immediate
+000002B6: 7090 ANDI R25,0x00 Logical AND with
immediate
+000002B7: 70A0 ANDI R26,0x00 Logical AND with
immediate
+000002B8: 70B0 ANDI R27,0x00 Logical AND with
immediate
+000002B9: 2388 TST R24 Test for Zero or Minus
+000002BA: F049 BREQ PC+0x0A Branch if equal
+000002BB: 6F5F ORI R21,0xFF Logical OR with
immediate
+000002BC: 93200060 STS 0x0060,R18 Store direct to data
space
+000002BE: 93300061 STS 0x0061,R19 Store direct to data
space
+000002C0: 93400062 STS 0x0062,R20 Store direct to data
space
+000002C2: 93500063 STS 0x0063,R21 Store direct to data
space
243: if((value >= 0x00800000)) value |= 0xFF000000;
+000002D1: 3060 CPI R22,0x00 Compare with immediate
+000002D2: E020 LDI R18,0x00 Load immediate
+000002D3: 0772 CPC R23,R18 Compare with carry
+000002D4: E820 LDI R18,0x80 Load immediate
+000002D5: 0782 CPC R24,R18 Compare with carry
+000002D6: E020 LDI R18,0x00 Load immediate
+000002D7: 0792 CPC R25,R18 Compare with carry
+000002D8: F048 BRCS PC+0x0A Branch if carry set
+000002D9: 6F9F ORI R25,0xFF Logical OR with
immediate
+000002DA: 93600060 STS 0x0060,R22 Store direct to data
space
+000002DC: 93700061 STS 0x0061,R23 Store direct to data
space
+000002DE: 93800062 STS 0x0062,R24 Store direct to data
space
+000002E0: 93900063 STS 0x0063,R25 Store direct to data
space
> liegt vielleicht an der Optimierung.
Oder vielmehr an der fehlenden solchen? Dann brauchst du dir aber auch
um die Codegröße keine Gedanken zu machen... Ich komme auf gleiches
Ergebnis wie Peter.