Download Struct Node - Machine Structures - Exams and more Exams Structural Analysis in PDF only on Docsity!
October 17, 2001 David Patterson Your login name cs61c- __________________________________________________________________
0. Sign-in Question [-1 point if not followed]: Fill out the front page correctly, and
write your login at the top of each page.
1 Pointers and Struct [6pt][5min]
struct node{ int value; struct node *next; };
struct list{ char foo; char *ptr; struct node *listHead; int *bar; int size; };
struct list linklist;
1.1 [3pt] What is the correct C syntax to access the field value in struct node through the
variable linklist?
a) linklist.listHead.value
b) linklist->listHead->value
c) linklist->listHead.value
d) linklist.listHead->value
e) none of the above
____________________
1.2 [3pt] On a MIPS machine with 32 bit addressing, and every word in memory must be
aligned to 4 byte addressing, how many bytes does the compiler allocate for the variable
linklist?
a) 14
b) 16
c) 17
d) 20
e) 24
f) none of the above
____________________
October 17, 2001 David Patterson Your login name cs61c- __________________________________________________________________
2 Pointers and Arrays [15pt][10min]
Below is a segment of C code:
char *p, *q, y=0; x[8]={0,1,2,3,4,5,6,7}; int i=0;
p = x; q = &y; *q = ‘a’; for(i = 0; i < 6; i++){ *p++ = *q; } q = p;
Assume the address of x is 0x400 and the address of y is 0x4008.
2.1 [3pt] What is the value of x[1] at the end of the code?
____________________
2.2 [3pt] What is the value of (x+1) at the end of the code?
____________________
2.3 [3pt] What is the value of *q at the end of the code?
____________________
For 2.4 and 2.5, consider the following code and assume the address of variable array is
0x10000000.
int array[5]={-1,0,1,2,3};
int main(){ unsigned char *ptr1 = (((unsigned char *) array) + 3); unsigned char val; int *ptr2 = array + 3;
val = *ptr1;
return 0; }
2.4 [3pt] What address (in hex) does ptr2 point to at the end of the program?
____________________
2.5 [3pt] What value does val contain at the end of the program?
a) 0 b) 2 c) 3 d) 255 e) none of the above ____________________
October 17, 2001 David Patterson Your login name cs61c- __________________________________________________________________
4 Number Systems [12pt][12min]
Suppose we represent signed numbers P and Q in 8 bits, using two’s complement.
Further suppose that the value of P is 0xAF.
4.1 [3pt] What is the value of P in base 10?
_____________________
4.2 [3pt] What is the most negative possible value of Q such that the signed addition of P
and Q does not overflow?
_____________________
4.3 [3pt] What is the most positive possible value of Q such that the signed addition of P
and Q does not overflow?
_____________________
4.4 [3pt] Consider the following five floating point numbers.
A = 1.0000 x 2- B = 1.0000 x 2^70 C = 1.0000 x 2^70 D = 1.0000 x 2^100 E = 1.0000 x 2^40
Assume they are 32 bit single precision IEEE standard floating point numbers, and the
compiled version of the following C code will be run on a IEEE floating point compliant
floating point unit. Which one of the following places of parentheses will lead the result
of F = A + B * C / D – E = 1.0000 x 2-1?
a) (A+(B*C)/D)-E
b) (A+B*(C/D))-E
c) A+((B*C)/D-E)
d) A+(B*(C/D)-E)
e) all of the above because addition and multiplication are associative.
_____________________
October 17, 2001 David Patterson Your login name cs61c- __________________________________________________________________
5 C Logical Operations [6pt][5min]
Assume N is an unsigned 32-bit integer, and given the C expression:
N == ( ( ( (N << 8) >> 8) & (0xFF00) ) | 0xFF0000)
5.1 [3pt] What is the smallest number that makes the expression true?
(Please write the 32-bit pattern down, following the bit positions indicated below the
lines.)
____________ -- ____________ -- ____________ -- ____________
5.2 [3pt] How many different numbers make the expression true?
____________________
6 MIPS Instructions [21pt][15min]
6.1 [3pt] With TAL (True Assembly Language), which of the following instruction(s)
could be used to load number 0xABCD8765 into register $t0?
a) addi $t0, $0, 0xABCD
b) lui $t0, 0xABCD
addiu $t0, $t0, 0x
c) lui $t0, 0xABCD
addi $t0, $t0, 0x
d) lui $t0, 0xABCD
ori $t0, $t0, 0x
e) none of the above
____________________
6.2 [3pt] What is the result in register $t0 after these lines of MAL (MIPS Assembly
Language) are executed?
add $t0, $zero, $zero ori $t0, $t0, 0xC3C3C3C andi $t0, $t0, 0xBBBBBBBB ori $t0, $t0, 0x2A2A2A2A
a) 0x
b) 0x
c) 0xABABABAB
d) 0x2A2A2A2A
e) none of the above
____________________
October 17, 2001 David Patterson Your login name cs61c- __________________________________________________________________
7 MIPS Reverse Engineering [15pt][15min]
You have heard of hackers reverse engineer programs from binary. With the disassembler
that you have written, you can use it to generate MIPS code, which you can use to write
out readable C code. Try to convert the following MIPS function foo into C code.
1 foo: 2 bnex $a0, L 3 beqz $a1, exit 4 li $v0, 0 5 jr $ra 6 7 exit1: 8 li $v0, 1 9 jr $ra 10 11 L: 12 addiu $sp, $sp, - 13 sw $ra, 12($sp) 14 sw $a0, 8($sp) 15 sw $a1, 4($sp) 16 17 addiu $a0, $a0, - 18 jal foo 19 sw $v0, 0($sp) 20 lw $a0, 8($sp) 21 lw $a1, 4($sp) 22 addiu $a0, $a0, - 23 addiu $a1, $a1, - 24 jal foo 25 lw $t0, 0($sp) 26 addu $v0, $v0, $t 27 28 lw $ra, 12($sp) 29 addiu $sp, $sp, 16 30 jr $ra
int foo(int x, int y) {
if (x==0 && y==0) return 1; /* Please fill in the rest of the code below. */
October 17, 2001 David Patterson Your login name cs61c- __________________________________________________________________
8 Self Modifying Program [6pt][7min] (This is Hard!)
This is a hard question, and you may want to save this until the end of the exam. The
following code illustrates how a program can modify itself. For simplicity, there is only
one instruction that is modified by the program, and assume you run this code on SPIM.
1 __start: 2 addiu $t0, $0, 0 # $t0 = 0 3 addiu $t1, $0, 0 # $t1 = 0 4 addiu $t2, $0, 2 # $t2 = 2 5 loop: 6 addiu $t0, $t0, 1 # $t0 = $t0 + 1 7 beq $t0, $t2, next # if ($t0 == $t2) branch to next 8 jal loop 9 next: 10 addu $t1, $t1, $ra # $t1 += $ra 11 beq $t1, $0, exit # if ($t1 == 0) branch to exit 12 lw $t3, 0($t1) # $t3 = mem[$t1] 13 addiu $t3, $t3, 1 # $t3 += 1 14 sw $t3, 0($t1) # store word $t3 to memory 15 j next # jump to next 16 exit: 17 done # program finishes
8.1 [3pt] Please identify which line is modified by the program?
___________________
8.2 [3pt] Write out the new MIPS instruction in TAL of this line.
___________________
9 Trivia [0pt][0min]
What is the origin of the terms “big endian” and “little endian”? Hint: They were coined
in 1726 A.D.
Table provided were:
MIPS operands
MIPS assembly language
Figure A.10 MIPS registers and usage convention
MIPS machine language
MIPS instruction formats
(You can find these tables in the CS61C Patterson and Hennessy’s Computer
Organization and Design textbook.)