Computer Systems: A Programmers Perspective
Computer Systems: A Programmers Perspective
B
0 01 000
0 00 100
0 01 011
*********
Problem 4
*********
unsigned transform(unsigned n)
{
int b, m;
for(m = 0; n != 0; n >>= 1) { // (or) for(m = 0; n > 0; n = n/2)
b = n & 1; // (or) b = n % 2;
if(b == 0) {
continue;
}
m = 2*m + 1; // (or) m = m + m + 1; (or) m = m<<1 + 1;
}
}
return m;
Alternate solution:
-------------------
unsigned transform(unsigned n)
{
int b, m;
for(m = 0; n != 0;) {
b = !(n & 1); // (or) b = (n % 2) - 1;
if(b == 0) {
m = 2*m + 1;
}
n = n >> 1;
}
}
return m;
*********
Problem 5
*********
Part 1.
a X X X X X X X b b b b b b b b
c c c c d d d X e e e e e e e e
f f f f f f f f
Part 2.
f f f f f f f f b b b b b b b b
e e e e e e e e c c c c d d d a
or
a d d d c c c c b b b b b b b b
e e e e e e e e f f f f f f f f
*********
Problem 6
*********
A: phd
B: bachelors
C: masters
*********
Problem 7
*********
int result = 4;
switch(a){
case 0:
case 1:
c = c - 5;
case 2:
result = 4 * c; //or result *= c
break;
case 5:
result = 86547; //or 0x15213
break;
case 3:
c = 2;
case 7:
b = b & c;
default:
result += b; // or result = b + 4
return result;
}
*********
Problem 8
*********
Stack
addresss
4:1:M
0:0:M
C. Final state: 0 X 3 X 1 X 2 3 (c)
0xffff1008
0xffff1004
0xffff1000
0xffff0ffc
0xffff0ff8
0xffff0ff4
+--------------------------------+
|
unknown
|
+--------------------------------+
|
15
|
+--------------------------------+
|
18
|
+--------------------------------+
|
0x080483b7
|
+--------------------------------+
|
0xffff0ff8
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
3
|
+--------------------------------+
|
15
|
+--------------------------------+
|
0x080483b7
|
+--------------------------------+
|
0xffff0fe0
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
0
|
+--------------------------------+
|
3
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
unknown
|
+--------------------------------+
0xffff0ff0
0xffff0fec
0xffff0fe8
0xffff0fe4
0xffff0fe0
0xffff0fdc
0xffff0fd8
0xffff0fd4
0xffff0fd0
0xffff0fcc
0xffff0fc8
0xffff0fc4
0xffff0fc0
0xffff0fbc
0xffff0fb8
0xffff0fb4
0xffff0fb0
Part B:
esp: 0xffff0fcc
ebp: 0xffff0fe0
Grading Rubric:
Part A (8):
Let m be the number of mistakes. The following is a
non-exhaustive list of possible mistakes:
-
result
break;
case 5:
result
case 4:
result
break;
default:
result
= x;
= 2 * x;
= result + y;
= y;
}
return result;
*********
Problem 8
*********
Answer: (B) 8.
For (A) the sequence
For (B) the sequence
For (C) the sequence
Hence, the answer is
*********
Problem 9
*********
The unstated assumption is that C[i][j] is stored in a register.
However, the question as written is ambiguous because it doesn't state
this assumption clearly. Thus we will accept a miss rate of either 1/2
or 1/4 for part A.
Part A:
16 floats in a block
For row-wise A: Pattern is 1 miss, 15 hits, 1 miss, 15 hits, ...
For col-wise B: Pattern is miss, miss, miss, ...
For C[i][j] not in register: pattern is miss, hit, hit, hit, hit, ...
If C[i][j] in register, then there are zero memory accesses to C[i][j] during
each inner loop.
If you assumed that C[i][j] is in a register, then there are only
accesses to A and B in the inner loop. Thus, the miss rate = 17/32 ~
1/2
If you assumed that C[i][j] is not in a register, then we have to
include the accesses to C in the miss rate calulation: Thus, the miss
rate = 17/48, which is closer to 1/4 than 1/2.
Part B:
For row-wise A, B: Pattern is 1 miss, 15 hits, 1 miss, 15 hits, ...
If you assumed that C[i][j] is in a register, then the miss rate is
2/32 = 1/16.
If you assumed that C[i][j] is not in a register, then the miss rate
is 2/48 = 1/24, and the closest rate on the answer key is still 1/16.
67 = 0100 0011
-35 = 1101 1101
B.
/*
* reverseBytes - reverse bytes
*
Example: reverseBytes(0x12345678) = 0x78563412
*
Legal ops: ! ~ & ^ | + << >>
*/
int reverseBytes(int x)
{
int newbyte0 = (x >> 24) & 0xff;
int newbyte1 = (x >> 8) & 0xff00;
int newbyte2 = (x << 8) & 0xff0000;
int newbyte3 = x << 24;
return newbyte0 | newbyte1 | newbyte2 | newbyte3;
}
C.
x = y = 0
None
x = 0
None
x = 0, y = 1
*********
Problem 3
*********
A.
B.
15
C.
1/32
D.
Bits
Value
Bits
Value
011
111
110
110
1
17
9
9 1/2
101
111
000
110
5
NaN
3/32
8 1/2
000
000
001
010
010
010
011
000
*********
Problem 4
*********
A.
B.
dddddddddddddddd
cccccccceeeeeeee
ffffbbbbbbaaaXXX
C.
19
D.
*********
Problem 5
*********
int lolwut(char *s)
{
int i, n;
n = 0;
for(i = 0; s[i] != '\0'; i++)
{
if(s[i] < '0' || s[i] > '9')
return -1;
}
n = n*10 + s[i] - '0';
}
return n;
}
A.
The instruction places the value of %ebx onto the stack, and it decrements
%esp. %ebx is a callee-saved register; since lolwut uses %ebx, its value
upon invocation must be saved so it can be restored before returning to the
caller.
B.
0xffff000c
*********
Problem 6
*********
----------|
d
| <- Start argument build area here
|
c
|
|
b
|
|
a
|
| ret addr|
ebp -> |
%ebp |
|
%ebx |
|
|
|
|
|
|
esp -> |
|
0x1000
*********
Problem 7
*********
A.
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
m
Miss rate = 1
B.
m
m
m
m
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
The second sample performs better than the first because it makes better use
of the cache. In the first, heavy conflict results in a large number of
evictions. In the second, the entire array fits in the cache, and the only
misses are cold misses.
Problem 6
*********
Cases 210, 214, and 218 should have "break".
0x400590:
0x400598:
0x4005a0:
0x4005a8:
0x4005b0:
0x4005b8:
0x4005c0:
0x4005c8:
0x4005d0:
0x4005d8:
0x400470
0x40048a
0x40048a
0x400477
0x40047c
0x40048a
0x400482
0x40048a
0x400482
0x400487
*********
Problem 7
*********
A. (a) Call pushes the return address on the stack, jmp does not.
(b) Ret gets the return address from the top of the stack.
B. x == 5
C. string "0123456" is stored at 0x80484d0
D. buf[0] = 0x33323130
buf[1] = 0x00363534
buf[2] = 0xffffd3e8
buf[3] = 0x080483fc
buf[4] = 0x080484d0
E. Value at %ebp is 0xffffd3e8
F. Value at %esp is 0x080483fc
G. No segfault. "0123456" is only 8 bytes including '\0', and
int[2] can store 8 bytes.
1.
c
a
a,b,c
a
a
b
d
-----------------------------------------------------------------------------Problem 2.
Part 1:
int mystery(int i)
{
if ( i!= 0 ) return i + mystery(i-1);
return i;
}
Part 2:
cmp $0x0, %eax
Part 3:
There is a jump to line 0x8048364 so 0x8048361 is not actually
executed everytime 0x8048364 is.
Problem 3.
Part 1:
0xffd3d208
Part 2:
0x080483cd
Part 3 (table):
arg1:
arg2:
arg3:
arg4:
0xffd3d1f0
0xffd3d1f4
0xffd3d1f8
0xffd3d1fc
15
7/64
NaN
2^(-149)
-----------------------------------------------------------------------------Problem 4:
Part 1:
Key:
_ unshaded
= shaded
bb==xxxx
ss==zzzz
ccccc===
aaaaaaaa
q===____
Part
ms.b
ms.x
ms.s
ms.z
ms.c
ms.a
ms.q
2:
= 0x00bb
= 0x0001d9f9
= 0x3b6d
= 0xbeefbabe
= 0x68, 0x6c, 0x70, 0x6d, 0x65
= 0xdeafelffledfable
= 0x21
Part 3:
(many possible solutions)
One example:
struct my_compressed_struct {
long long a;
long z;
int x;
short b;
short s;
char[5] c;
char q;
Part 4:
Depends on previous solution.
From our example above:
aaaaaaaa
xxxxzzzz
bbsscccc
cq==____
size = 28
-----------------------------------------------------------------------------Problem 5:
c
b
a
ret Addr to caller
old ebp
eax (c)
ecx (b)
edx (a)
0xde
old ebp
eax (c)
ecx (b)
edx (a)
0xde
old ebp <- %ebp
blank
blank
black <- %esp
-----------------------------------------------------------------------------Problem 6:
Step 0: User coulde enter a string long enough to overflow the buffer
and overwrite mains return address to the address of the exploit.
Step 1:
Part 1: 0xf7e263a0
Part 2: 0xff89d95d
Step 2:
garbage | old ebp | system addr | 4 byte garbage | /bin/bash addr
Step 3:
Main will return to the system function with the argument
"/bin/bash". This will open a new shell for the attacker to input
commands.
-----------------------------------------------------------------------------Problem 7:
void mystery(int (*funcP) (int), int a[], int n) {
int q;
for (q = 0; q < n; q++) {
a[q] = funcP(a[q]);
}
}
Value
1
Format B
Bits
011111 00
Value
1
1101
0110
1111
0000
1100
0111
0000
0101
112
23/32
inf
5/1024
100101
011110
110111
010111
11
10
10
01
112
24/32
3*2^23
5/1024
*********
Problem 4
*********
int foo (unsigned int a)
{
int b = 0;
switch (a + 1) {
case 0:
b = a >> 1;
__________;
case 1:
b = ~b;
______;
case 2:
b = -b;
break;
case 3:
b = a;
_____;
case 4:
b = b ^ a;
break; // optional break (falls through either way)
}
return b;
}
4. a ==
a ==
a ==
a ==
a ==
o.w.
-1 => Tmin
0 => 6
1 => -5
2 => 0
3 => 6
=> 5
*********
Problem 5
*********
odin
dva
tri
chetyre
b
e
a
d
*********
Problem 6
*********
a) ecx = rax +
b) eax = *(rdi
eax = *(rdi
c) no stack to
d) x86_64
r8
+ rax)
// rdi is int *
+ 4 * rax) // rdi is char *
return from, no push %ebp
b
c
c
b
b
a
a
b
d
*********
Problem 8
*********
H = 28
J = 15
Decimal
3
1/16
-14
1/4
Binary
-----000001
111011
000100
--------------------*********
Problem 2
*********
a) 32 bytes
b) 6 bytes
c) fun2
fun4
fun3
fun1
d)0x000f
0xbfb2ffdc
0x0000000c
0x0012
0x000000f3
0x000000d5
0x000000c
0x01
*********
Problem 3
*********
int switchfn(inta, longb) {
int y=0,x=)xdeadbeef;
switch(A*b) {
case 1:
return 24;
case 6:
a= x + b*4;
return a;
case 0:
return a+b;
case 4:
x=a;
y*=b;
break;
case 2:
a= y==x;
-7/16
5/4
-5/8
13(12)
100111
001101
101001
011010
case 3:
b = y<x;
case 5:
return a/b;
}
return x==y;
}
*********
Problem 4
*********
void mystery_sort (long* array, long len)
{
long a, b, tmp;
while(len>0)
{
a = 0;
for(b=len-1; b>0; b--)
{
if(array[b]>array[a])
{
a=b;
}
}
len--;
tmp = array[len];
array[len] = array[a];
array[a] = tmp;
}
*********
Problem 5
*********
a
a
b
b
a
d
d
b
b
a
c
Update 10/6/11 by Taerim Kim:
Although C was accepted as the solution in Spring 2009, none of the choices
for Problem 5 Question 11 are correct.
*********
Problem 6
*********
a)
Enter new stack frame by saving old base pointer and
allocating space by subtracting from stack pointer
b)Fill into dissasembly of foo
0x8, 0xc, 0x10, 0x14
or
8, 12, 16, 20
c)
The new foo is not saving the old ebp. This makes
the stack frame 4 bytes shorter
old
-----| args |
| ret |
| ebp | ebp
|
|
|
|
| ... |
|
|
|______| esp
new
-----| args |
| ret |
|
|
|
|
|
|
| ... |
|
|
|______| esp
d)
0x44, 0x48, 0x4c, 0x50
or
68, 72, 74, 80
e)
Advantages:
save space (4 bytes per stack frame)
save time (don't execute enter leave instructions)
%ebp can now be used as GPR
Drawbacks:
makes debugging "impossible" as you can no longer
easily get a stack trace by climbing the stack with the base pointer
*********
Problem 1
********
Expression
Decimal
Binary
------------------------------------|
-17
| 1110 1111
--|
41
| 0010 1001
sa
|
-6
|
1010
b
|
-12
| 1111 0100
sc
|
4
|
0100
ux
|
192
| 1100 0000
TMax
|
127
| 0111 1111
TMax-TMin |
-1
| 1111 1111
----------------------------------*********
Problem 2
********
A.
B.
C.
D.
E.
True.
False.
False.
True.
False.
*********
Problem 3
********
110
100
111
000
11100
10101
00000
00001
|
|
|
|
15
53/16
+Inf
1/128
|
|
|
|
10010
10000
10100
01000
111
101
110
000
*********
Problem 4
********
Blanks should be filled in as:
int **
i < n
j <= i
M[i][j] > 0
i
0
*********
Problem 5
********
H = 6
J = 31
|
|
|
|
15
13/4
56
1/128
*********
Problem 6
********
C
D
A
E
*********
Problem 7
********
long test(long a, long b, long c)
{
long answer = 5;
switch(a)
{
case 5:
c = b ^ 15;
/* Fall through */
case 7: (or 0)
case 0: (or 7)
answer = c + 112;
break;
case 2: (or 4)
answer = (c + b) << 2; (or 12)
break;
case 4: (or 2)
answer = 12; (or (c + b) << 2)
break;
default:
answer = 5;
}
return answer;
}
*********
Problem 8
********
A.
Allocate bytes as:
aaaabbxxcccccccc
dxxxeeeefxxxgggg
hhhhxxxx
B. Answers vary.
ccccccccaaaaeeee
gggghhhhbbdfxxxx
C. 12
D. 4
|
|
|
|
|
|
|
|
|
|
Binary
011011
100100
101011
011
101011
111000
001000
110
100000
*********
Problem 2
********
A.
2^(-149)
NaN
2^24 + 1
B.
False (Tmin)
True
False (TMax, TMin)
True
*********
Problem 3
********
A.
0 001 1000
1 111 0000
0 101 0010
0.6875 or 21/32
-0.125 or -1/8
NaN
B.
0 000 1111 = 15/64 = 0.234375
C.
*********
Problem 4
********
int **
A[i][j]*i*j
*********
Problem 5
********
H = 9
J = 62
*********
Problem 6
********
A
B
C
D
Wed
Mon
Thu
Tue
*********
Problem 7
********
long test(long a, long b, long c)
{
long answer = 0;
switch(c)
{
case 3:
answer = b-a;
break;
case 2: (or 7)
case 7: (or 2)
answer = b+42;
break;
case 0:
a = a+b;
/* Fall through */
case 6:
answer = a^b;
break;
default:
answer = 0;
}
}
return answer;
*********
Problem 8
********
A.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -|a |xx|us|us|b |xx|cn|cn|c |xx|xx|xx|xx|xx|xx|xx| e|u | p|r |i |c |e |e | d|
xx|
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -|xx|xx|uk|uk|uk|uk| | | | | | | | | | | | | | | | | | | |
|
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -B. Answers vary.
C. 12
D. 4
Rounded
7/8
1
8
10
value
no rounding needed
round up to even, renormalize
round down to even
no rounding needed
*********
Problem 3
*********
int foo (int n) {
int a, i;
a = 0;
for (; n > 1; n--) {
a = a + n;
for (i = 0; i < n; i++)
a = a + i;
}
return a;
*********
Problem 4
*********
H = 127
J = 3
*********
Problem 5
*********
Part A.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| vintages
|xx xx xx xx|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| cost
|z |xx xx xx xx xx xx xx|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| next
| ages
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|xx xx| type
|a |xx xx xx xx xx xx xx|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Part B. How many bytes of space in WineNode are wasted? 20
Part C.
typedef struct WineNode {
int vintages[3];
short ages[5];
char a;
char z;
int type;
double cost;
WineNode *next;
} WineNode;
Note: there may be several correct solutions for Part C:
Part D:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| vintages
| ages
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|a |z | type
|xx xx xx xx|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| cost
| next
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Part E.
----------------------------------*********
Problem 2
*********
------------------------------------------------------------------Format A
Format B
bits
Value
Bits
Value
Comment
------------------------------------------------------------------101 1110
15/2
1001 111
15/2
010 1001
25/32
0110 101
13/16
Round Up
110 1111
31/2
1011 000
16
Round Up
000 0001
1/64
0001 000
1/64
Denorm-->Norm
------------------------------------------------------------------*********
Problem 3
*********
A 40-byte string results in the low order byte of
the return address being overwritten with a '\0'
byte, which changes the return address from 0x400f7c to
0x400f00, coincidently, the address of smoke().
*********
Problem 4
*********
L = 9
M = 33
N = 2
*********
Problem 5
*********
0x400690:
0x4006a0:
0x4006b0:
0x00000000004004d1
0x00000000004004bb
0x00000000004004d4
*********
Problem 6
*********
int bar2(int x)
{
int y = 0;
int z = x/4;
for( ; z > 0 ; y++)
z = z/4;
return y;
}
}
*********
Problem 7
*********
apr
0x00000000004004b8
0x00000000004004c0
0x00000000004004c3
jan
feb
mar
*********
Problem 8
*********
A.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|id| X|code |
amount | name |X X X X X |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
data
|in| X X X X X X X|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
next
| address
|X X X
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
B. size(Union_1) = 48 bytes
C. (a) Output #1 is u->value = 0x6c6c6548
(b) Output #2 is u->buf
= Hello WoSM
(c) Output #3 is u->buf
= He
Register
%eax
%ebx
%ecx
%edx
%esi
Variable
l, A[j], j
r, j
i
A[i], t
A
%edi
%esp
%ebp
x
stack pointer
stack frame pointer
B.
These are callee save registers.
C.
static int bunny(int l, int r, int *A) {
int x = A[l];
int i = l - 1;
int j = r + 1;
while(i < j) {
do { j--; } while(A[j] > x);
do { i++; } while(A[i] < x);
if(i < j) {
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
return j;
}
D.
draft_horse() {
if() {
bunny();
draft_horse();
draft_horse();
}
return;
}
E.
This is the code for quicksort.
4.
char *note;
} Win_Compact;
E.
0x004e4143
7.
T, F, T, F, F, T, F
*********
Problem 3
*********
fun6
*********
Problem 4
*********
fall
summer
spring
winter
*********
Problem 5
*********
IA-32 Windows:
1
2
3
||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ||
c - - - - - - - d d d d d d d d s s - - p p p p f f f f p p p p ||
||
IA-32 Linux:
1
2
||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 ||
c - - - d d d d d d d d s s - - p p p p f f f f p p p p ||
||
*********
Problem 6
*********
int mystery(int a, int b, int c)
{
int x, y;
y = c;
for (x = a + b; x > 0; x--)
y++;
return y;
}
*********
Problem 7
*********
A.
buf[0] = 0x64636261
buf[1] = 0x68676665
buf[2] = 0x08040069
B.
ebp = 0x68676665
C.
eip = 0x08040069
*********
Problem 8
*********
%eax = 0x400446e8
X
1,4
3
2
Problem 6
=========
1.
0xbfffb30c
2.
68 fc b2 ff bf c3 xx xx
xx xx xx xx pq rs tu vw
fc b2 ff bf 00
where xx can be anything except 00,
and pqrstuvw can be any valid hexademical values which aren't zeros
3.
Problem 7
=========
#define M 11 //two points for each index
#define N 3
int arr1[M][N];
int arr2[M][N];
int moo(int x)
{
int i;
for(i = 0; i < M; i++) //1 point for this line
{
arr1[i][0] = arr2[i][2]+4*x; //a point for each index, plus 2 for the
product
}
return i; //1 point for this line
}
Problem 8
=========
A.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |XXXXX|
weight
|components |mom |XXXXX|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
B.
typedef struct {
double weight;
double *components;
short momentum;
char ID[2];
} Modified;
C.
16
y-z
|
0
| 0 0000
TMax
|
15
| 0 1111
TMin
|
-16
| 1 0000
----------------------------------*********
Problem 2
*********
Part I
A. Denormalized numbers
(a) E = -6
(b) M = 31/32
B. Normalized numbers
(a) E = -6
(b) E = +7
(c) M = 63/32
Part II
----------------------------------------------------------Description
| E |
M |
V
|
Binary
----------------------------------------------------------Zero
| -6 |
0 |
0
| 0 0000 00000
----------------------------------------------------------Smallest positive
| -6 | 1/32 | 1/2048 | 0 0000 00001
----------------------------------------------------------Largest denorm
| -6 | 31/32| 31/2048 | 0 0000 11111
----------------------------------------------------------Smallest pos norm
| -6 |
1 | 32/2048 | 0 0001 00000
----------------------------------------------------------One
| 0 |
1 |
1
| 0 0111 00000
----------------------------------------------------------Largest odd integer | 5 | 63/32|
63
| 0 1100 11111
----------------------------------------------------------Largest finite number| 7 | 63/32|
252
| 0 1110 11111
----------------------------------------------------------Positive infinity
| - |
- |
+inf | 0 1111 00000
----------------------------------------------------------*********
Problem 3
*********
M=3
N=9
*********
Problem 4
*********
A.
Product_Struct1:
0
end of struct
|
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|name
|XX|type |model
|c |XXXXXXXX|price
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Product_Struct2:
end of struct
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|name
|type |c |XX|model|XXXXX|price
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
B.
sizeof(Product_Struct1) = 24
sizeof(Product_Struct2) = 16
sizeof(Product_Union) = 24
C.
(a)
(b)
(c)
(d)
(e)
p->product_id = 0x64636261
p->two.name = 0x64636261
p->two.type = 0x6665
p->two.color = 0x00
p->two.model = 0x2ace
*********
Problem 5
*********
(c) fun6
*********
Problem 6
*********
A.
return addr:
saved %ebp :
&buf
:
saved %ebx :
%esp
:
0x04
0x00
0xf8
0xd8
0xd8
B. (%ebp) = 0x6874726f
C. 0x080484e3
*********
Problem 7
*********
int foo(int x, int y, int z)
{
int i, result;
result = y;
for (i = x; i >= z; i--) {
result = result + i;
}
return result;
0
1
2
4
8
14
16
24 26
32
|--------------------------------------------------------|
| c[0] | c[1] | pad | intp | union1 | pad | d | s | pad |
|--------------------------------------------------------|
C. 32 bytes
D. 24 bytes
Question 4:
int foo (int op, int a, int b)
int result = 0;
switch (op)
{
case 0: result
case 1: result
case 2: result
case 3: result
case 4: result
}
=
=
=
=
=
a &
a |
a ^
~a
a +
b;
b;
b;
;
b;
break;
break;
break;
break;
break;
return result;
Question 5:
A.
-- alloc -Uses the next free BP register to save the number of the last
used stack register.
Why don't we have to save the 'old base pointer'?
There is a separate register for each stack frame. Hence, there's no
need to save any values - they are automatically saved by the BPn
registers.
-- push -Uses the next free STK register to save data on the 'stack' (and
increments/decrements some index into the STK registers)
-- pop -Takes the value from the last used register and decrements/increments
some index into the STK registers.
-- call -Uses the next free RET register to save the return address.
Also jumps to the procedure
of memory.
It's not really a base pointer in the same sense as we know, and the
solution to the problem at hand (putting the 'base pointer' in the BP
registers) isn't necessarily good either, but at some point, system
software
will have to save out these registers to a memory area, since the stack
must remain valid
Many people thought that we would still need some memory address to serve
as the base pointer because we need it to index on the stack. However,
imagine the case where the STK, BP, and RET sets of registers are
infinitely large. Following the above solution for the operation of each
instruction, it is possible to see that there is no reason to keep
a base pointer (since there is a BP register for each stack frame and all
the BP registers do is store an index - not a pointer) to be able to
index into data.
Question 6:
Part A: Send some negative number for index such that cmds[index] is a bad
address.
Part B: Overflow buffer to set the return address to point into the buffer
you sent, which contains the code to execute.
Part C: The head of the buffer should contain the /etc/passwd entry.
Overflow buffer to set return address into the buffer, which sets
result_fname to point to "/etc/passwd" (stored in the sent buffer),
and then code to jump to the line with open().
Question 7:
FindMin:
push %ebp
push %ebp
mov %esp, %ebp
push %ebx
mov 0x8(%ebp), %ecx
mov 0xc(%ebp), %edx
mov (%ecx), %eax
mov $0x0, %ebx
cmp %edx, %ebx
jge .done
.loop
cmp %edx, (%ecx, %ebx, 4)
jge .incr
mov (%ecx, %ebx, 4), %eax
.incr
inc %ebx
cmp %edx, %ebx
jl .loop
.done
pop %ebx
mov %ebp, %esp
-------
pop %ebp
ret
Problem 5
*********
M=15
N=9
*********
Problem 6
*********
int foo(int a)
{
int i;
int result = a + 2;
for (i=0; i < a; i++) {
result += (i + 5);
result *= (i + 3);
}
return result;
}
*********
Problem 7
*********
A.
OldSensorData:
end of struct
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|code |XXXXX|
start
| raw
|XX|
data
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
NewSensorData:
end of struct
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|code |start|
raw
|XX|sense| ext |XXXXX|
data
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
B.
newData->start
newData->raw[0]
newData->raw[2]
newData->raw[4]
newData->sense
=
=
=
=
=
0xff00
0xb8
0x50
0xe1
0x008f
*********
Problem 8
*********
A.
<- buf[0] -><- buf[1] ->
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
|64|72|2e|65|76|69|6c|00|
|
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
^
ebp
---------- addresses increase this way ------>
The printf inside main prints 0x6c6976 (or 0x006c6976 is OK too)
B.
(a) buf[0] = 0x652e7264
buf[3] = 0x08040073
(b) %ebp = 0x6576696c