@@ -14,30 +14,162 @@ int main(int argc, char **argv)
1414 char * line = NULL ;
1515 ssize_t linelen = -1 ;
1616 size_t len = 0 ;
17+ size_t outputsize = 0 ;
18+ const char * delims = " \t" ;
19+ char * outbuf = NULL ;
20+ struct stat st ;
21+ size_t bufsize = 0 ;
22+ unsigned int i = 0 ;
23+ unsigned long long bits = 0 ;
24+ unsigned long long bytes = 0 ;
1725
18- input = fopen (argv [1 ], "r " );
26+ input = fopen (argv [1 ], "rb " );
1927 if (input == NULL )
2028 {
2129 perror ("Could not open input file" );
2230 return 1 ;
2331 }
2432
25- output = fopen (argv [2 ], "w " );
33+ output = fopen (argv [2 ], "wb " );
2634 if (output == NULL )
2735 {
2836 perror ("Could not open output file" );
2937 return 2 ;
3038 }
3139
40+ stat (argv [1 ], & st );
41+ bufsize = (size_t )st .st_size * 2 ;
42+ outbuf = calloc (bufsize , 1 );
43+ if (outbuf == NULL )
44+ {
45+ perror ("Could not allocate output buffer" );
46+ }
3247 linelen = getline (& line , & len , input );
3348 while (linelen > -1 )
3449 {
35- puts (line );
50+ char * tokens ;
51+
52+ if (strlen (line ) >= 2 )
53+ {
54+ tokens = strtok (line , delims );
55+ if (tokens != NULL && tokens [0 ] != ';' && tokens [0 ] != '\n' && tokens [0 ] != '\r' )
56+ {
57+
58+ while (tokens != NULL )
59+ {
60+ if (tokens [0 ] == ';' )
61+ {
62+ break ;
63+ }
64+ else if (tokens [0 ] != '\n' && tokens [0 ] != '\r' )
65+ {
66+ size_t tokenlen = strlen (tokens );
67+ int doneline = 0 ;
68+ if (tokens [tokenlen - 1 ] == '\n' )
69+ {
70+ tokens [tokenlen - 1 ] = '\0' ;
71+ }
72+
73+ if (!strncmp (tokens , "HLT" , 3 ))
74+ {
75+ printf ("0000 0000 0000 0000 0000" );
76+ doneline = 1 ;
77+ }
78+ else if (!strncmp (tokens , "LOD" , 3 ))
79+ {
80+ printf ("0001|" );
81+ }
82+ else if (!strncmp (tokens , "STR" , 3 ))
83+ {
84+ printf ("0010|" );
85+ }
86+ else if (!strncmp (tokens , "ADD" , 3 ))
87+ {
88+ printf ("0011|" );
89+ }
90+ else if (!strncmp (tokens , "NOP" , 3 ))
91+ {
92+ printf ("0100 0000 0000 0000 0000" );
93+ doneline = 1 ;
94+ }
95+ else if (!strncmp (tokens , "NND" , 3 ))
96+ {
97+ printf ("0101|" );
98+ }
99+ else if (!strncmp (tokens , "JMP" , 3 ))
100+ {
101+ printf ("0110|" );
102+ }
103+ else if (!strncmp (tokens , "CXA" , 3 ))
104+ {
105+ printf ("0111 0000 0000 0000 0000" );
106+ doneline = 1 ;
107+ if (bits % 8 != 0 )
108+ {
109+ outbuf [bytes ] |= 0x70 ;
110+ bytes ++ ;
111+ bits += 4 ;
112+ outbuf [bytes ] = 0 ;
113+ bytes ++ ;
114+ bits += 8 ;
115+ outbuf [bytes ] = 0 ;
116+ bytes ++ ;
117+ bits += 8 ;
118+ }
119+ else
120+ {
121+ outbuf [bytes ] = 7 ;
122+ bytes ++ ;
123+ bits += 8 ;
124+ outbuf [bytes ] = 0 ;
125+ bytes ++ ;
126+ bits += 8 ;
127+ outbuf [bytes ] &= 0xF0 ;
128+ bits += 4 ;
129+ }
130+ }
131+ else
132+ {
133+ printf ("%s|" , tokens );
134+ }
135+
136+ if (doneline )
137+ {
138+ break ;
139+ }
140+ }
141+ tokens = strtok (NULL , delims );
142+ }
143+ puts ("" );
144+ }
145+ }
36146 linelen = getline (& line , & len , input );
37147 }
38-
148+ for (i = 0 ; i < bufsize ; i ++ )
149+ {
150+ char temp = outbuf [i ];
151+ char j ;
152+ for (j = 0 ; j < 8 ; j ++ )
153+ {
154+ if (temp & 1 )
155+ {
156+ printf ("1" );
157+ }
158+ else
159+ {
160+ printf ("0" );
161+ }
162+ if ((j + 1 )%4 == 0 )
163+ {
164+ printf (" " );
165+ }
166+ temp >>= 1 ;
167+ }
168+ }
169+ printf ("\n" );
170+ free (outbuf );
39171 }
40-
172+ return 0 ;
41173}
42174
43175void help (void )
0 commit comments