Name (please print): CS 5513 Entry Quiz Systems Programming (CS2213/CS3423)) 1. What is a compiler? In addition to the definition, give examples of compilers you have used. A compiler is a program that translate a high-level language like C so that it can run ona particular target. An example would be gcc, which takes in C code, and produces assembly, object files and/or executables (thus it calls a linker). 2. Compare and contrast object file, executable, and source file. Source file: code in high level language such as C, which is read in by a compiler to produce object code. Object code is simply source code that has been compiled so that it is in the hex format specified by the machine s ISA (binary form of assembly). An executable is a series of object files that are linked to make a full program for the target architecture. 3. What command would you give to compile a program on the commandline assuming you save your C routine to the name bob.c, and wish the executable to be called xmyprog and that you have used the standard C function sqrt? gcc -o xmyprog bob.c -lm 1
4. Assume you have allocated a float array during the execution of a procedure, and you need to pass back the allocated pointer as a parameter. Assume the subroutine-local float pointer name is fp, and that the parameter name is Pret. What should the declaration of Pret look like (eg., how many * do you need?), and how would you assign fp (already allocated inside the routine) to this parameter? Declaration: float **Pret assignment: *Pret = fp; 5. What is the difference between calloc and malloc? Calloc zeros the allocated space, and malloc does not. 6. What would the following print statement print out: printf("sizeof(int,float,double) = (%d,%d,%d)\n", sizeof(int), sizeof(float), sizeof(double)); sizeof(int,float,double) = (4,4,8) 2
7. Write a main routine that reads command line arguments. It should accept the flags in any order, and accept an integer, a float, and a string. These values will be indicated by the flags -I, -F, and -S, respectively. Your program should read them in from the command line, and print out the discovered values. So, the invocation:./xmyprog -I 6 -F 3.2 -S "I know systems programming" Should print out: You have entered: Int = 6 float = 3.20 string = I know systems programming done. You should generate the same output if the flag order is switched. You should print a usage message if any other flags are passed. If values are not specified, the integer should default to -1, the float should default to 3.14, and the string should default to default string. 3
#include <stdio.h> #include <stdlib.h> void PrintUsage(int iarg, char *arg) fprintf(stderr, "Unrecognized flag at %d: %s \n", iarg, arg); fprintf(stderr, "USAGE: [-I <int>] [-F <float>] [-S <string>]\n"); exit(iarg?iarg:-1); int main(int nargs, char **args) int i, I=(-1); float F=3.14; char *str = "default string"; for (i=1; i < nargs; i++) if (args[i][0]!= - ) PrintUsage(i, args[i]); switch(args[i][1]) case S : if (++i >= nargs) PrintUsage(i, "out of flags"); str = args[i]; break; case F : if (++i >= nargs) PrintUsage(i, "out of flags"); F = atof(args[i]); break; case I : if (++i >= nargs) PrintUsage(i, "out of flags"); I = atoi(args[i]); break; default: PrintUsage(i, args[i]); printf("you have entered:\n"); printf(" Int = %d\n", I); printf(" float = %.2f\n", F); printf(" string = %s \n", str); printf("done.\n"); return(0); 4
8. Complete the function outlined below, assuming proper C standard header files have already been included. #define INITSIZE 16 double *AppendToArray ( int N, /* # of elements from newd to add to existing array */ double *newd /* array, at least N long, of elts to add to existing array */ ) /* * This routine appends the first N elements of newd to a dynamically * allocated array, which is returned by this function, and persists between * function calls. The new elements are assigned after the last valid entry * in the existing array. If this function is called with N<0, then any * dynamically allocated array is deallocated, and NULL is returned. * For non-negative N, if the array does not exist, it is allocated with size * of MAX(2*N,INITSIZE). If the array exists but is not large enough to hold * the union of the old and new values, then a new array of at least double * the length (and large enough to hold all values) is allocated, the old * and new values are assigned to the new array, and the old array is * deallocated. Values are kept in the order in which they were appended. * * RETURNS: pointer to dynamically allocated array holding the union of any * old values (first entries) with new values (next N entries). */ static int n=0, len=0; static double *d=null; int i; /* * Got array entries to append */ if (N > 0) int newn = N + n; /* * If present array too short, allocate one that is long enough, * copy the old stuff to it, and free old array */ if (newn > len) double *p; if (len == 0) len = (INITSIZE >= N+N)? INITSIZE : N+N; else len = (newn >= len+len)? newn : len+len; p = malloc(len * sizeof(double)); assert(p); for (i=0; i < n; i++) p[i] = d[i]; if (d) free(d); d = p; 5
for (i=n; i < newn; i++) d[i] = newd[i-n]; n = newn; /* * Want to free existing array */ else if (N < 0) free(d); len = n = 0; d = NULL; return(d); #define NELT 20 int main(int nargs, char **args) int i; double d, *dp; for (i=0; i < NELT; i++) d = i; dp = AppendToArray(1, &d); for (i=0; i < NELT; i++) printf("d[%d] = %.2f\n", i, dp[i]); assert(!appendtoarray(-1, NULL)); return(0); 6
Assembly & Pipelining (CS 3843 Computer Organization) 9. Assume that MIPS register r2 holds the address of the start of the 80-element integer array X, and that r3 holds the address of the start of the the 80-element integer array Y. Assuming integers are 32-bits, write a MIPS assembly code fragment to add the corresponding elements of X into Y. Assume one delay slot. li r4, 80*4 /* stx = 80*sizeof(int) */ dadd r2, r4 /* stx = X + 80*sizeof(int) */ LOOP: lw r5, 0(r2) /* r5 = *X */ lw r6, 0(r3) /* r6 = *Y */ dadd r5, r6 /* r6 = *Y + *X */ sw r6, 0(r3) /* *Y = r6 */ daddiu r2, r2, 4 /* X++ */ bne r2, r4, LOOP /* while (stx!= X); */ daddiu r3, r3, 4 /* Y++; in delay slot! */ 10. Add the hex number 0x8f to the binary number 0b11001, and write the answer in both decimal and hex form. 1000 1111 + 1 1001 ========= 1010 1000 = (10) (8) = 0xA8 = 2^3 + 2^5 + 2^7 = 8 + 32 + 128 = 168 7
11. Assume that IA32 register %eax holds the address of the start of the 80-element integer array X, and that %ecx holds the address of the start of the the 80-element integer array Y. Assuming integers are 32-bits, write a x86-32 assembly code fragment to add the corresponding elements of X into Y. add $80*4, %eax /* X += N */ add $80*4, %ecx /* Y += N */ movl $-80*4, %edx /* edx = loop index */ LOOP: mov (%eax, %edx), %ebx /* ebx = *X */ add (%ecx, %edx), %ebx /* ebx = *X + *Y */ mov %ebx, (%ecx, %edx) add $4, %edx jnz LOOP 12. Assume a 6 stage pipeline, with cache access completion in stage 4, hazard detection (and stall) occuring in stage 2, and integer EX occuring in stage 3. How many cycles of delay are there for a load immediately followed by a use (DADD), assuming single issue and forwarding? Draw the pipeline diagram demonstrating the delay you report. S1 S2 S3 S4 S5 S6 S1 S2 S2 S3 S4 S5 S6! only one cycle of delay 8
CS 3733 Operating Systems 13. What is a process, and how is it different than a thread? A process is a thread of execution that has its own private virtual memory address space. A thread is a lightweight process that shares its virtual address space with other threads (making inter-thread communication much easier than inter-process), and only has a private stack space. 14. Draw a picture of a typical layout of virtual memory, including the locations of the stack and heap. stack unused space heap data (global) text (code) 0x0 9