








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Sciences Computer Science Division
CS 61c J. Wawrzynek Spring 2006
Your Name: Fect, Per
ID Number: 999-99-
Left Neighbor ID: Right Neighbor ID:
This is a open-book exam. You are allowed to use any books and notes that you wish. No calculators or electronic devices of any kind, please. You have 2 hours. Each question is marked with its number of points.
This exam booklet should have 11 printed pages, plus 4 blank pages at the end. Check to make sure that you have all the pages. Put your student ID neatly on each page.
Show your answers in the space provided for them. Write neatly and be well organized. If you need extra space to work out your answers, you may use the back of previous questions or the blank sheets attached to the back of your exam booklet. However, only the answers appearing in the proper answer space will be graded.
Good luck!
1
Which of these unsigned numbers is largest: FadedAbehex, DeadBeefhex or FeedFacehex [1 point]?
0xFeedFace
What is B0Dhex in decimal [1 point]?
What is (^337) ten in hexadecimal [1 point]?
0x
What is the decimal equivalent of the 6-bit two’s complement number (^101010) two [ point]?
Put a T (true) or F (false) in each table cell [^14 point each, total is rounded up ⇒ 4 points]:
sign & 1’s 2’s unsigned magnitude complement complement
Can represent positive numbers T T T T
Can represent negative numbers F T T T
Has more than one representation for 0 F T T F
Uses the same addition process as unsigned T F F T
F000000C loop: addi $1, $1, -1 # [ 8 | 1 | 1 | -1 ]
F0000010 beq $1, $0, done # [ 4 | 0 | 1 | 1 ]
F0000014 j loop # [ 2 | 3 ]
F0000018 done:
For each instruction in the program, write down the values (in decimal) of each field in the machine language version of that instruction using the following notation: [ value | value | ... ] (With this notation, the MIPS instruction add $3,$2,$1 instruction would be described as [ 0 | 2 | 1 | 3 | 0 | 32 ].
Put your answers to the right of the “#”s.
char* upcase(char* str) {
char* p; char* result;
result = (char*) malloc(1+strlen(str));
strcpy(result, str);
for( p=result; p!=’\0’; p++ ) { if (p >= ’a’ && *p <= ’z’) *p += ’A’ - ’a’;
return result;
}
Below is table for the ASCII character codes, that you might need for part b). The numbers along the left and top indicate the first and second hex digits of the codes, respectively.
6 ‘ a b c d e f g h i j k l m n o 7 p q r s t u v w x y z { | } ~ DEL
Write the corresponding MIPS assembly code below. You may not use any form of MUL. Your answer should use as few a number of instructions as possible [4 points].
foo: sll $v0, $a0, 8 # $a0 contains x add $v0, $v0, $a
jr $ra
Multiplication is more difficult when neither argument is known at compile time. The general procedure for achieving multiplication of two unsigned numbers is to use a series of shift and add operations (think about how long-hand multiplication works). The following assembly code multiplies two unsigned numbers, $a0 and $a1, leaving the result in $v0. Assume that the result is sufficiently small that it fits in a single register. Fill in the missing lines [6 points].
addi $v0, $zero, $zero # clear $v
loop: beq $a1, $zero, done # if $a1==0, we are done andi $t0, $a1, 1 # check bottom bit of $a1... beq $t0, $zero, skip # ...if it is 0, skip over
add $v0, $v0, $a0 # fill me in!
skip: srl $a1, $a1, 1 # shift $a1 to the right
sll $a0, $a0, 1 # fill me in! j loop # repeat done: jr $ra
MIPS assembly code translation:
####################################################################
#################################################################### BitCount: addi $sp, $sp, -8 # make stack space sw $ra, 4($sp) # save return address sw $s0, 0($sp) # save $s add $v0, $0, $0 # initialize $v0 to 0 beq $a0, $0, end # if (x==0) return andi $s0, $a0, 1 # bit = x & 0x srl $a0, $a0, 1 # x >> 1 jal BitCount # recursive call add $v0, $v0, $s end: lw $ra, 4($sp) # restore $ra lw $s0, 0($sp) # restore $s addi $sp, $sp, 8 # restore stack jr $ra
Scrap.
Scrap.
Scrap.