[英]How the heck does this compile to 4kb?
#define F_CPU 1000000
#include <stdint.h>
#include <avr/io.h>
#include <util/delay.h>
const uint8_t sequences[] = {
0b00000001,
0b00000011,
0b00000110,
0b00001100,
0b00011000,
0b00110000,
0b00100000,
0b00110000,
0b00011000,
0b00001100,
0b00000110,
0b00000011,
0b00000001,
};
const uint8_t totalSequences = sizeof(sequences) / sizeof(sequences[0]);
int main(void) {
DDRB = 0b00111111;
uint8_t i;
uint8_t b;
while(1) {
for(i = 0; i < totalSequences; i++) {
const uint8_t currentSequence = sequences[i];
const uint8_t nextSequence = sequences[(i + 1) % totalSequences];
// blend between sequences
for(b = 0; b < 100; b++) {
PORTB = currentSequence; _delay_us(b);
PORTB = nextSequence; _delay_us(100-b);
}
_delay_ms(50);
}
}
return 0;
}
這是我程序的全部。 當我像下面那樣直接(不進行混合)設置PORTB時,編譯后的二進制文件為214個字節。 當我包括第二個for循環時,我的已編譯二進制文件超過4kb。 我只有1kb的閃存可用,因此我需要在其中安裝它。
const uint8_t currentSequence = sequences[i];
const uint8_t nextSequence = sequences[(i + 1) % totalSequences];
//// blend between sequences
//for(b = 0; b < 100; b++) {
// PORTB = currentSequence; _delay_us(b);
// PORTB = nextSequence; _delay_us(100-b);
//}
PORTB = currentSequence;
PORTB = nextSequence;
_delay_ms(50);
我的工具鏈是WINAVR,使用以下代碼進行編譯:
avr-gcc -Os -mmcu=attiny13 -Wall -std=c99 main.c -o main.out
avr-objcopy -O binary main.out main.bin
我不太了解二進制文件的反編譯,看看編譯器做了什么,但是不管它是什么,都是錯誤的。 為什么帶有內部循環的二進制文件為4kb,如何修復它?
修改后的循環(278個字節):
while(1) {
for(i = 0; i < totalSequences; i++) {
const uint8_t currentSequence = sequences[i];
const uint8_t nextSequence = sequences[(i + 1) % totalSequences];
// blend between sequences
for(b = 0; b < 100; b++) {
int d;
PORTB = currentSequence; for(d = 0; d < b; d++, _delay_us(1));
PORTB = nextSequence; for(d = 100; b < d; d--, _delay_us(1));
}
_delay_ms(50);
}
}
除非參數為編譯時間常數,否則delay()函數會破壞代碼大小。 使用可變參數也不會延遲正確的時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.