gpt4 book ai didi

c - 这到底是怎么编译成 4kb 的?

转载 作者:太空宇宙 更新时间:2023-11-04 07:24:41 24 4
gpt4 key购买 nike

#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 的 FLASH 可用,所以我需要把它放在那里。

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() 函数会增大代码大小。它也不会延迟带有可变参数的正确时间。

关于c - 这到底是怎么编译成 4kb 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19263074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com