


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



This assignment is an exercise in assembly programming, which will provide useful background for the last component of your compiler project, the code generator. Your goal is to create a subroutine scan(a,n) which takes as input a one dimensional array a of size n , and returns a new array of size n + 1 , such that the i -th element of the new array (for i ≠ n ) equals the sum of the first i elements of a , and the last element represents the maximal element of a. For instance, if the input array consists of four elements [4; 6; 8; 5], then the output array should have five elements [4; 10; 18; 23; 8]. Write two versions of this subroutine in the SPARC assembly language, one for an integer array and one for a double array. These two routines should be made suitable for calling from C with the following interface: int * iscan (int *a, int n) double * dscan (double *a, int n) where a is a pointer to the input array and n is the size of the array. Each subroutine should allocate a fresh array (of size n + 1) on the heap to hold the result, and return a pointer to this array after filling it in. Each subroutine should also check two error cases, “ n ≤ 0” and “No room available in the heap”; in both cases, the routine should return the null pointer. For example, if your routines are linked with the following main program: int a[] = {3, 4, 5, 6, 7, -8}; double b[] = {3.0, 4.0, 5.0, 6.0, 7.0, -8.0}; main() { int i, *c = iscan (a, 6); double *d = dscan (b, 6); printf ("c = ["); for (i=0; i<7; i++) printf (" %4d", c[i]); printf ("]\n"); printf ("d = ["); for (i=0; i<7; i++) printf (" %4.1f", d[i]); printf ("]\n"); } it should produce the following output: c = [ 3 7 12 18 25 17 7] d = [ 3.0 7.0 12.0 18.0 25.0 17.0 7.0]
Do not use the “-S“ switch of “gcc” to generate your answer. Also, do not use the “-S“ switch of “gcc” to generate a program which you then modify and optimize. (That would be considered cheating!) Instead, you must write the SPARC code from scratch.
The following files can be found via the class web page or FTPed from: ~harry/public_html/compilers/p
int adder (int x, int y) { return x + y; } This step teaches you how to link a subroutine with C routines, and how to pass arguments and return results.