## Midterm Exam Fall 2017 (time: 60 minutes)

### Notes:

• If you perceive any ambiguity in any of the questions, state your assumptions clearly.
• Questions vary in difficulty; it is strongly recommended that you do not spend too much time on any one question.
• The exam consists of 4 pages, 9 questions, and a total of 50 points.

### 1.(5 points) Circle the correct answer among the choices given. If you circle more than one answer, you will lose the grade of the corresponding question.

(A) If your program consists of 5 C files, the output of the linker consists of :

1. one file
2. five files
3. depends on the compiler
4. depends on whether we have 32-bit or 64-bit machines

(B) Assume a signed int x, what does the following expression present:

(1 + (x<<3) + ~x + x)

1. 7x b. 8x c. 8x + 1
2. 7x + 1
3. none of the above

(C) Suppose we have a 32-bit machine. The size of “short int *” is:  代考计算机系统组织

1. 4 bytes
2. 8 bytes
3. 2 bytes
4. Depends on the compiler.

(D) Suppose we have a 64-bit machine. The size of “short int *” is:

1. 4 bytes
2. 8 bytes
3. 2 bytes
4. Depends on the compiler.

(E) If we write a C program that includes an undefined variable. Then:

1. the compiler will complain
2. the assembler will complain
3. the linker will complain
4. the loader will complain

### 2.[4 points] Floating point variables take 4 bytes, same size as int variables. Yet, the range of numbers that float can present is much bigger than int. So, why don’t we just get rid of int in programming languages and just use float. (Your answer must not exceed 2 sentences).

3. [6 points] State one advantage and one disadvantage of MACROs.

[Advantage]

[Disadvantage]

### 4.Suppose we have the following decimal number: -23  代考计算机系统组织

a) [3 points] Write that number in an 8-bit binary number. To get full credit, show all the steps.

b) [2 points] Translate the number you calculated in a) above to hexadecimal.

5.[6 points] Suppose x is an integer. Write one C statement that multiplies x by 51 without using any multiplication operation. That is x *= 51; or x = x * 51; are not accepted. Also x =x+x+x+ … 51 times and the like will not be accepted (hint: think in terms of shifting and addition operations).

### 6. [10 points]The following C code initializes an array of 500 elements with the numbers from 0 to 499.

That is, first element of the array is initialized to 0, second element to 1, etc. Then it adds these numbers and puts the result in sum. Finally, it prints sum on the screen. The code has 5 mistakes. Indicate those mistakes and show how you can fix them. Put your answer in the table below the code. (Note: some of these mistakes may NOT make the compiler complain)

#include < stdio.h>

int main()

{

char x;

int * y;

int sum;

y = malloc(500*sizeof(int));

for( x = 0; x < 500; x++)

*(y + x ) = x;

for( x = 0; x < 1000; x ++)

sum + = y[x];

printf(“sum = %d\n”, sum);

–some other function calls not relevant to the problem —

return 0;

}

### 7.[2 points] We say that the memory is designed such that each byte has an address. Why don’t we simplify the memory design and give one address to every 4 bytes for example?8. Suppose we have the variable x declared as char and initialized to some value.  代考计算机系统组织

a) [2 points] Write one C statement that sets the least significant bit to 1, leaving all other bits intact.

b) [2 points] Based on what you did in a) did x become even number? Odd number? Or it has no relation.

c) [2 points] Write an if-statement in C that puts x to zero if its most significant bit is 1.

d) [2 points] Repeat question c) above but use a different condition in if-statement to check for the same condition.

9.[4 points] We saw that we can specify addresses in x86 as D(Ra, Rb, S)

a) Why such complicated form is needed and not just use the simpler (Register) ?

b) S can only take the values 1, 2, 4, and 8. Why is that?