Computer Systems: A Programmers Perspective

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 43
At a glance
Powered by AI
The document contains exam solutions covering a range of computer science problems, including binary representations, floating point values, algorithms and data structures.

The exam solutions cover problems related to binary, floating point, algorithms and data structures, assembly language and computer architecture.

The binary representations are given in both decimal and binary formats.

Exam 1 Solutions

15-213 / 18-213 Fall 2012


*********
Problem 1
*********
1-a 2-c 3-d 4-c 5-a 6-b 7-c 8-(b or d) 9-c 10-d
The correct answer for 8 was initially listed as d) temporal locality,
but the correct answer is actually spatial locality. While it's true
that blocking in things like matmult primarily exploits temporal
locality, blocking is effective for transpose because it exploits
spatial locality by effectively using the entries in each cache line;
there is no reuse.
*********
Problem 2
*********
Expression
4b decimal 4b binary
6b decimal
6b binary
-----------------------------------------------------------------8
|
-8
| 1000
|
-8
| 11 1000
-TMin
|
-8
| 1000
| -32
| 10 0000
x >> 1
|
-3
| 1101
|
-3
| 11 1101
(-x ^ -1) >> 2 |
-2
| 1110
|
-2
| 11 1110
---------------------------------------------------------------*********
Problem 3
*********
| A
|
One
| 0 011 00 |
1/2
| 0 010 00 |
11/8 | 0 011 10 |

B
0 01 000
0 00 100
0 01 011

Exact in both formats


Exact in both formats, norm in A, denorm in B
Format A round to even, format B exact

*********
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

The diagram starts with the


arguments for foo()
+-----------------------------------+
0xffffd850|
5
|
+-----------------------------------+
0xffffd84c|
4
|
+-----------------------------------+
0xffffd848|
3
|
+-----------------------------------+
0xffffd844| caller ra: 0x080483c9
|
+-----------------------------------+
0xffffd840| old ebp: ffffd858
| <- Part B: %ebp=0xffffd840
+-----------------------------------+
0xffffd83c|
3
|
+-----------------------------------+
0xffffd838|
4
|
+-----------------------------------+
0xffffd834| foo ra: 0x08048397
| <- Part C: esp=0xffffd834
+-----------------------------------+
0xffffd830| old ebp: 0xffffd840
| ok to omit, not part of the
stack anymore
+-----------------------------------+
0xffffd82c|
|
+-----------------------------------+
0xffffd824|
|
+-----------------------------------+
}
*********
Problem 9
*********
A. TTSSSBBB
B.
Set:Tag:hit/miss
0:1:M
6:2:M
0:1:H
7:3:M
6:2:H
2:2:M
2:3:M
6:2:H

4:1:M
0:0:M
C. Final state: 0 X 3 X 1 X 2 3 (c)

Exam 1 Solutions 15-213 / 18-213 Fall 2011


*********
Problem 1
*********
1-c 2-b 3-c 4-c 5-d 6-c 7-b 8-c
*********
Problem 2
*********
umax: 63
tmin: -32
(unsigned) ((int) 4): 4
(unsigned) ((int) -7): 57
((unsigned) 0x21) << 1) & 0x3f: 0x02
(int) (20 + 12): -32
12 && 4: 1
(! 0x15) > 16: 0
*********
Problem 3
*********
Value
FP bits Rounded value
1
100 10
3
normalized, exact
9
110 00
8
normalized, round down, because 10 is odd
3/16
000 11
3/16
exact, denorm
15/2
110 00
8
normalized, round up, change exponent
*********
Problem 4
*********
1st blank: n == 0 or n < 1
2nd blank: n >> 1 or n / 2
*********
Problem 5
*********
Part A:
+--------------------------------+
|
unknown
|
+--------------------------------+
|
18
|
+--------------------------------+
|
213
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
unknown
|
+--------------------------------+
|
unknown
|

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:
-

An entry is left blank, although it should contain a value.


An entry is filled in, although it should remain blank.
An entry contains an incorrect value.
The stack is shifted up or down by four bytes. (If the stack
is shifted by more than four bytes, count the number of times
it is shifted.)

In general, 8 - ceil(m / 2) points should be awarded for this


subproblem. However, at the grader's discretion, one or two
extra points can be awarded for solutions that display some
knowledge of x86 stack conventions, or one or two extra points

can be deducted for solutions that betray only feeble knowledge


of x86 stack conventions.
Part B (2):
One point each for %esp and %ebp. The addresses must be
consistent with the values provided in the stack diagram.
*********
Problem 6
*********
Part A:
+----+----+----+----+----+----+----+----+
| a | XX | XX | XX | XX | XX | XX | XX |
+----+----+----+----+----+----+----+----+
| b | b | b | b | b | b | b | b |
+----+----+----+----+----+----+----+----+
| c | c | XX | XX | XX | XX | XX | XX |
+----+----+----+----+----+----+----+----+
|d[0]|d[0]|d[0]|d[0]|d[0]|d[0]|d[0]|d[0]|
+----+----+----+----+----+----+----+----+
|d[1]|d[1]|d[1]|d[1]|d[1]|d[1]|d[1]|d[1]|
+----+----+----+----+----+----+----+----+
|e[0]|e[1]|e[2]| XX | f | f | f | f |end
+----+----+----+----+----+----+----+----+
Part B:
(gdb) disassemble foo
Dump of assembler code for function foo:
0x00000000004004e4 <+0>:
sub
$0x8,%rsp
0x00000000004004e8 <+4>:
movb
$0x65,(%rdi)
0x00000000004004eb <+7>:
movq
$0x0,0x18(%rdi)
0x00000000004004f3 <+15>:
movw
$0x213,0x10(%rdi)
0x00000000004004f9 <+21>:
movzbl 0x29(%rdi),%ecx
0x00000000004004fd <+25>:
lea
0x2c(%rdi),%rdx
0x0000000000400501 <+29>:
mov
0x8(%rdi),%rsi
0x0000000000400505 <+33>:
mov
$0x40062c,%edi
0x000000000040050a <+38>:
mov
$0x0,%eax
0x000000000040050f <+43>:
callq 0x4003e0 <printf@plt>
0x0000000000400514 <+48>:
add
$0x8,%rsp
0x0000000000400518 <+52>:
retq
End of assembler dump.
Part C:
Most compact ordering is 40 bytes. for example: b, d, f, e, a, c
*********
Problem 7
*********
int test(int x, int y, int z)
{
int result = 3;
switch(z)
{
case 0:
x = x & 25;
case 3:
case 7:

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

of accesses will result in M H M M M M, hit rate = 0.16


of accesses will result in M H H M M M, hit rate = 0.33
of accesses will result in M H H H M M, hit rate = 0.5
(B)

*********
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.

Exam 1 Version 1 Solutions CS 213 Spring 2011


*********
Problem 1
*********
1. c
2. b
3. d
4. b
5. c
6. b
7. d
8. c
9. b
10. a
11. a
12. T
*********
Problem 2
*********
A.

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.

Note: The correct alignment of 16-byte types on 64-bit Linux is 16 bytes.


Since the cheat sheet we handed out gave incorrect information, we accepted
solutions assuming 8-byte alignment also.
aaaXbbbbbbXXXXXX
ccccccccXXXXXXXX
dddddddddddddddd
eeeeeeeeffffXXXX

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

Miss rate = 1/8


C.

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.

Exam 1 Solutions 15-213 / 18-243 Fall 2010


*********
Problem 1
*********
1-b 2-a 3-c 4-b 5-a 6-d 7-b 8-a 9-a 10-a
11-b 12-a
13-IA32: 1, 4, 4, 4 x86-64: 1, 4, 8, 8
14-a 15-a 16-a 17-a 18-a 19-elided (ambiguous) 20-a
*********
Problem 2
*********
Value
FP bits Rounded value
1
011 00
1
easy normalized, exact (1 pt)
12
110 10
12
normalized exact (1 pt)
11
110 10
12
round up to 12 (because 10 is odd) (1 pt)
1/8
000 10
1/8
denorm, exact (2 pt)
7/32
001 00
1/4
straddles norm/denorm boundary, round up
*********
Problem 3
*********
Version A: H = 15 J = 3
Version B: H = 5 J = 7
*********
Problem 4
*********
Part A:
abccddddeeeefXXX
gggg
Part B:
There are multiple correct answers. In general, put
the largest data items first. E.g.,
ddddeeeeffffccab
fXXX
*********
Problem 5
*********
A. Clyde
B. Inky
C. Pinky
D. Blinky
*********

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.

Solution Key to 15213-s10 Exam 1


Problem
Part 1:
Part 2:
Part 3:
Part 4:
Part 5:
Part 6:
Part 7:

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]);
}
}

Exam 1, Version 1 Solutions


15-213/18-243 Fall 2009
*********
Problem 1
*********
Part A.
0
Nan
2^-149
Part B.
False
True
True
True (!ux is a signed int)
*********
Problem 2
*********
A.
aaaaaaaaaxb1b2b3
ccccdxxxeeeexxxx
ffffffffggxxxxxx<-end
B.
ffffffffcccceeee
b1b2b3ggaaaaaaaa
adxxxxxx<-end
C. 14
D. 6
*********
Problem 3
*********
Format A
Bits
0111 0000

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

32-bit has les registers than 64-bit mode, so local variables


would need to be stored on the stack. Stack accessing takes
extra instructions and extra time.
e)
int mystery (int * array, size_t size, int e){
int a;
int max = size;
int min = 0;
while (max - min > 0) {
a = (max - min) / 2 + 1;
if (array[a] < e) {
max = a;
} else if (array[a] < e) {
min = a;
} else {
return a
}
}
return -1;
}
f) Binary Search
*********
Problem 7
*********
1.
2.
3.
4.
5.
6.
7.
8.
9.

b
c
c
b
b
a
a
b
d

*********
Problem 8
*********
H = 28
J = 15

Exam 1 Solutions CS 213 Spring 2009


*********
Problem 1
*********
Description
Bias
Smallest Positive positive
Lowest finite
Smallest positive normalized

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

Exam 1, Version 1 CS 213 Fall 2008

*********
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.

It yields i when i < 0, and ~i when i >= 0


For 0 and TMin
For large values of i, e.g., TMax, the addition can overflow
Will use floating point arithmetic, which does not overflow
Converting i to floating point can cause rounding. E.g., for TMax

*********
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.

Here is one solution:

ccccccccaaaaeeee
gggghhhhbbdfxxxx
C. 12
D. 4

Exam 1, Version 1 CS 213 Spring 2008


*********
Problem 1
********
Decimal
27
-28
-21
3
43
-8
8
-2
-32

|
|
|
|
|
|
|
|
|
|

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.

0 110 1111 = 31/2 = 15.5

*********
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

Exam 1 Solutions CS 213 Fall 2006


*********
Problem 1
*********
Fals (TMax)
True
False (all)
False (Tmin)
True
True
False (MSB==1, e.g., 0x80000000)
False (0, TMin)
*********
Problem 2
*********
Value
FP bits
7/8
010 11
15/16
011 00
9
110 00
10
110 01

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.

How many bytes of space in this new WineNode are wasted? 4

Note: There is only one correct solution for Part E.


*********
Problem 6
*********
beta
delta
alpha
gamma
*********
Problem 7
*********
long fun(long a, long b, long s)
{
long result = 0;
switch (s) {
case 3:
case 7:
result = b-1;
break;
case 6:
b = b*a;
/* Fall through */
case 5:
result = a & b;
break;
case 1:
result = a+b;
break;
default:
result = a*2;
}
return result;
}

Exam 1 Solutions CS 213 Fall 2005


*********
Problem 1
*********
Expression | Decimal
| Binary
-----------------------------------|
-2
| 111 1110
-|
19
| 001 0011
x
|
-16
| 111 0000
uy
|
112
| 111 0000
x - uy
|
0
| 000 0000 C promotes signed x to unsigned
TMax+1
|
-64
| 100 0000
TMin-1
|
63
| 011 1111
-TMin
|
-64
| 100 0000
TMin+TMin |
0
| 000 0000
TMax+TMin | -1
| 111 1111

----------------------------------*********
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

15-213 Fall, 2004Exam 1 SOLUTIONS


1.
Intended Solution (Bias = +7)
Entries: -0, 15/1024, -17/1024, 13, -248, +infty
Solution for Bias = -7
Entries: -0, 240, -272, 212,992, -24,063,232, +infty
2.
A.
B.
C.
D.
E.
F.
3.
A.

1...1 for x < 0, 0...0 for x >= 0


-2 for x < 0, 0 for x >= 0
x >= 0
opt_abs(x) == abs(x) - 1
Compute return value as: comp-mask
0 and TMin

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.

A. In the original code, strlen(str) must be called on every


iteration due to potential side-effects. Moving it to line 3
reduces the number of calls from 'n' calls to a single call.
B. Yes. In this code, the length of str is never changed, therefore
strlen(str) will return the same value for each iteration of the
loop. Additionally, strlen(str) has no side-effects. These two
properties make it safe to move.
C. In the original code, each iteration of the loop has a
multiplication that depends on the single variable hash. By
unrolling the loop, each iteration can perform two multiplications
in parallel because they depend on separate variables. Note that in
modern processors, accurate branch prediction will make loop
overheads negligable in both cases (as the increment and jump
instructions can be performed in parallel to the ongoing
multiplication).
D. Yes. Many compilers have an option to perform loop unrolling.
In this case there are no potential side effects of a loop
iteration, and thus loop unrolling would be safe to perform.

E. Another optimization is replacing the * 32 with >> 5, a form of


strength reduction. Because the shift operation is faster than
multiply, this should improve performance.
5.
A.

|<---------------- buf ---------------->|


+----+----+----+----+----+----+----+----+----+----+----+----+----+
|
| 69 | 6c | 75 | 76 | 32 | 31 | 33 | 00 |
|
|
|
|
+----+----+----+----+----+----+----+----+----+----+----+----+----+
^
%ebp
B.

|<---------------- buf ---------------->|


+----+----+----+----+----+----+----+----+----+----+----+----+----+
|
| xx | xx | xx | xx | xx | xx | xx | xx | st | qr | op | mn |
+----+----+----+----+----+----+----+----+----+----+----+----+----+
|<-- Ret addr ----->|
|<---- arg -------->|
+----+----+----+----+----+----+----+----+----+----+----+----+----+
| e2 | 83 | 04 | 08 | xx | xx | xx | xx | 13 | 52 | 01 | 00 |
|
+----+----+----+----+----+----+----+----+----+----+----+----+----+
6.
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 24 25
26 27
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-+--+--+
| WID |
name
|//| address
|
balance
| |////////|
note
|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-+--+--+
^
|
domestic
B.
typedef struct {
short WID;
char name[5];
char domestic;
Location address;
double balance;
char *note;
} Compact;
C.
24
D.
typedef struct {
short WID;
char name[5];
char domestic;
double balance;
Location address;

char *note;
} Win_Compact;
E.

0x004e4143

7.
T, F, T, F, F, T, F

Exam 1 Solutions CS 213 Fall 2003


*********
Problem 1
*********
--------------------------------Expression
decimal
binary
--------------------------------Zero
|
0
| 00 0000
--|
-6
| 11 1010
--|
18
| 01 0010
ux
|
47
| 10 1111
y
|
-3
| 11 1101
x >> 1
|
-9
| 11 0111
TMax
|
31
| 01 1111
-TMin
|
-32
| 10 0000
TMin+TMin |
0
| 00 0000
*********
Problem 2
*********
Part I
A. [1,2): 2^7
B. [2,3): 2^6
Part II
A. Denormalized numbers
(a) E = -6
(b) M = 127/128
B. Normalized numbers
(a) E = -6
(b) E = +7
(c) M = 255/128
Part III
----------------------------------------------------------Description
| E |
M
|
V
|
Binary
----------------------------------------------------------Zero
| -6 |
0
|
0
| 0 0000 00000
----------------------------------------------------------Smallest positive
| -6 | 1/128
| 1/8192 | 0 0000 00001
----------------------------------------------------------Largest denorm
| -6 | 127/128 | 127/8192| 0 0000 11111
----------------------------------------------------------Smallest pos norm
| -6 |
1
| 128/8192| 0 0001 00000
-----------------------------------------------------------

*********
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

EXAM ! SOLUTIONS SPRING 03


Problem 1
=========
17. This answer was given at the very start of class as a reward
to those students who had showed up ontime.
Problem 2
=========
Expression
decimal
binary
hex
-------------------------------------------Zero
|
0
| 0000 0000 | 00
--|
-3
| 1111 1101 | fd
i
|
-11
| 1111 0101 | f5
i >> 4
|
-1
| 1111 1111 | ff
ui
|
245
| 1111 0101 | f5
s
|
-2
| 1111 1110 | fe
s ^ 7
|
-7
| 1111 1001 | f9
us
|
14
| 0000 1110 | 0e
TMax
|
127
| 0111 1111 | 7f
TMin
|
-128
| 1000 0000 | 80
Problem 3
=========
Description
Binary
M
E
V
---------------------------------------0 010 010
5/4
-1
5/8
2 3/8
0 100 010
5/4
1
5/2
-infinity
1 111 000
most neg norm 1 110 111
15/8 3
-15
smal pos dnrm 0 000 001
1/8
-2
1/32
Problem 4
=========
func1
Problem 5
=========
scooby ->
dooby ->
doo
->
scrappy->

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.

0xvwturspq (matching the pqrstuvw above)

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

Exam 1 Solutions CS 213 Fall 2002


Problem 1
*********
Expression
decimal
binary
----------------------------------Zero
|
0
| 0 0000
--|
-5
| 1 1011
--|
-14
| 1 0010
y
|
-9
| 1 0111
z
|
23
| 1 0111

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;

15-213 Exam 1 Spring 2002 Solutions


Question 1:
Expression
decimal
binary
----------------------------------Zero
|
0
| 00 0000
--|
-3
| 11 1101
--|
-14
| 11 0010
ux
|
51
| 11 0011
y
|
-3
| 11 1101
x >> 2
|
-4
| 11 1100
TMax
|
31
| 01 1111
----------------------------------Question 2:
Description
|
Binary
|
M
|
E
|
Value
-----------------------------------------------------------------Positive Zero
| 0 000 0000 |
0
|
-2
|
0.0
-------------------------------------------------------------------| 0 000 0101 |
5/16 |
-2
|
5/64
-----------------------------------------------------------------Largest Denormalized | 0 000 1111 | 15/16 |
-2
| 15/64
-----------------------------------------------------------------Smallest Normalized | 1 001 0000 |
8/8
|
-2
|
-1/4
-----------------------------------------------------------------Negative Two
| 1 100 0000 |
1
|
1
|
-2.0
-------------------------------------------------------------------| 1 110 1101 | 29/16 |
3
|
-14.5
-----------------------------------------------------------------Negative infinity
| 1 111 0000 |
--|
--- | -infty
-----------------------------------------------------------------Question 3:
A. 4
B.

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

B. What is the problem with using registers in this fashion?


Registers only offer a limited, static amount of space.
C. Why is the notion of a base pointer still required?
If the call stack gets too large, the data in these registers still must be
saved in a stack-ish area. Hence, the notion of a base pointer is
still required because we may need to save these registers out to a area

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

-------

save callee-save register


ecx = A
edx = size
eax = min = A[0]
ebx = i
cmp size, i

-- cmp min, A[i]


-- eax = min = A[i]
-- incr i
-- cmp size, i
-- restore callee-save reg.

pop %ebp
ret

Exam 1 Solutions CS 213 Fall 2001


*********
Problem 1
*********
Expression
decimal
binary
----------------------------------Zero
|
0
| 00 0000
--|
-6
| 11 1010
--|
18
| 01 0010
ux
|
47
| 10 1111
y
|
-3
| 11 1101
x >> 1
|
-9
| 11 0111
TMax
|
31
| 01 1111
-TMin
|
-32
| 10 0000
TMin+TMin |
0
| 00 0000
----------------------------------*********
Problem 2
*********
-----------------------------------------------------------------Description
|
Binary
|
M
|
E
|
Value
-----------------------------------------------------------------Minus Zero
| 1 000 0000 |
0
|
-2
|
-0.0
-------------------------------------------------------------------| 0 100 0101 | 21/16 |
1
|
21/8
-----------------------------------------------------------------Smallest Denormalized| 1 000 1111 | 15/16 |
-2
| -15/64
-----------------------------------------------------------------Largest normalized
| 0 110 1111 | 31/16 |
3
|
31/2
-----------------------------------------------------------------One
| 0 011 0000 |
1
|
0
|
1.0
-------------------------------------------------------------------| 0 101 0110 | 11/8
|
2
|
5.5
-----------------------------------------------------------------Positive infinity
| 0 111 0000 |
--|
--- | +\infty
-----------------------------------------------------------------*********
Problem 3
*********
fun8
*********
Problem 4
*********
fun6
*********

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

You might also like