Problem 1 (3 parts, 30 points) Code Fragments Part A (5 points) Write a MIPS code fragment that branches to label Target when register $1 is less than or equal to register $2. You may use only two instructions. For maximum credit, include comments. slt $3, $2, $1 # $1<=$2 = not($2<$1) beq $3, $0, Target # if not($2<$1), branch to Target Part B (10 points) Write a for loop in C that takes the values at the even indexes in a 100- element vector A and stores them at consecutive indexes in vector B. For example, B[0] gets A[0], B[1] gets A[2], B[2] gets A[4], etc. For maximum credit, declare and initialize variables as needed. int A[100] = {4, -1, 3,, 17; int B[50]; int I; for(i=0; i<50; i++) B[i] = A[2*i]; Part C (15 points) Write a MIPS code fragment that is equivalent to the for loop you wrote in Part B. For maximum credit, include comments..data Aaddr:.word 4, -1, 3,, 17 # int A[100]={4,-1,3,,17; Baddr:.alloc 50 # int B[50];.text addi $1, $0, 0 # initialize loop counter Loop: slti $3, $1, 200 # is counter < 200 (i<50) beq $3, $0, Exit # if not, Exit Loop sll $2, $1, 1 # 2*i lw $4, Aaddr($2) sw $4, Baddr($1) addi $1, $1, 4 j Loop # read A[2*i] # write it to B[i] # increment counter # loop back Exit:... # instructions after the loop 1
Problem 2 (1 part, 20 points) Draw a control flow diagram for the following C code fragment. int i, y, x; int A[] = {3, 4,... for (i = 0; i < 10; i++) { x = f(i, y); if (x<0) continue; do{ x++; if ((x!=99)&&(a[i]>100)) z = A[i]; else z = x; while(a[z]); printf("%d\n", z); Flow Control 2
Problem 3 (4 parts, 22 points) Short Answer Questions Part A (2 points) In the following command, what does -o tell gcc to do? gcc HW1-2.c g Wall o HW1-2 The executable file that gcc produces as output should be named HW1-2. Part B (3 points) Write a Linux command to rename a file named old.c to new.c. mv old.c new.c Part C (10 points) The following Linux command displays the contents of the file named myfile.txt on the standard output: more myfile.txt Below is a part of an implementation of the more command. Fill in the five empty blanks to correctly access the command line argument and open the file specified by the argument (not just myfile.txt). int main(int argc, char *argv[]) { FILE *FP; if (argc!= 2 ) { printf("usage: more file\n"); exit(1); FP = fopen ( argv[1], r ); if (FP == NULL) { printf("%s could not be opened.\n", argv[1] ); return 0; else { Part D (7 points) Write a single C statement that corresponds to the following MIPS code. Assume $1 holds A, $2 holds B, $3 holds C, and $4 holds D. Do not use an if-then-else. addi $4, $0, 0 bne $1, $0, Set beq $2, $0, End bne $3, $0, End Set: addi $4, $0, 1 End: D = A (B &&!C) 3
Problem 4 (3 parts, 28 points) Subroutines and Stack Part A (15 points) The program Sort calls a subroutine Mystery (defined on next page). Complete the program Sort by adding MIPS code to correctly preserve appropriate registers before the jal by pushing them on the stack and to restore them after the subroutine call. Assume Mystery can modify any registers, not just the ones modified in the code on the next page. Sort: addi $1, $0, 4 # init Array index to 1 OuterLoop: lw $3, Array($1) # load in current element X add $2, $1, $0 # reinitialize Array index addi $29, $29, -12 sw $1, 0($29) sw $3, 4($29) sw $31, 8($29) # make room on stack for 3 words # push $1 # push $3 # push $31 jal Mystery # in: $2, $3; out: $2 lw $1, 0($29) lw $3, 4($29) lw $31, 8($29) addi $29, $29, 12 # pop $1 # pop $3 # pop $31 # readjust stack pointer sw $3, Array($2) # store current value here addi $1, $1, 4 # move current to next element slti $5, $1, 400 # all elements inserted? bne $5, $0, OuterLoop # if not, then continue addi $1, $0, 196 # point to 49th element lw $3, Array($1) # load 49th element addi $1, $1, 4 # point to 50th element lw $4, Array($1) # load 50th element add $3, $3, $4 # sum 49th and 50th sra $2, $3, 1 # compute median (average) jr $31 # return to caller 4
Now consider the following MIPS subroutine that takes two inputs: an array index I in $2 and an integer array element Value in $3. Its result is placed in $2. The label Array is the base address of array A. Mystery:... # L0 InnerLoop: addi $4, $2, -4 # L1 lw $4, Array($4) # L2 slt $5, $3, $4 # L3 beq $5, $0, Exit # L4 sw $4, Array($2) # L5 addi $2, $2, -4 # L6 bne $2, $0, InnerLoop # L7 Exit: jr $31 # L8 Part B (3 points) If $2 holds index I into array A, what does $4 hold at line L3? $4 = A[I-1] Part C (10 points) Write C code that is equivalent to the InnerLoop body of the Mystery subroutine. Assume A is a globally defined array. For maximum credit, choose the most appropriate loop construct and declare and initialize variables as needed. int A[] = {9, 33, -15,...; int Mystery(int I, int Value){ do { if (Value < A[I-1]){ A[I] = A[I-1]; I--; else break; while(i); return(i); 5