Exam 1 Version 1 Solutions
CS 213 Spring 2011
Copyright By PowCoder代写 加微信 powcoder
67 = 0100 0011
-35 = 1101 1101
* 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;
x = 0, y = 1
Bits Value Bits Value
011 000 1 101 010 5
111 000 17 111 010 NaN
110 001 9 000 011 3/32
110 010 9 1/2 110 000 8 1/2
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
dddddddddddddddd
cccccccceeeeeeee
ffffbbbbbbaaaXXX
int lolwut(char *s)
for(i = 0; s[i] != '\0'; i++)
if(s[i] < '0' || s[i] > ‘9’)
return -1;
n = n*10 + s[i] – ‘0’;
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
0xffff000c
———–
0x1000 | d | <- Start argument build area here
| c |
| b |
| a |
| ret addr|
ebp -> | %ebp |
| %ebx |
| |
| |
| |
esp -> | |
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
m h h h h h h h
m h h h h h h h
m h h h h h h h
m h h h h h h h
Miss rate = 1/8
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.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com