Sample Questions
Sample Questions
2. a
C. 1 + (a << 3) + ˜a
I. a >> 2
Page 1 of 6
Problem 2. (15 points):
Assume we are running code on a 6-bit machine using two’s complement arithmetic for signed integers. A
“short” integer is encoded using 3 bits. Fill in the empty boxes in the table below. The following definitions
are used in the table:
short sy = -3;
int y = sy;
int x = -17;
unsigned ux = x;
Zero 0
– −6
– 01 0010
ux
x >> 1
TMax
−TMin
TMin + TMin
Page 2 of 6
Problem 3. (15 points):
We are running programs on a machine with the following characteristics:
• Values of type int are 32 bits. They are represented in two’s complement, and they are right shifted
arithmetically. Values of type unsigned are 32 bits.
• Values of type float are represented using the 32-bit IEEE floating point format, while values of
type double use the 64-bit IEEE floating point format.
We generate arbitrary values x, y, and z, and convert them to other forms as follows:
For each of the following C expressions, you are to indicate whether or not the expression always yields 1.
If so, circle “Y”. If not, circle “N”.
Page 3 of 6
Problem 4. (15 points):
Consider the source code below, where M and N are constants declared with #define.
int mat1[M][N];
int mat2[N][M];
sum_element:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
movl 12(%ebp),%ecx
sall $2,%ecx
leal 0(,%eax,8),%edx
subl %eax,%edx
leal (%eax,%eax,4),%eax
movl mat2(%ecx,%eax,4),%eax
addl mat1(%ecx,%edx,4),%eax
movl %ebp,%esp
popl %ebp
ret
N=
Page 4 of 6
Problem 5. (20 points):
Condider the following assembly code for a C for loop:
loop:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%ecx
movl 12(%ebp),%edx
xorl %eax,%eax
cmpl %edx,%ecx
jle .L4
.L6:
decl %ecx
incl %edx
incl %eax
cmpl %edx,%ecx
jg .L6
.L4:
incl %eax
movl %ebp,%esp
popl %ebp
ret
Based on the assembly code above, fill in the blanks below in its corresponding C source code. (Note: you
may only use the symbolic variables x, y, and result in your expressions below — do not use register
names.)
__________;
__________;
}
__________;
return result;
}
Page 5 of 6
Problem 6. (20 points):
Write a bash script taking a directory as an argument and producing the list of the files in the directory sorted
by the length of the file name.
Page 6 of 6