prerequisite quiz solutions

Computer Organization and Design: The Hardware/Software Interface

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 [ This page left for π ] 3.141592653589793238462643383279502884197169399375105820974944
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 2) In this problem, you must design a finite state machine (FSM) to navigate a robot out of a
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 01/29/2008 for the course CS 152 taught by Professor Kubiatowicz during the Spring '04 term at University of California, Berkeley.

Page1 / 12

prerequisite quiz solutions - University of California,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online