Created
April 14, 2023 00:58
-
-
Save larsks/82450c9e7d130602d82ecb00a741afae to your computer and use it in GitHub Desktop.
Embedding binary data in C programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
extern uint8_t rom_start[], rom_end[]; | |
extern uint16_t rom_size; | |
int main() { | |
printf("rom_size: 0x%X\n", rom_size); | |
for (int i = 0; i < rom_size; i++) { | |
printf("%02x: %02x\n", i, *(rom_start + i)); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS = -g | |
example: example.o rom.o | |
$(CC) -o $@ $^ | |
rom.o: rom.S rom.bin | |
rom.bin: | |
echo -n -e '\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00' > $@ | |
clean: | |
rm -f example *.o rom.bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.section ".rodata" | |
.global rom_start | |
.global rom_end | |
.global rom_size | |
rom_start: | |
.incbin "rom.bin" | |
rom_end: | |
rom_size: .int rom_end - rom_start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment