EE 361 University of Hawaii Fall

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

Course Administration

CS/COE1541: Introduction to Computer Architecture

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

MIPS Functions and Instruction Formats

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

ECE 30 Introduction to Computer Engineering

More C functions and Big Picture [MIPSc Notes]

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

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

Control Instructions

Computer Architecture

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

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

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

Instructions: Assembly Language

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

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Course Administration

Lecture 5: Procedure Calls

Chapter 2A Instructions: Language of the Computer

CENG3420 Lecture 03 Review

Lecture 5: Procedure Calls

CS3350B Computer Architecture MIPS Introduction

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

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

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

MIPS%Assembly% E155%

CS 61c: Great Ideas in Computer Architecture

Lectures 3-4: MIPS instructions

CS3350B Computer Architecture

CS222: MIPS Instruction Set

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

Chapter 2: Instructions:

Instruction Set Architectures (4)

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

Instruction Set Architecture

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

Patterson PII. Solutions

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

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

Thomas Polzer Institut für Technische Informatik

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

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

Mips Code Examples Peter Rounce

Computer Architecture. The Language of the Machine

MIPS (SPIM) Assembler Syntax

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

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

CS 110 Computer Architecture MIPS Instruction Formats

COMPUTER ORGANIZATION AND DESIGN

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

MIPS Instruction Set

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

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Lecture 6: Assembly Programs

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

CSE Lecture In Class Example Handout

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

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

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

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

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

CSE Lecture In Class Example Handout

CSCE 5610: Computer Architecture

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Computer Organization MIPS ISA

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Compiling Code, Procedures and Stacks

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

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

PHBREAK: RISC ISP architecture the MIPS ISP. you read: text Chapter 3 CS 350

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Reduced Instruction Set Computer (RISC)

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

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CS3350B Computer Architecture MIPS Instruction Representation

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

Reduced Instruction Set Computer (RISC)

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Chapter 2. Instructions: Language of the Computer

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

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

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

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

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Functions in MIPS. Functions in MIPS 1

Instruction Set Architecture. "Speaking with the computer"

Computer Architecture. MIPS Instruction Set Architecture

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

EE 109 Unit 15 Subroutines and Stacks

Chapter 2. Instruction Set Architecture (ISA)

Chapter 3. Instructions:


Lecture 4: MIPS Instruction Set

CS 61c: Great Ideas in Computer Architecture

Transcription:

C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and MIPS machine language [MIPSb Notes] 2

main() n = max(x,y); C Functions Caller function/subroutine Subroutine call What do we need? 1. Jump to max 2. Store return address max(int i, int j) Callee function/subroutine Return from subroutine 3. Jump to return address 3 Implementation main() main: n = max(x,y); RetAddr: jal max $31 = RetAddr Jump to max max(int i, int j) max: jr $31 Jump to RetAddr 4

Implementation Input Parameters passed by value A single returned output value Input Parameters passed by value through designated registers A single returned output value by convention in $2 int max(int i, int j) Body of Code # i: $3 j: $4 max: Body of Code $2 = return value jr $31 5 main() n = max(x,y); Example main: # Calling procedure, where x: $20. y: $21. n: $22 move $3,$20 # Load input parameters move $4,$21 jal max # Call subroutine move $22,$2 # Load return value into n max(int i, int j) if (i < j) return j; else return i; # Input parameters i: $3. j: $4 max: slt $1,$3,$4 beq $0,$1,Skip move $2,$4 jr $31 Skip: move $2,$3 jr $31 6

jump-and-link (jal) jal Label $31 = return address J-Type jump to Label How does CPU compute return address? Return address can be found in program counter CPU $0, $1,..., $31 Program Counter (PC) Program Counter (PC) Register: 32-bits Holds address of next instruction to execute It is usually incremented after every instruction (incremented by 4) PC (at the time jal is being executed) jal Label 7 New Instructions: Immediate Addressing Remember: add, sub, slt There s also: addi $2,$3,1000 # $2 = $3 + 1000 (a constant) subi $2,$3,100 # $2 = $3-1000 [This is a pseudo instruction like mov] slti $2,$3,100 # $2 = 1 if $3 < 100 # 0 otherwise I-type instruction i means immediate addressing, which will be discussed a little later 8

Immediate Addressing Differences with old instructions add $2,$3,$4 addi $2,$3,4 R-Type I-Type 0 3 4 2 0 32 8 3 2 4 $4 is a register where operand is 4 is the operand located 9 Example main: main() k = nonneg(x); k += nonneg(y+5); move $3,$20 # $3 = x jal nonneg # call nonneg ret1: move $22,$2 # k = nonneg() addi $3,$21,5 # $3 = y+5 jal nonneg # call nonneg ret2: add $22,$22,$2 # k += nonneg() nonneg(int i) if (i < 0) return 0; else return i; nonneg: slt $1,$3,$0 # $1= 1 if i < 0 beq $1,$0,Else move $2,$0 # return 0 jr $31 Else move $2,$3 # return i jr $31 10

Temporary Storage function(x1, x2, x3,., x100) int array[1000]; double a; char name[1000];... The function needs memory cells for passing input parameters local variables The storage is temporary, needed only during the life of the function. Registers $1-$31 offer only a limited amount of storage. We can use main memory but can we organize it so that management of the resources is simple and efficient? Stacks 11 Stack: Stack A data structure that changes in size dynamically It has a top Push things in from the top Pull things out from the top Push Pop/Pull Stack 12

Motivation Example funct1( ) funct2( ); funct1( ) funct2( ); funct2( ) funct3( ); funct3( ) funct4( ); funct4( ) funct2( ) funct3( ); funct2 funct3 funct2 funct4 funct3 funct2 funct3( ) funct4( ); Stack Stack Stack Stack funct4( ) funct1( ) funct2( ); funct2( ) funct3( ); funct3( ) funct4( ); funct4( ) 13 Implementing Stacks Memory addresses $sp = Stack Pointer It can be most any register except special registers e.g., $0 and $31. By the MIPS convention $sp = $29 $sp Reading from top of stack: lw $2,0($sp) Writing to top of stack: sw $2,0($sp) 14

Implementing Push/Pop Push: Example of pushing $10 on stack add $sp,$sp,-4 # make space on top of stack for new value sw $10,0($sp) # store $10 on the stack Pop: Example of popping stack and putting value in $12 lw $12,0($sp) # load the top of stack into $12 add $sp,$sp,+4 # update stack pointer $sp 15 C statement n = function(x) Example Assembly language # n: $10. x: $11. # Pass parameters through $3 add $sp,$sp,-4 # Store old value of $3 away just in case sw $3,0($sp) # it s being used by something else move $3,$11 # Move x in $3 jal function move $10,$2 # n = function(x) lw $3,0($sp) # Restore old value of $3 add $sp,$sp,4 16

function1() Example function2(); function2() function3() function2: add sw jal lw add $sp,$sp,-4 $31,0($sp) function3 $31,0($sp) $sp,$sp,4 function3() 17 Good Housekeeping If you use registers, make sure that either it s not used by anything else OR if it s used, store it s contents away (say, into stack), and restore after you re done Stack housekeeping: keeping the stack balanced If you allocate (or push) space on the stack then remove all of it when you re done 18

Where do we store stacks? Memory Organization Program Variables and arrays Stack 19 Addressing Modes Instructions have two characteristics: operation: what it does, e.g., add, subtract, set-less-than, load word, store word addressing modes: how to get operands. The mode specifies where the operands are located Some of the types: Register Immediate Base or Displacement Program Counter (PC) Relative Direct Specifies register where operand is located Operand is in the instruction Example: lw and sw Example: beq and bne Address of operand is in the instruction Instruction size is limited to 32 bits. Addresses stored in instructions will be less than 32 bits, often 16 bits. However, addresses for word-operands are 32 bits. How do we get a 32 bit address from a smaller number of bits? Fill in the other bits. Sign extension 20

Addressing Modes Register Addressing: register (number) is specified add $2,$3,$4 0 3 4 2 0 32 Base or Displacement Addressing: lw $2,1000($3) location of operand is identified with a register and a constant (displacement) 35 3 2 1000 Thus, address of operand is computed by $3 + 1000 16 bits Expanded to 32 bits 21 Addressing Modes Immediate Addressing: operand is in the instruction Example: addi $2,$3,1000 For practical benchmarks, over half of the arithmetic instructions have constant operands Principle: Make the common fast. So make immediate addressing fast. Most constants are 16 bits, but sometimes you may get a BIG ONE. lui $2,1000 load-upper-immediate loads into register $3 the value 1000, but in the upper 16 bits $2 1000 I-Type Instruction 15 0 2 1000 22

Addressing Modes Loading a Big Constant: $3 7 5 lui $3,7 7 0 addi $3,$3,5 7 0 + 0 5 7 5 23 PC-Relative Addressing PC-Relative Addressing: address = PC + offset (specified in instruction) Example: bne $1,$2,Label What s stored in machine instruction is an offset Address Loop: instruction 0 instruction 4 instruction 8 beq $3,$4,Loop 12 beq rt rs offset instruction 16-4 PC + address offset = Loop address offset = Loop - PC = 0-16 = - 16 BUT notice address offsets are always multiples of 4, so the Actual Offset in the machine code is the address offset/4 24

Example # Clear array A[100] move $20,$0 addi $21,$0,100 Loop: beq $0,$21,Skip? sw $0,Astart($20) addi $21,$21,-1 beq $0,$0,Loop? Skip: 25 Offsets beq $2,$3,Label has a 16-bit offset 16-bit offset can be positive or negative represented using twos complement Twos Complement Brief Overview: Representation of signed integers with bits high order bit indicates sign 1 means negative 0 means positive Sign Extension To make a 16-bit twos complement number into a 32-bit twos complement number just add more bits to the left. add 1s if number is negative add 0s if number is positive Examples 10001 is negative 01101 is positive PC-Relative Addressing: 32-bit PC + 16-bit offset 26

Comparison of Jumping and Conditional Branching Jump and Jump-and-Link: J-type j 10000 2 10000 6 bits 26 bits Direct addressing Number is used directly as address (plus a type of extension) Conditional Branch: I-type bne $3,$21,Loop 5 8 21 offset (to Loop) 16 bits Range is better for J-type because there are more bits 16 bit offset is sufficient for most instances of conditional branch 27 What About Far Away Branches? beq $18,$19,Loop Compiler figures out that the offset is greater than 16 bits Skip: bne j $18,$19,Skip Loop 28

Review Stacks Addressing Computation of addresses and offsets Base or displacement addressing PC-Relative addressing How to compute offsets for MIPS MIPS instructions add, sub, slt, addi, subi, slti, lui, lw, sw, beq, bne, j, jr, jal, mul. 15 instructions out of 32 in the back cover of the text. Actually, there s just a few different types of instructions left. 29