Practice Midterm PDF
Practice Midterm PDF
Lost at C? (12 points): The following problem assumes the following declarations:
int x = rand();
float f= foo(); // f is not NaN
unsigned ux = rand();
For
the
following
C
expressions,
circle
either
Y
or
N
(but
not
both).
If
you
circle
the
right
answer,
you
get
+2
points.
If
you
circle
the
wrong
answer,
you
get
-‐1
point.
If
you
do
not
circle
anything,
you
get
0
points.
So
do
not
just
guess
wildly.
Always
True?
a. x > 0 ⇒ ((x<<4)>>5)>0 Y N
b. f > 0 ⇒ ((f<<4)>>5)>0 Y N
c. (x>>20)==(~(x>>20)+1) ⇒ x ==(int)(float) x Y N
d. x ≤ 0, f ≤ 0 ⇒ x*f ≤ 0 Y N
f. ux – 2 ≥ -2 ⇒ ux ≤ 1 Y N
Note that “⇒” represents an implication. A ⇒ B means that you assume A is true, and your answer
should indicate whether B should be implied by A – i.e. given that A is true, is B always true?
1
2. Boole’s Foolery (8 points): Consider the following sequence of operations on integers x and y:
x = x ^ (~y);
y = y ^ x;
A) x
B) ~x
C) –x
D) y
E) ~y
F) –y
2
3. Lucky Number Seven (7 points): Consider the following expression:
~(((~(7<<7)+7)^~7)>>7)
What decimal integer value would this evaluate to? Simplify as much as possible.
struct S76 {
unsigned int ID;
char name[20];
short zip;
long misc;
} my_data[10];
3
5. Bit Off More Than You Can Chew? (10 points): Consider the code fragment below:
union
{
int
x;
unsigned
int
u;
float
f;
char
s[4];
}
testout;
testout.x=0x40000000;
What would be printed for each of the following statements:
a. printf("%d", testout.x);
b. printf("%u",
testout.u);
c. printf("%f",
testout.f);
d. printf("%c
%c
%c
%c",
testout.s[3],
testout.s[2],
testout.s[1],
testout.s[0]);
How many bytes would testout occupy in memory?:
e. # of bytes: ______________
4
6. Let Me EAX Another Question (15 points): Consider the following array reference:
hash_table[(index&255)^((index>>8)&255)];
We will implement this reference in an assembly code fragment. Assume that we want to store the value of this
reference in register %eax. The code fragment will be run on a 32-bit little-endian machine. The assembly
code fragment is below – with some blanks left for you to fill in.
To help you fill in the blanks – here’s some interaction with gdb to get some key values you will need. This
interaction takes place immediately before the assembly fragment above is executed. The following
interaction takes place before the code is executed:
5
7. Magic 8 Ball says “Success is not likely” (10 points): You are debugging an application in execution
using gdb on a 32-bit (i.e. pointers use 32 bits), little-endian architecture. The application has a variable
called magic8ball - defined as
char magic8ball[8][8][8];
Using gdb you find the following information at a particular stage in the application:
(gdb)
p
&magic8ball
$1
=
(char
(*)[8][8][8])
0xffffd448
And:
6
0xffffd688:
0x6279614d
0x00a50065
0x6576654e
0x00000072
0xffffd698:
0x68676952
0x08040074
0x6e6f7257
0x08040067
0xffffd6a8:
0x6576654e
0x00000072
0x6279614d
0x00a50065
0xffffd6b8:
0x00006f4e
0x00000000
0x68616559
0x00000000
0xffffd6c8:
0x656b694c
0x0000796c
0x0068614e
0x00000000
0xffffd6d8:
0x0068614e
0x00000000
0x00736559
0x00000000
0xffffd6e8:
0x656b694c
0x0000796c
0x68616559
0x00000000
0xffffd6f8:
0x0068614e
0x00000000
0x68616559
0x00000000
0xffffd708:
0x6279614d
0x00a50065
0x68616559
0x00000000
0xffffd718:
0x6576654e
0x00000072
0x6e6f7257
0x08040067
0xffffd728:
0x6e6f7257
0x08040067
0x00006f4e
0x00000000
0xffffd738:
0x6279614d
0x00a50065
0x6e6f7257
0x08040067
0xffffd748:
0x0068614e
0x00000000
0x68676952
0x08040074
0xffffd758:
0x65727553
0x00000000
0x00006f4e
0x00000000
0xffffd768:
0x68616559
0x00000000
0x0068614e
0x00000000
0xffffd778:
0x0068614e
0x00000000
0x68676952
0x08040074
0xffffd788:
0x00736559
0x00000000
0x68616559
0x00000000
0xffffd798:
0x00006f4e
0x00000000
0x68616559
0x00000000
0xffffd7a8:
0x68616559
0x00000000
0x656b694c
0x0000796c
0xffffd7b8:
0x68676952
0x08040074
0x00006f4e
0x00000000
0xffffd7c8:
0x6576654e
0x00000072
0x6e6f7257
0x08040067
0xffffd7d8:
0x00736559
0x00000000
0x6576654e
0x00000072
0xffffd7e8:
0x0068614e
0x00000000
0x656b694c
0x0000796c
0xffffd7f8:
0x65727553
0x00000000
0x00736559
0x00000000
0xffffd808:
0x65727553
0x00000000
0x65727553
0x00000000
0xffffd818:
0x6576654e
0x00000072
0x656b694c
0x0000796c
Hint – don’t forget gdb’s trick about reversing byte ordering within each 4-byte chunk.
If the application were to output the value of magic8ball[3][2] – what would it be? i.e. what would be
returned from the statement printf(“%s”, magic8ball[3][2]);
7
8. I Cannot Function in this Environment (15 points): The following two procedure fragments are part of
a program compiled on an x86-64 architecture.
00000000004004a0
<func2>:
4004a0:
8d
04
37
lea
(%rdi,%rsi,1),%eax
4004a3:
0f
bf
d2
movswl
%dx,%edx
4004a6:
29
d0
sub
%edx,%eax
4004a8:
c3
retq
00000000004004b0
<func1>:
4004b0:
0f
bf
d2
movswl
%dx,%edx
4004b3:
48
8d
34
f6
lea
(%rsi,%rsi,8),%rsi
4004b7:
c1
e7
08
shl
$0x8,%edi
4004ba:
01
d2
add
%edx,%edx
4004bc:
0f
bf
d2
movswl
%dx,%edx
4004bf:
48
01
f6
add
%rsi,%rsi
4004c2:
e9
d9
ff
ff
ff
jmpq
4004a0
<func2>
8
9. Now That’s a Switch (15 points): A switch statement is described via the following assembly code
fragments on x86-64:
Three variables (i, j, k) are used in the switch statement – all three are declared as type int. Before the
start of the code fragment above, variable i is in %eax, variable j is in %edp, and variable k is in %ecx. Using
this information, fill in the cases for the switch statement on the solution page.