ECE331: Hardware Organization and Design

Similar documents
ECE232: Hardware Organization and Design

Lecture 5: Procedure Calls

Lecture 6: Assembly Programs

Lecture 5: Procedure Calls

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

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

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

Course Administration

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

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

ECE 30 Introduction to Computer Engineering

ECE260: Fundamentals of Computer Engineering

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

Chapter 2A Instructions: Language of the Computer

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

Lecture 5. Announcements: Today: Finish up functions in MIPS

ECE260: Fundamentals of Computer Engineering

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

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

COMPUTER ORGANIZATION AND DESIGN

Lecture 7: Examples, MARS, Arithmetic

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

Instruction Set Architectures (4)

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

Functions in MIPS. Functions in MIPS 1

Common Problems on Homework

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

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

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

CS3350B Computer Architecture

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Today. Putting it all together

ECE331: Hardware Organization and Design

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

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

Compiling Techniques

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CSE Lecture In Class Example Handout

Chapter 2. Instructions: Language of the Computer

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

Control Instructions

ECE331: Hardware Organization and Design

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

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

Thomas Polzer Institut für Technische Informatik

ECE260: Fundamentals of Computer Engineering

CENG3420 Lecture 03 Review

Machine Instructions - II. Hwansoo Han

Computer Architecture

CS 61C: Great Ideas in Computer Architecture Func%ons and Numbers

CS64 Week 5 Lecture 1. Kyle Dewey

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

EE 361 University of Hawaii Fall

Computer Hardware Engineering

Procedure Call Instructions

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

Procedure Call and Return Procedure call

Computer Organization and Design

MIPS Assembly (Functions)

MIPS Instruction Set Architecture (2)

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

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

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

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

CS3350B Computer Architecture MIPS Procedures and Compilation

MIPS function continued

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

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

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

MIPS Functions and Instruction Formats

Functions and Procedures

Chapter 2. Baback Izadi Division of Engineering Programs

Computer Organization MIPS ISA

CS3350B Computer Architecture MIPS Introduction

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

CS 61c: Great Ideas in Computer Architecture

We can emit stack-machine-style code for expressions via recursion

MIPS%Assembly% E155%

LAB C Translating Utility Classes

SPIM Procedure Calls

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

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

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

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

CS 316: Procedure Calls/Pipelining

ECE 15B Computer Organization Spring 2010

Levels of Programming. Registers

All instructions have 3 operands Operand order is fixed (destination first)

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

Compiling Code, Procedures and Stacks

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

COMP2611: Computer Organization MIPS function and recursion

Chapter 2. Instructions: Language of the Computer

MIPS Assembly (FuncDons)

Transcription:

ECE331: Hardware Organization and Design Lecture 8: Procedures (cont d), Binary Numbers and Adders Adapted from Computer Organization and Design, Patterson & Hennessy, UCB

Review: Procedure Calling Steps required 1. The calling program places parameters in registers 2. The calling program transfers control to the procedure (callee) 3. The called procedure acquire storage that it needs from memory 4. The called procedure executes its operations 5. The called procedure places results in registers for the calling program to retrieve. 6. The called procedure reverts the appropriate MIPS registers to their original or correct state. 7. The called procedure returns control to the the next word in memory from which it was called. 8. The calling program proceeds with its calculations ECE331: Adders 2

Procedure Call Instructions (concise) Procedure call: jump and link jal Procedure_Label Address of following instruction put in $ra Jumps to target address Procedure return: jump register jr $ra Copies $ra to program counter Can also be used for computed jumps e.g., for case/switch statements ECE331: Adders 3

Jump-and-Link (detail) A special register (storage not part of the register file) maintains the address of the instruction currently being executed this is the program counter (PC) The procedure call is executed by invoking the jump-and-link (jal) instruction the current PC (actually, PC+4) is saved in the register $ra and we jump to the procedure s address (the PC is accordingly set to this address) jal New_Procedure_Address Since jal may over-write a relevant value in $ra, it must be saved somewhere (in memory?) before invoking the jal instruction Control is returned back to the caller after completing the callee procedure by using the jump register command jr $ra ECE331: Adders 4

Storage Management on a Call/Return A called procedure must create space for all its variables on the stack Arguments are copied into $a0-$a3; the jal is executed After the called procedure (callee) creates stack space, it updates the value of $sp Once the called procedure finishes, it copies the return value into $v0, frees up stack space, and $sp is restored to its original value The responsibility for copies between stack and registers may fall upon either the calling procedure or the called procedure before during after ECE331: Adders 5

What values are saved? $sp 0x7fff fffc Stack Dynamic Data pc 0x0040 0000 0 Static Data Text Reserved ECE331: Adders 6

The Stack The register scratchpad for a procedure seems volatile It seems to disappear every time we switch procedures A procedure s values are therefore backed up in memory on a stack Proc A s values High address Proc A Proc B s values Proc C s values Stack grows this way Low address return call Proc B call Proc C return return ECE331: Adders 7

Leaf Procedure Example MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Return Grow the stack by 4 bytes Save $s0 on stack Procedure body Result Restore $s0 Shrink the stack by 4 bytes C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; } MIPs assumes that arguments g,, j are in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 ECE331: Adders 8

Non-Leaf Procedures Procedures that call other procedures For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call Restore from the stack after the call ECE331: Adders 9

Customs for function calls in MIPS Steps for the code that is calling a function: Caller (calling procedure) Before Entry: Step 1: Pass the arguments: The first four arguments (arg0-arg3) are passed in registers $a0-$a3 Remaining arguments are pushed onto the stack Step 2: Execute a jal instruction Jump and Link will save your return address in the $ra register, so that you can return to where you started After Return: Step 1: Get result from $v0, $v1 ECE331: Adders 10

Customs for function calls in MIPS Steps for the code that is called : Callee (called procedure) On Entry: Step 1: Save callee-saved registers on stack Save registers $s0-$s7 if they are used in the callee procedure Save $ra register Before Exit: Step 1: Save return values in $v0-$v1 registers Step 2: Restore $s0-$s7 registers, if they were saved Step 3: Restore $ra register Step 4: Execute jr $ra ECE331: Adders 11

Example string copy Copy the string stored in y into the array called x. Convert to assembly: void strcpy (char x[], char y[]) { int i; i=0; while ((x[i] = y[i])!= `\0 ) i += 1; } Base addresses for x and y in $a0 and $a1, respectively $s0 is used for i strcpy: addi $sp, $sp, -8 sw $s0, 0($sp) sw $ra, 4($sp) add $s0, $zero, $zero L1: add $t1, $s0, $a1 lb $t2, 0($t1) add $t3, $s0, $a0 sb $t2, 0($t3) beq $t2, $zero, L2 addi $s0, $s0, 1 j L1 L2: lw $s0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 jr $ra ECE331: Adders 12

New Topic: Computer Organization 5 classic components of any computer Computer Processor (CPU) Control Memory Devices Input Datapath Output In the upcoming week we will be looking at optimizing the datapath for addition and multiplication ECE331: Adders 13

Review: Unsigned Binary Integers Given an n-bit number x n-1 x 0 x = x + n 1 n 2 1 0 n 12 + xn 22 +! + x12 x02 Range: 0 to +2 n 1 Example 0000 0000 0000 0000 0000 0000 0000 1011 2 = 0 + + 1 2 3 + 0 2 2 +1 2 1 +1 2 0 = 0 + + 8 + 0 + 2 + 1 = 11 10 Using 32 bits 0 to +4,294,967,295 ECE331: Adders 14

Review: 2 s-complement Signed Integers Given an n-bit number x = x + n 1 n 2 1 0 n 12 + xn 22 +! + x12 x02 Bit n-1 is sign bit 1/0 for negative/non-negative numbers Range: 2 n 1 to +2 n 1 1 x n-1 Example 1111 1111 1111 1111 1111 1111 1111 1100 2 = 1 2 31 + 1 2 30 + + 1 2 2 +0 2 1 +0 2 0 = 2,147,483,648 + 2,147,483,644 = 4 10 Using 32 bits 2,147,483,648 to +2,147,483,647 Most-negative: 1000 0000 0000 Most-positive: 0111 1111 1111 x 0 2 s comp. binary decimal 1000-8 1001-7 1010-6 1011-5 1100-4 1101-3 1110-2 1111-1 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 ECE331: Adders 15

Review: Signed Negation To get X complement X and add 1 Complement means 1 0, 0 1 Example: negate +2 +2 = 0000 0000 0010 2 2 = 1111 1111 1101 2 + 1 = 1111 1111 1110 2 Subtraction: y x = y + ( x +1) Why this works x + x = 1111...1112 x + 1= x = 1 Sign Extension Representing a number using more bits Preserve the numeric value Replicate the sign bit to the left Examples: 8-bit to 16-bit +5: 0000 0101 => 0000 0000 0000 0101 5: 1111 1011 => 1111 1111 1111 1011 ECE331: Adders 16