Implement a C language function that computes the factorial of a number, recursively, according to the following prototype:

Similar documents
McGill University Faculty of Engineering FINAL EXAMINATION Fall 2007 (DEC 2007)

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

MIPS function continued

Department of Electrical and Computer Engineering Introduction to Computer Engineering I (ECSE-221) Assignment 3: Sequential Logic

ECE260: Fundamentals of Computer Engineering

Lecture 5: Procedure Calls

ECE331: Hardware Organization and Design

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

ECE232: Hardware Organization and Design

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

2B 52 AB CA 3E A1 +29 A B C. CS120 Fall 2018 Final Prep and super secret quiz 9

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Lecture 14: Recap. Today s topics: Recap for mid-term No electronic devices in midterm

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

ICS 233 COMPUTER ARCHITECTURE. MIPS Processor Design Multicycle Implementation

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

Lecture 7: MIPS Functions Part 2. Nested Function Calls. Lecture 7: Character and String Operations. SPIM Syscalls. Recursive Functions

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

ECE 30 Introduction to Computer Engineering

Chapter 2A Instructions: Language of the Computer

Format. 10 multiple choice 8 points each. 1 short answer 20 points. Same basic principals as the midterm

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

MIPS Functions and Instruction Formats

Signature: 1. (10 points) Basic Microcontroller Concepts

S Computer Architecture. Computer architecture assignment E1 A subroutine, which searches for given byte pattern from an byte array.

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

We will study the MIPS assembly language as an exemplar of the concept.

King Fahd University of Petroleum and Minerals College of Computer Science and Engineering Computer Engineering Department

ECE550 PRACTICE Midterm

Lab 4 : MIPS Function Calls

Chapter 2. Instructions:

(a) Implement processor with the following instructions: addi, sw, lw, add, sub, and, andi, or, ori, nor, sll, srl, mul

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CSc 256 Midterm 1 Fall 2011

CSE Lecture In Class Example Handout

ECE 30 Introduction to Computer Engineering

MIPS Assembly (Functions)

SPIM Procedure Calls

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

CS/COE1541: Introduction to Computer Architecture

Review of Activation Frames. FP of caller Y X Return value A B C

Lecture 5: Procedure Calls

CS3350B Computer Architecture

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

COMP 303 Computer Architecture Lecture 6

CS 61c: Great Ideas in Computer Architecture

MIPS Procedure Calls. Lecture 6 CS301

CENG3420 Lecture 03 Review

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Procedure Call and Return Procedure call

MIPS Assembly Language. Today s Lecture

Winter 2006 FINAL EXAMINATION Auxiliary Gymnasium Tuesday, April 18 7:00pm to 10:00pm

CS 110 Computer Architecture MIPS Instruction Formats

4.2: MIPS function calls

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Implementing Procedure Calls

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

COMP2611: Computer Organization MIPS function and recursion

ECE260: Fundamentals of Computer Engineering

MIPS Assembly Language Programming

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Machine Instructions - II. Hwansoo Han

MIPS Assembly Language Programming

COE608: Computer Organization and Architecture

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

SPIM Instruction Set

CS 316: Procedure Calls/Pipelining

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Compiling Techniques

COMPUTER ORGANIZATION AND DESIGN

Homework 4 - Solutions (Floating point representation, Performance, Recursion and Stacks) Maximum points: 80 points

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

ECE331: Hardware Organization and Design

Chapter 2. Instruction Set. The MIPS Instruction Set. Arithmetic Operations. Instructions: Language of the Computer

Reduced Instruction Set Computer (RISC)

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

CS 61C: Great Ideas in Computer Architecture (Machine Structures) More MIPS Machine Language

Chapter 3. Instructions:

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Introduction to Assembler Language Programming

EE 361 University of Hawaii Fall

ECE260: Fundamentals of Computer Engineering

Transcription:

McGill University Department of Electrical and Computer Engineering ECSE-221 Introduction to Computer Engineering Assignment 5 Assembly and Datapaths Due Date: Monday, November 30 th 2009 at 11:59PM Question 1 Implement a C language function that computes the factorial of a number, recursively, according to the following prototype: void factorial (long argument, long *result); If you are unsure how to proceed, try Googling on the keywords recursive and factorial; the function is relatively straightforward to write. Demonstrate your function by writing an interactive test program that operates as follows: % factorial 6 6! = 720 Question 2 Translate your C function into MIPS assembly, passing arguments on the stack according to the following call convention (assume, below, that the argument is in $10, and that the data is to be returned to location data_return in the data segment): addi $sp, $sp, -8 sw $10, 0($sp) la $11, data_return sw $11, 4($sp) jal factorial addi $sp, $sp, 8 Using the SPIM console I/O functions, write a simple interactive program to query a value from the user and print the factorial of the value as output, e.g., Enter data > 6 6! = 120

Question 3 4 4 8 The figure shown above shows a datapath for a 4-bit x 4-bit unsigned binary multiplier adapted from the one shown in Patterson and Hennessy. The corresponding algorithm, taken literally from the text, is reproduced below. 0. Init. Place Multiplicand in Multiplicand register. Place Multiplier in lower half of Product register. Clear upper half of Product register 1. Test and Update. If the LSB of Product register is 1, add Multiplicand register to upper half of Product register and place result back in the upper half of Product register. 2. Shift. Product register shifted right one 1 bit. 3. Count. If 4th repetition then done else goto step 1.

Do the following: 1. Implement the datapath shown on the previous page in LogicWorks. The Multiplicand and Multiplier inputs are supplied using Hex keypads (i.e. there is no need for an explicit Multiplicand register). The Product register can be implemented using the Shift Reg-4 component and the ALU with the Adder-4. Since the multiplication is unsigned, you will need to take care of the carry out generated by the ALU. Logic will also have to be provided for initializing the Product register at the start of the sequence as well as latching the final product for display. Clearly identify your control lines. 2. A total of 9 clock pulses are necessary to compute the product from the 4-bit multiplier and multiplicand. You will need to synthesize a controller to supply the correct states for your control lines for each of the 9 clock pulses. Start by drawing the state diagram corresponding to this controller, and then produce the state transition table that will be used to generate the ROM program. Implement your controller and validate it with an appropriate LogicWorks simulation. 3. Combine your controller with the datapath and verify that the system functions correctly. Produce 4 examples tracing the clock and the outputs of the product register. 4. Assume the following parameters: Tsu (reg) = 0.1 ns, Th (reg) = 0 ns, Tpd (reg) = 1 ns, Ta (ROM) = 5 ns, Tpd (ALU) = 10 ns, Tpd (all gates) = 0.1 ns. Determine the maximum frequency of operation of the circuit and produce a simulation, to scale, that validates the maximum clock frequency.

Question 1 /*----------------------------------------------------------------------*/ /* Program Name: factorial */ /* Function: Computes the factorial of a command line */ /* argument. */ /* Usage: % factorial [long] */ /* Author: F.P. Ferrie */ /* Date: March 28, 2008 */ /*----------------------------------------------------------------------*/ #include <stdio.h> void factorial (long argument, long *result); int main (int argc, char *argv[]) { long argument, result; if (argc!= 2) { printf("usage: factorial [long]\n"); return(0); } sscanf(argv[1],"%ld",&argument); factorial(argument,&result); printf("%ld! = %ld\n",argument,result); return(0); } /*----------------------------------------------------------------------*/ /* Recursive factorial function */ /* void factorial (long argument, long *result) */ /*----------------------------------------------------------------------*/ void factorial (long argument, long *result) { long temp; if (argument < 2) *result=1; else { factorial(argument-1,result); *result=argument*(*result); } } C:\Users\ferrie\221\lcc\lcc>factorial 0 0! = 1 C:\Users\ferrie\221\lcc\lcc>factorial 1 1! = 1 C:\Users\ferrie\221\lcc\lcc>factorial 2 2! = 2 C:\Users\ferrie\221\lcc\lcc>factorial 3 3! = 6 C:\Users\ferrie\221\lcc\lcc>factorial 4 4! = 24 C:\Users\ferrie\221\lcc\lcc>factorial 5 5! = 120 C:\Users\ferrie\221\lcc\lcc>factorial 6 6! = 720 C:\Users\ferrie\221\lcc\lcc>factorial 7 7! = 5040

Question 2 # Program Name: factorial # Function: Computes the factorial of a command line # argument. # Usage: % factorial [long] # Author: F.P. Ferrie # Date: March 28, 2008.text # Factorial: computes factorial of a number. Arguments passed # on stack according to the convention below. ##SP+4: return (reference) # SP argument (value) factorial: # Context save addi $sp, $sp, -20 sw $10, 0($sp) # Argument sw $11, 4($sp) # Return pointer sw $12, 8($sp) # Scratch sw $fp, 12($sp) # Frame pointer sw $31, 16($sp) # Linkage register # Argument fetch addi $fp, $sp, 20 # Set frame pointer lw $10, 0($fp) # Get factorial argument lw $11, 4($fp) # Get pointer to return # Termination condition for recursion slti $12, $10, 2 # Argument < 2? beq $12, $0, fl10 # No, do recursion li $12, 1 # Yes, set return value sw $12, 0($11) # Store at pointer location b fl20 # To restore context fl10: addi $sp, $sp, -8 # Set up stack for call addi $10, $10, -1 # Set arg to arg-1 sw $10, 0($sp) # Argument on stack sw $11, 4($sp) # Return on stack jal factorial # Execute call addi $sp, $sp, 8 # Restore sp addi $10, $10, 1 # Restore argument for next step lw $12, 0($11) # Get (n-1)! mul $12, $10, $12 # Compute argument x result sw $12, 0($11) # Return n x (n-1)! fl20: lw $10, 0($sp) # Restore context lw $11, 4($sp) lw $12, 8($sp) lw $fp, 12($sp) lw $31, 16($sp) addi $sp, $sp, 20

jr $31 # Return # Main program.globl main main: la $4, prompt # Do prompt - Enter value > li $2, 4 syscall li $2, 5 # Get number from console syscall addi $sp, $sp, -8 # Set up function call sw $2, 0($sp) la $4, return sw $4, 4($sp) jal factorial # Do it addi $sp, $sp, 8 move $4, $2 # Print result li $2, 1 syscall la $4, excl li $2, 4 syscall la $4, return lw $4, 0($4) li $2, 1 syscall la $4, crlf li $2, 4 syscall b main # Data Segment.data prompt:.asciiz "Enter value > " excl:.asciiz "! = " crlf:.asciiz "\n".align 2 return:.word 0 7/36 Console ========================= Enter value > 0 0! = 1 Enter value > 1 1! = 1 Enter value > 2 2! = 2 Enter value > 3 3! = 6 Enter value > 4 4! = 24 Enter value > 5 5! = 120 Enter value > 6 6! = 720 Enter value > 7

7! = 5040 Enter value > 8 8! = 40320 Enter value > 9 9! = 362880 Enter value > 10 10! = 3628800 Enter value >

Question 3 a) Datapath implementation using parts from the standard LogicWorks library. There are 2 datapath control signals, Init and Ld, both of which are active 0. When Init is asserted (i.e. 0), the upper product register is cleared and the lower product register is loaded with the multiplier (lower hex keypad). Init is asserted on the first clock pulse, and de-asserted (1) for the remainder of the cycle. The Ld signal determines whether the product register shifts or loads. Ld is asserted on the first clock pulse (initialization), and then at each ALU operation (pulses 0, 1, 3, 5, 7). An additional D- FF is needed to save the carry out so that it can be shifted in to the product register on the subsequent clock pulse. By connecting P0 to the select line of the multiplicand register (i.e. the hex keypad), adding 0 or adding the multiplicand is handled automatically. Finally, we observe that the Init signal marks

both the end of the current cycle and the start of a new one. Hence the Init signal is used to load the two display registers which latch the result from the product register. b) Finite State Machine

State Transition Table Implementation:

b) Multiplier Implementation

d) Timing Analysis and Demonstration Tclock = Tsu(max) + Tpd(max) + Tcl = 0.1 ns + 1 ns + Tcl Tcl = Tpd(Add) + Tpd(AND) = 10.1 ns Tclock = 11.2 ns Fmax = 89.29 Mhz Simulation: Scale by 10. Show correct function with Tclock = 112 units, and incorrect with 111. Correct answer F x F = E1 with Tclock = 112 units

Incorrect answer F X F = C5 with Tclock = 111 units