prerequisite quiz solutions

# Computer Organization and Design: The Hardware/Software Interface

This preview shows pages 1–5. Sign up to view the full content.

1 University of California, Berkeley College of Engineering Computer Science Division EECS Fall 2001 John Kubiatowicz Prerequisite Quiz September 7, 2001 CS152 Computer Architecture and Engineering This prerequisite quiz will be used in determining class admissions. The use of notes is not allowed during this quiz. Good Luck! Your Name: Solution SID Number: Discussion Section: 1 25 2 30 3 20 4 25 Total 100

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3 1) One way to calculate the square of a number x to add all the odd numbers from 1 to 2 x -1. So x² = (2x-1) + (2x-3) + ... + 3 + 1. For example, 4² = 7 + 5 + 3 + 1 = 16. Here is a recursive procedure to calculate the square of a number in C: Corrections below in blue. int square (int x) { if (x == 0) { return 0; } else if (x < 0) { return square (-x); } else { return ((2*x – 1) + square (x - 1)); } } Here is a translation of the above program into MIPS assembly language (assume no branch delay slot): square: addi \$v0, \$zero, 0 ; if a0 = 0, return 0 beq \$a0, \$zero, exit ; Don’t really need recursive call for second clause! slt \$t0, \$a0, \$zero beq \$t0, \$zero, notless ; if a0 < zero jump ahead sub \$a0, \$zero, \$a0 notless: addi \$sp, \$sp, -8 sw \$s0, 4(\$sp) sw \$ra, 8(\$sp) ; store \$ra on stack, do NOT ; use offset 0 off of \$sp ; by MIPS convention add \$t0, \$a0, \$a0 addi \$s0, \$t0, -1 addi \$a0, \$a0, -1 jal square add \$v0, \$v0, \$s0 lw \$s0, 4(\$sp) lw \$s0, 4(\$sp) ; restore s0, ra from stack lw \$ra, 8(\$sp) addi \$sp, \$sp, -8 exit: jr \$ra ; don’t touch sp if a0 = 0

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 2) In this problem, you must design a finite state machine (FSM) to navigate a robot out of a maze. Assume that the robot is placed into the maze as shown in the Figure below: Suppose that this robot can move forward one square, or turn right/left 90 degrees.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern