150 likes | 280 Views
This document focuses on hardware-oriented programming in C, emphasizing code optimization through compiler techniques and inline assembly. It highlights optimization options such as generating fast or compact code with the SDCC compiler and explores various program structures, including loops (for, while, do-while) and conditional statements (if). Additionally, it presents practical examples and tasks, such as optimizing sample programs and selecting appropriate variable types and loop types. This comprehensive guide is essential for those looking to enhance performance in embedded system development.
E N D
Riistvarapõhine programmeerimine Labor 6 C programmi optimeerimine
Programmi optimeerimine • Kompilaatoriga optimeerimine • C koodi optimeerimine • Assembleri lisamine • Assembleri moodulid • In-line assembler ID218 Riistvaralähedane programmeerimine
Kompilaatoriga optimeerimine SDCC manuaal http://sdcc.sourceforge.net/doc/sdccman.html/node52.html • [--opt-code-speed] The compiler will optimize code generation towards fast code, possibly at the expense of code size. • [--opt-code-size] The compiler will optimize code generation towards compact code, possibly at the expense of code speed ID218 Riistvaralähedane programmeerimine
C optimeerimine, Muutuja • Muutuja valik • Char 1b • Int 2b • Signed char -128 +127 • Unsigned char 0 +255 Näidis led61.c; led63.c, ~100 B ID218 Riistvaralähedane programmeerimine
C optimeerimine, tsükkel • Tsükkel, tüübi valik • FOR • IF • WHILE • DO … WHILE ID218 Riistvaralähedane programmeerimine
C optimeerimine, FOR tsükkel for ( i=0; i<100; i++) { . . . } for ( i=100; i==0; i--) ID218 Riistvaralähedane programmeerimine
C optimeerimine, IF tsükkel counter++; if (counter > 100) { counter = 0; } i = 0; if( ++i == 100 ) { i = 0; } ID218 Riistvaralähedane programmeerimine
C optimeerimine, WHILE tsükkel i = 0; while (i <100) { . . . i++; } ID218 Riistvaralähedane programmeerimine
C optimeerimine, DO … WHILE i = 0; do { . . . i++; } while ( i == 99 ) ; ID218 Riistvaralähedane programmeerimine
C optimeerimine, tingimus • Tsükkel, tingimuse valik • If (sec == 60 ) • If (sec =< 60 ) • If (sec < 61 ) • If (sec > 60 ) ID218 Riistvaralähedane programmeerimine
Assembleri moodul, lcd_driver.c, C /*#define LCD_USE_FAST_ASM*/ void lcd_command(unsigned char cmd) { lcd_busy(); lcd_command_wr = cmd; } ID218 Riistvaralähedane programmeerimine
Assembleri moodul, lcd_driver.c, Assembler #define LCD_USE_FAST_ASM void lcd_command(unsigned char cmd) - b _lcd_command: mov dptr, #0x8002 00001$: movx a, @dptr jb acc.7, 00001$ mov a, b mov dptr, #0x8000 movx @dptr, a 00101$: ret ID218 Riistvaralähedane programmeerimine
Assembleri moodul, tulemused • Lcd_driver.c - C • 7 funktsiooni • led61.ihx - 5 587 Bytes • Lcd_driver.c - ass • 6 funktsiooni • Led61.ihx - 5 469 Bytes ID218 Riistvaralähedane programmeerimine
Inline assembler, led64.c for ( j=0; j<10000; j++) { LED = i; } _asm mov dptr,#0xA000 . . . . . . . sjmp 00001$ 00002$: _endasm; ID218 Riistvaralähedane programmeerimine
Ülesanded • Optimeerida näidisprogramm led 65.c • Optimeerimise võimalused • Taimeri katkestus, 1 s (kohustuslik) • Sobiva muutujatüübi valik • Sobiva Tsükklitüübi valik • Sobiva lõpetamisetingimuse valik • Üleliigsed read välja • Valida vähemalt 3 võimalust ID218 Riistvaralähedane programmeerimine