CSCI206 - Computer Organization & Programming

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Recursion in MIPS Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Leaf and Non-Leaf Procedures A leaf procedure is one that.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Solution 2nd Exam.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Calling Convention Chapter 2.7 Appendix A.6.
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Assembly Code Example Selection Sort.
The University of Adelaide, School of Computer Science
Computer Architecture CSCE 350
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Procedure call frame: Hold values passed to a procedure as arguments
Lecture 8: MIPS Instruction Set
MIPS Assembly Language
Intro to Computer Architecture
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Memory/Storage Architecture Lab Computer Architecture MIPS Instruction Set Architecture ( Supporting Procedures )
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedures 2a MIPS code examples Making Use of a Stack.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
MIPS Calling Convention. Procedure Calls Procedure must work the same from any call Procedure uses regs that main was using We need a convention to –pass.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic4: Procedures José Nelson Amaral.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
CSCI206 - Computer Organization & Programming
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
MIPS Assembly Language Programming
Prof. Hsien-Hsin Sean Lee
Lecture 6: Assembly Programs
Computer Architecture Instruction Set Architecture
CSCI206 - Computer Organization & Programming
Computer Architecture Instruction Set Architecture
CSCI206 - Computer Organization & Programming
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
What's wrong with this procedure?
CSCI206 - Computer Organization & Programming
Addressing in Jumps jump j Label go to Label op address 2 address
Solutions Chapter 2.
The University of Adelaide, School of Computer Science
Chapter 2 Instructions: Language of the Computer part 2
MIPS function continued
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Systems Architecture I
MIPS function continued
Computer Architecture
MIPS function continued
Presentation transcript:

CSCI206 - Computer Organization & Programming Procedures and Recursions zyBook: 8.2

Two’s complement multiplication When doing two’s complement multiplication, the signs will take care of themselves. The values (operands) must be fully “sign-extended” before operation (multiplication)! The result of two n-bits values will have 2*n bits. Thus sign extension must be 2*n bit long. We will discuss the topic in greater detail in Chapter 10

Examples Discussions and examples are from (2/23/2017) http://pages.cs.wisc.edu/~smoler/cs354/beyond354/int.mult.html A 4-bit, 2’s complement example: (-1) * (-7) == 7, extending to 8 bits 1111 1111 (-1) X) 1111 1001 (-7) ------------------------------------ 11111111 00000000 +) 11111111 ----------------------------------- 1111100000000111 ---> 7 Computer will extend the sign automatically to the full bits 1111100000000111 becomes 0000000000000111

Examples A 4-bit 2’s complement example 3*(-5) == -15, extending to 8 bits 0000 0011 (3) X) 1111 1011 (-5) ---------------------- 00000011 00000000 +) 00000011 0000001011110001 ---> -15 Computer will extend the sign automatically to the full bits 0000001011110001 becomes 1111111111110001 Run the program mult.s in MARS

Factorial int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)); } To implement factorial, we need to use the mult instruction in MIPS.

Factorial (1) Stack $ra One call to fact $a0 $sp fact: # PC == 0x0040001c addi $sp, $sp, -8 # create space for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # $t0 = 1 if n < 1 beq $t0, $zero, Else # if n>=1, go to Else addi $v0, $zero, 1 # return 1 addi $sp, $sp, 8 # pop 2 words off the stack jr $ra # return to caller Else: # PC == 0x0040003c addi $a0, $a0, -1 # n>=1: n = n-1 jal fact # fact(n=n-1) PC == 0x00400040 lw $a0, 0($sp) # restore save argument n lw $ra, 4($sp) # restore return address mul $v0, $a0, $v0 # $v0 = n * fact(n-1) int fact (int n) { if (n < 1) return (1); else return (n * fact(n-1)); }

Factorial (2) local / automatic variable, located on the stack 1 2 3 4 5 6 7 8 9 10 11 int fact (int n) { int result; if (n < 1) result = 1; else result = n * fact(n-1); return result; } local / automatic variable, located on the stack single return, value from stack

Factorial (2) fact: addi $sp, $sp, -12 # create space for 3 items sw $ra, 8($sp) # save return address sw $a0, 4($sp) # save argument slti $t0, $a0, 1 # $t0 = 1 if n < 1 beq $t0, $zero, Else # if n>=1, go to Else li $t0, 1 # setup for base-case sw $t0, 0($sp) # store 1 in result j fact_return Else: addi $a0, $a0, -1 # n>=1: n = n-1 jal fact # fact(n=n-1) PC = 0x0x01FC lw $a0, 4($sp) # restore saved argument n mul $t0, $a0, $v0 # $v0 = n * fact(n-1) sw $t0, 0($sp) # store result on stack #fall through to return fact_return: lw $ra, 8($sp) # restore return address lw $v0, 0($sp) # get result addi $sp, $sp, 12 # pop 3 words off the stack jr $ra # return to caller Factorial (2) 1 2 3 4 5 6 7 8 9 10 11 int fact (int n) { int result; if (n < 1) result = 1; else result = n * fact(n-1); return result; }

fact: addi $sp, $sp, -8 # create space for 3 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # $t0 = 1 if n < 1 beq $t0, $zero, Else # if n>=1, go to Else li $v0, 1 # setup for base-case j fact_return Else: addi $a0, $a0, -1 # n>=1: n = n-1 jal fact # fact(n=n-1) lw $a0, 0($sp) # restore saved argument n mul $v0, $a0, $v0 # $v0 = n * fact(n-1) #fall through to return fact_return: lw $ra, 4($sp) # restore return address addi $sp, $sp, 8 # pop 2 words off the stack jr $ra # return to caller Factorial (2b) 1 2 3 4 5 6 7 8 9 10 11 int fact (int n) { int result; if (n < 1) result = 1; else result = n * fact(n-1); return result; } Because result is assigned after all other function calls, we can use a register to hold this local variable. This is an optimization!

Factorial (3) Delayed stack setup is ok! blt $a0, 13, check_facts # check n li $v0, 0 jr $ra # if n > 12, return immediately check_facts: sll $t0, $a0, 2 # byte offset of n lw $v0, facts($t0)# load facts[n] bgtz $v0, fact_return # branch if lookup worked compute_fact: slti $t0, $a0, 1 # $t0 = 1 if n < 1 beq $t0, $zero, L1 # if n>=1, go to L1 li $v0, 1 # setup for base-case j fact_return L1: addi $sp, $sp, -8 # create space for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument addi $a0, $a0, -1 # n>=1: n = n-1 jal fact # fact(n=n-1) PC = 0x0x01FC lw $a0, 0($sp) # restore saved argument n lw $ra, 4($sp) # restore return address mul $v0, $a0, $v0 # $v0 = n * fact(n-1) addi $sp, $sp, 8 # pop 2 words off the stack #fall through to return fact_return: sll $t0, $a0, 2 # byte offset sw $v0, facts($t0)# save result in facts[n] jr $ra # return to caller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int facts[12] = {0}; int fact (int n) { if (n > 12){ return 0; } if (facts[n] == 0){ if (n < 1){ facts[n] = 1; } else { facts[n] = n * fact(n-1); return facts[n]; Delayed stack setup is ok! Use look-up table for previously computed results!