## CS360 Midterm Exam #1. February 21, 2017. James S. Plank

The programs for all five problems are here (even questions four and five). Compile them with the -m32 flag to force them to use 4-byte pointers. I was (in my opinion) quite generous with partial credit.

### Question 1: 20 points

• Line 1: Here's a graphic for line one:

• Line 2: Here's a graphic for line two:

• Line 3: This will zero out every other digit in i, starting with the highest one.: 0x07040802.

• Line 4: This will zero out every other digit in i, starting with the lowest one: 0x50406010.

• Line 5: This will turn every other digit to f, starting with the lowest one: 0x7f2f0f3f.

• Line 6: This will shift i by four hex digits: 0x00005744.

• Line 7: This will shift j by three hex digits: 0x10735000.

• Line 8: c is the last two digits of i: 0x12. Therefore, the answer is 0x00000012.

• Line 9: Since d is a byte, the left-shift gets rid of the '1'. d is therefore 0x20: The answer is 0x00000020.

• Line A: The first four digits of i and n are the same, so the XOR turns them into zero. The last four digits of n are zero, so they become equal to the last four digits of i. The answer is 0x00006812.

• Line B: Since the last byte of n is 0x00, adding 16 turns it into 0x10. The answer is 0x57440010.

2 points each for lines 1 through 7. 1.5 points each for lines 8-B.

### Question 2: 20 points

Here's ASCII art for x, y and z: (using an asterisk for the NULL character):
```0123456789ABCDEFGHIJ0123456789*
^         ^         ^
x         y         z
```
The strcpy(z, "-") command turns it into the following:
```0123456789ABCDEFGHIJ-*23456789*
^         ^         ^
x         y         z
```
And the strcat(y, "#") command turns it into the following:
```0123456789ABCDEFGHIJ-#*3456789*
^         ^         ^
x         y         z
```
Finally, the two lines that set "*y" and "z[5]":
```0123456789*BCDEFGHIJ-#*34*6789*
^         ^         ^
x         y         z
```
Below, I show the five values of x in the for loop:
```0123456789*BCDEFGHIJ-#*34*6789*
^      ^      ^      ^      ^
```
```UNIX> a.out
0123456789
789
EFGHIJ-#
#
89
UNIX>
```

Four points per line.

### Question 3: 15 points with 6 extra credit

If we look at the eight bytes of i, they are stored in little endian:

i[0]
 0x67 0xef 0xcd 0xab
i[1]
 0xeb 0x24 0xda 0x13

So, c[0] through c[3] are the four bytes of i[0], and c[4] through c[7] are the four bytes of i[1]. Thus, the first two lines are:

```1: 0x67 0xef 0xcd 0xab
2: 0xeb 0x24 0xda 0x13
```
Now, when we perform the memcpy this copies c[2] through c[5] into v:

v
 0xcd 0xab 0xec 0x24

When we print v in hex, it prints the above four bytes in little endian:

```3: 0x24ebabcd
```
The next statements copy c[0] and c[1] into the first two bytes of v (remember, vp points to the first byte of v), and c[6] and c[7] into the last two bytes of v. This turns v into:

v
 0x67 0xef 0xda 0x13

When we print v in hex, it prints the above four bytes in little endian:

```4: 0x13daef67
```
Now, when we add 0x10101 to i[0] that increments each of the first three bytes by one. i[0] is now:

i[0]
 0x68 0xf0 0xce 0xab

So, when we print out the bytes, we get:

```5: 0x68 0xf0 0xce 0xab
```
Finally, when we add one to c[4] and two to c[5], the state of i becomes:

i[0]
 0x68 0xf0 0xce 0xab
i[1]
 0xec 0x26 0xda 0x13

When we print the two values in i, they are printed in little endian:

```6: 0xabcef068
7: 0x13da26ec
```
Putting it all together, the answer is:
```UNIX> gcc -m32 q3.c
UNIX> a.out
1: 0x67 0xef 0xcd 0xab
2: 0xeb 0x24 0xda 0x13
3: 0x24ebabcd
4: 0x13daef67
5: 0x68 0xf0 0xce 0xab
6: 0xabcef068
7: 0x13da26ec
UNIX>
```

Three points per line.

### Question 4: 20 points

• Lines 1-3: Here's a graphic for the first three lines. This should let you follow x, y, z, and their dereferences.

So, the first three lines are:

 ```0 @ 8 ```

• Lines 4-6: When we increment i, we are adding four to it, because it points to a four-byte quantity (an integer). Here's a graphic for these lines:

So, the next three lines are:

 ```2028278088 2028278092 2028278088 ```

• Lines 7-10: When we increment d, we are adding eight to it, because it points to an eight-byte quantity (a double). Here's a graphic for these lines:

When you are calculating e[2], you need to add eight to e, because e points to a four-byte quantity (a pointer). Then, you add eight to e[2] to find e[2][1]. So, the last four lines are

 ```2.27934735e+274 2.27939357e+274 2.27938697e+274 2.27939357e+274 ```

You can verify this by compiling and running the program:
```UNIX> gcc -m32 q4.c
UNIX> a.out
0x78e51120 2.27938697e+274    2028278064    0x78e51130    '0' | -- | -- |'x'
0x78e51124                    2028278084    0x78e51144    'D' | -- | -- |'x'
0x78e51128 2.27936055e+274    2028278088    0x78e51148    'H' | -- | -- |'x'
0x78e5112c                    2028278068    0x78e51134    '4' | -- | -- |'x'
0x78e51130 2.27934735e+274    2028278080    0x78e51140    '@' | -- | -- |'x'
0x78e51134                    2028278060    0x78e5112c    ',' | -- | -- |'x'
0x78e51138 2.27938697e+274    2028278064    0x78e51130    '0' | -- | -- |'x'
0x78e5113c                    2028278084    0x78e51144    'D' | -- | -- |'x'
0x78e51140 2.27940017e+274    2028278072    0x78e51138    '8' | -- | -- |'x'
0x78e51144                    2028278092    0x78e5114c    'L' | -- | -- |'x'
0x78e51148 2.27939357e+274    2028278072    0x78e51138    '8' | -- | -- |'x'
0x78e5114c                    2028278088    0x78e51148    'H' | -- | -- |'x'
1: 0
2: @
3: 8
4: 2028278088
5: 2028278092
6: 2028278088
7: 2.27934735e+274
8: 2.27939357e+274
9: 2.27938697e+274
A: 2.27939357e+274
UNIX>
```

Two points per line.

### Question 5: 20 points

The struct ms data structure consumes 16 bytes:
• The first four bytes are the characters a, b, c and d.
• The next four bytes are the integer e.
• The next four bytes are the (char *) f.
• The last four bytes are the (struct ms *) g.
Here is an explanation of each line of output:
• Line 1: This was given in the problem description: 0x61626324.
• Line 2: These are the four characters that start at byte 0x61626324: ": c b a".
• Line 3: This is the integer that starts at byte 0x61626328: 1633837928.
• Line 4: p->f is the (char *) that starts at byte 0x6162632c. Its value is 0x61626333. So the answer is the four chars that start at 0x61626333: "a . c b".

• Line 5: Since the size of Mystruct is 16, this will be p+16: 0x61626334.
• Line 6: These are the four characters that start at byte 0x61626334: ". c b a".
• Line 7: This is the integer that starts at byte 0x61626338: 1633837934.
• Line 8: p[1].f is the (char *) that starts at byte 0x6162633c. Its value is 0x6162636e. So the answer is the four chars that start at 0x6162636e: "b a L c".

• Line 9: Since the size of Mystruct is 16, this will be p+4*16: 0x61626364.
• Line A: These are the four characters that start at byte 0x61626364: "7 c b a".
• Line B: This is the integer that starts at byte 0x61626368: 1633837890.
• Line C: q->f is the (char *) that starts at byte 0x6162636c. Its value is 0x6162634f. So the answer is the four chars that start at 0x6162634f: "a 0 c b".

• Line D: This is stored p+12: 0x61626330. Its value is 0x6162632c
• Line E: These are the four characters that start at byte 0x6162632c: "3 c b a".
• Line F: This is the integer that starts at byte 0x61626330: 1633837868.
• Line G: q->f is the (char *) that starts at byte 0x61626334. Its value is 0x6162632e. So the answer is the four chars that start at 0x6162632e: "b a , c".

• Line H: q[1] starts 16 bytes beyond q: 0x6162363c. Therefore, q[1].f is the pointer stored at 0x61626348: 0x61626368.
• Line I: These are the four characters that start at byte 0x61626368: "B c b a".
• Line J: This is the integer that starts at byte 0x6162636c: 1633837903.
• Line K: q->f is the (char *) that starts at byte 0x61626370. Its value is 0x6162634c. So the answer is the four chars that start at 0x6162634c: "' c b a".

To summarize:
```UNIX> gcc -m32 q5.c
UNIX> a.out
0x61626324    1633837882    0x6162633a    ':' |'c' |'b' |'a'
0x61626328    1633837928    0x61626368    'h' |'c' |'b' |'a'
0x6162632c    1633837875    0x61626333    '3' |'c' |'b' |'a'
0x61626330    1633837868    0x6162632c    ',' |'c' |'b' |'a'
0x61626334    1633837870    0x6162632e    '.' |'c' |'b' |'a'
0x61626338    1633837934    0x6162636e    'n' |'c' |'b' |'a'
0x6162633c    1633837934    0x6162636e    'n' |'c' |'b' |'a'
0x61626340    1633837904    0x61626350    'P' |'c' |'b' |'a'
0x61626344    1633837887    0x6162633f    '?' |'c' |'b' |'a'
0x61626348    1633837928    0x61626368    'h' |'c' |'b' |'a'
0x6162634c    1633837863    0x61626327    ''' |'c' |'b' |'a'
0x61626350    1633837872    0x61626330    '0' |'c' |'b' |'a'
0x61626354    1633837909    0x61626355    'U' |'c' |'b' |'a'
0x61626358    1633837933    0x6162636d    'm' |'c' |'b' |'a'
0x6162635c    1633837860    0x61626324    '\$' |'c' |'b' |'a'
0x61626360    1633837880    0x61626338    '8' |'c' |'b' |'a'
0x61626364    1633837879    0x61626337    '7' |'c' |'b' |'a'
0x61626368    1633837890    0x61626342    'B' |'c' |'b' |'a'
0x6162636c    1633837903    0x6162634f    'O' |'c' |'b' |'a'
0x61626370    1633837900    0x6162634c    'L' |'c' |'b' |'a'
0x61626374    1633837906    0x61626352    'R' |'c' |'b' |'a'
0x61626378    1633837944    0x61626378    'x' |'c' |'b' |'a'
0x6162637c    1633837927    0x61626367    'g' |'c' |'b' |'a'

1: 0x61626324
2: : c b a
3: 1633837928
4: a . c b

5: 0x61626334
6: . c b a
7: 1633837934
8: b a L c

9: 0x61626364
A: 7 c b a
B: 1633837890
C: a 0 c b

D: 0x6162632c
E: 3 c b a
F: 1633837868
G: b a , c

H: 0x61626368
I: B c b a
J: 1633837903
K: ' c b a

UNIX>
```