• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Convert a data file into a .S file suitable for assembly.
3  * This reads from stdin and writes to stdout and takes a single
4  * argument for the name of the symbol in the assembly file.
5  */
6 
7 #include <stdio.h>
8 
main(int argc,char * argv[])9 int main(int argc, char *argv[]) {
10     unsigned char buf[4096];
11     size_t amt;
12     size_t i;
13     int col = 0;
14     char *name;
15 
16     if (argc != 2) {
17         fprintf(stderr, "usage: %s NAME < DAT_FILE > ASM_FILE\n", argv[0]);
18         for (i=0; i<argc; i++) {
19             fprintf(stderr, " '%s'", argv[i]);
20         }
21         fprintf(stderr, "\n");
22         return 1;
23     }
24 
25     name = argv[1];
26 
27     printf("\
28 #ifdef __APPLE_CC__\n\
29 /*\n\
30  * The mid-2007 version of gcc that ships with Macs requires a\n\
31  * comma on the .section line, but the rest of the world thinks\n\
32  * that's a syntax error. It also wants globals to be explicitly\n\
33  * prefixed with \"_\" as opposed to modern gccs that do the\n\
34  * prefixing for you.\n\
35  */\n\
36 .globl _%s\n\
37 	.section .rodata,\n\
38 	.align 8\n\
39 _%s:\n\
40 #else\n\
41 .globl %s\n\
42 	.section .rodata\n\
43 	.align 8\n\
44 %s:\n\
45 #endif\n\
46 ", name, name, name, name);
47 
48     while (! feof(stdin)) {
49         amt = fread(buf, 1, sizeof(buf), stdin);
50         for (i = 0; i < amt; i++) {
51             if (col == 0) {
52                 printf(".byte ");
53             }
54             printf("0x%02x", buf[i]);
55             col++;
56             if (col == 16) {
57                 printf("\n");
58                 col = 0;
59             } else if (col % 4 == 0) {
60                 printf(", ");
61             } else {
62                 printf(",");
63             }
64         }
65     }
66 
67     if (col != 0) {
68         printf("\n");
69     }
70 
71     return 0;
72 }
73