Advertisement

C Program to ASM

Started by May 23, 2001 02:14 PM
2 comments, last by Lucy 23 years, 8 months ago
Hi My name is Lucy and I am having trouble putting some c code into asm Please could you have a look at this c program and tell me what it is in ASM I am in desperate need of help...Thanks Lucy sp_ex.c (annotated) \\ Include standard libraries - don''t worry about this! #include \\ Make it easier to read: from here on, ORDER is 512, or the number of \\ elements to use in the FIR filter. #define ORDER 512 \\ The start of the actual program int main(int argc, char **argv) { \\ An array of 512 signed 16 bit numbers to contain the constants short int c[ORDER]; \\ An array of 512 signed 16 bit numbers to contain the shift register short int b[ORDER]; \\ A temporary counter int i; \\ File handles FILE *fc, *fo, *fi; \\ A temporary signed 16 bit number short int t; \\ A 64 bit accumulator (you only need 48 bits!) long long acc; /* Parse and check the command line options */ \\ Make sure there are only three arguments on the command line. If not, \\ print a message saying: \\ Usage: <input> \\ on the screen, and exit if(argc!=4) { fprintf(stderr, "Usage: <input> \n"); return -1; } \\ Write the three file names to the screen (more for debugging than anything \\ else. fprintf(stderr, "Constants file: %s\n", argv[1]); fprintf(stderr, "Input file: %s\n", argv[2]); fprintf(stderr, "Output file: %s\n", argv[3]); \\ fc is the filehandle for the constants file. Open it. fc = fopen(argv[1], "rb"); \\ If we could not open the file, report the error and exit. if(!fc) { fprintf(stderr, "Could not open constants file %s\n", argv[1]); return -1; } \\ Repeat the following for i from 0 to 511 (for a total of 512 values) for(i=0; i = t; } \\ "for" loop ends here - we have read all 512 elements. \\ Close the constants file. fclose(fc); /* Open the data files */ \\ Open the input file, with filehandle "fi" fi = fopen(argv[2], "rb"); \\ If there was an error opening the file, report it and exit. if(!fi) { fprintf(stderr, "Could not open input file %s\n", argv[2]); return -1; } \\ Create the output file with filehandle "fo" fo = fopen(argv[3], "wb"); \\ If there was an error creating the file, report it and exit. if(!fo) { fprintf(stderr, "Could not open output file %s\n", argv[3]); return -1; } /* Initialise the input vector - you can start optimising here ! */ \\ Set each element of array b to 0 for(i=0; i = 0; /* While we have data process it */ \\ This is the main loop - as long as we can read a signed 16 byte value \\ from the input file, continue processing. while(fread(&t, 2, 1, fi) == 1) { /* Shift the new data into the buffer */ \\ First shift the entire array one position to the right - begin at element \\ 512, and let it equal element 511, and repeat all the way until element 1 \\ equals element 0 for(i=(ORDER-1); i>0; i–) b = b[i-1]; \\ Now let element 0 equal the value we just read from the input file. b[0] = t; /* Zero the accumulator */ \\ Let the accumulator equal 0 acc = 0; /* Multiply each element in the buffer by the associated constant */ \\ Repeat for all values of i from 0 to 511 (all 512 elements): for(i=0; i<ORDER; i++) \\ Add to the accumulator (using at least 48 bit arithmetic), the product of \\ b and c (the i''th elements of the b and c arrays respectively). acc += (long long)b*(long long)c; /* Compensate for the 15 bit shift in the constants */ \\ Once you have summed all the products, shift the result right by 15 \\ (divide it by 32768). acc >>= 15; /* Clamp the result incase there was an error / inaccuracy */ \\ Now, if the result is bigger than 32767, let it equal 32767 acc = (acc > 32767)?32767:acc; \\ If the result is less than -32768, let it equal -32768 acc = (acc < -32768)?-32768:acc; /* Write the new result to the output */ \\ Make sure the result is formatted as a signed 16 bit integer t = (short int)acc; \\ Write the result to the output file. fwrite(&t, 2, 1, fo); } \\ End of the main loop \\ Close the output file fclose(fo); \\ Close the input file fclose(fi); \\ Return to DOS return 0; } </i>
Lucy
Most modern compilers have an option that makes them output assembly instead of (or in addition to) a compiled program.
Read your compilers documentation to find out how you do it.

That''s the easist way I know of...
Advertisement
Thanks Do you know a specific compiler is it in linux or windows
quote:
Original post by Anonymous Poster

Most modern compilers have an option that makes them output assembly instead of (or in addition to) a compiled program.
Read your compilers documentation to find out how you do it.

That''s the easist way I know of...


Lucy
visual c++ for windows does.

This topic is closed to new replies.

Advertisement