COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

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

Control Instructions

ECE260: Fundamentals of Computer Engineering

Lecture 5: Procedure Calls

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

ECE232: 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

Course Administration

ECE331: Hardware Organization and Design

Lecture 5: Procedure Calls

Chapter 2A Instructions: Language of the Computer

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

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

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

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

Instruction Set Architectures (4)

CENG3420 Lecture 03 Review

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

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

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

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

CSE Lecture In Class Example Handout

Thomas Polzer Institut für Technische Informatik

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

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CS 316: Procedure Calls/Pipelining

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

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

Functions in MIPS. Functions in MIPS 1

Instruction Set Architecture

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

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

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

Lecture 6: Assembly Programs

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

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

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

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

MIPS function continued

MIPS Procedure Calls. Lecture 6 CS301

COMPUTER ORGANIZATION AND DESIGN

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

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

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

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

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

MIPS Instruction Set Architecture (2)

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

EE 361 University of Hawaii Fall

Computer Architecture

COE608: Computer Organization and Architecture

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

Instructions: Assembly Language

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

Today. Putting it all together

Machine Instructions - II. Hwansoo Han

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

CS/COE1541: Introduction to Computer Architecture

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

COMP2611: Computer Organization MIPS function and recursion

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

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

CS3350B Computer Architecture MIPS Procedures and Compilation

CA Compiler Construction

Chapter 2. Instructions: Language of the Computer

Lecture 7: Procedures

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

MIPS Assembly (Functions)

MIPS Functions and Instruction Formats

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

CSE Lecture In Class Example Handout

CS64 Week 5 Lecture 1. Kyle Dewey

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

Computer Organization and Design

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

Reduced Instruction Set Computer (RISC)

CS 61c: Great Ideas in Computer Architecture

Functions and Procedures

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

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Procedure Call and Return Procedure call

Compiling Code, Procedures and Stacks

Lecture 7: Procedures and Program Execution Preview

Computer Organization MIPS ISA

Code Generation. Lecture 19

CENG3420 L03: Instruction Set Architecture

ECE369. Chapter 2 ECE369

CS61C : Machine Structures

Run-time Environment

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

Implementing Procedure Calls

Reduced Instruction Set Computer (RISC)

ECE 30 Introduction to Computer Engineering

Storage in Programs. largest. address. address

ECE331: Hardware Organization and Design

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Transcription:

COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1

Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access Transfer control to the procedure Acquire the storage resources needed for the procedure Perform the desired task Place the result value in a place where the calling program can access Return control to the point of origin, since a procedure can be called from several points in a program Comp 303 Computer Architecture 2

Register usage conventions $a0-$a3: four argument registers in which to pass parameters $v0-$v1: two value registers in which to return values $ra: one return address register to return to the point of origin The jump-and-link instruction (jal): jumps to an address and simultaneously saves the address of the following instruction in register $ra jal ProcedureAddress Comp 303 Computer Architecture 3

Program counter We need a register to hold the address of the current instruction being executed Program Counter (due to historical reasons) PC in MIPS jal saves PC+4 in register $ra At the end of the procedure we jump back to the $ra (an unconditional jump) jr $ra The caller puts the parameter values in $a0-$a3 The caller uses jal X to jump to procedure X The callee performs the calculations, places the results in $v0-$v1 Returns control to the caller by jr $ra Comp 303 Computer Architecture 4

Stack Suppose the procedure needs more than 4 arguments We store the values in Stack (a last-in-first-out queue) A stack needs a pointer to the most recently allocated address in the stack: stack pointer Placing data onto the stack is called a Push. Removing data from the stack is called a Pop. The stack pointer in MIPS is $sp. By convention stacks grow from higher addresses to lower addresses!!! (You push values onto the stack by subtracting from the stack pointer) Comp 303 Computer Architecture 5

Procedure call When making a procedure call, it is necessary to 1. Place inputs where the procedure can access them 2. Transfer control to procedure 3. Acquire the storage resources needed for the procedure 4. Perform the desired task 5. Place the result value(s) in a place where the calling program can access it 6. Return control to the point of origin MIPS Provides instructions to assist in procedure calls (jal) and returns (jr) Uses software conventions to place procedure input and output values control which registers are saved/restored by caller and callee Uses a software stack to save/restore values Comp 303 Computer Architecture 6

A procedure call with a stack int leaf-example (int g, int h, int i, int j) { int f; f = (g+h)-(i+j); return f; } Assume the parameter variables g, h, i, and j correspond to the argument registers $a0, $a1, $a2, and $a3, and f corresponds to $s0. leaf_example: sub $sp, $sp, 12 # adjust stack to make room for 3 items sw $t1, 8($sp) # save register $t1 for use afterwards sw $t0, 4($sp) # save register $t0 for use afterwards sw $s0, 0($sp) # save register $s0 for use afterwards add $t0, $a0, $a1 # register $t0 contains g + h add $t1, $a2, $a3 # register $t1 contains i + j sub $s0, $t0, $t1 # register $s0 contains (g + h) - (i + j) add $v0, $s0, $zero # register $v0 contains the result lw $s0, 0($sp) # restore register $s0 for caller lw $t0, 4($sp) # restore register $t0 for caller lw $t1, 8($sp) # restore register $t1 for caller add $sp, $sp, 12 # adjust stack to delete 3 items jr $ra # jump back to calling routine Comp 303 Computer Architecture 7

A procedure call with a stack (cont d) High address $sp $sp Content of reg. $t1 $sp Content of reg. $t0 Content of reg. $s0 Low address Before procedure call During procedure call After procedure call Comp 303 Computer Architecture 8

Some register conventions R0 R1 $zero $at R2 $v0 R3 $v1 R4 $a0 R5 $a1 R6 $a2 R7 $a3 R8 $t0 R9 $t1 R10 $t2 R11 $t3 R12 $t4 R13 $t5 R14 $t6 R15 $t7 Constant 0 Reserved for assembler Return Values Procedure arguments Caller saved temporaries: may be overwritten by called procedures R16 $s0 R17 $s1 R18 $s2 R19 $s3 R20 $s4 R21 $s5 R22 $s6 R23 $s7 R24 $t8 R25 $t9 R26 $k0 R27 $k1 Comp 303 Computer Architecture 9 R28 R29 $gp $sp R30 $s8 R31 $ra Callee saved temporaries: may not be overwritten by called procedures Caller save temp Reserved for operating system Global pointer Stack pointer Callee save temp Return address

Recursion (Nested procedure call) int fact (int n) { if (n < 1) return 1; else return (n * fact(n-1)); } Comp 303 Computer Architecture 10

Recursion fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save the return address sw $a0, 0($sp) # save the argument n slti $t0, $a0, 1 # test for n<1 beq $t0, $zero, L1 # if n>=1, goto L1 addi $v0, $zero, 1 # return 1 addi $sp, $sp, 8 # pop 2 items off stack jr $ra # return to after jal L1: addi $a0, $a0, -1 # n>=1: argument gets (n-1) jal fact # call fact with (n-1) lw $a0, 0($sp) # return from jal: restore argument n lw $ra, 4($sp) # restore the return address addi $sp, $sp, 8 # adjust stack pointer to pop 2 items mul $v0, $a0, $v0 # return n*fact(n-1) jr $ra Comp 303 Computer Architecture 11

Stack allocation in MIPS The stack is also used to store variables that are local to the procedure that do not fit in registers (local arrays or structures) The segment of the stack containing a procedure s saved registers and local variables is called a procedure frame or activation record. Some MIPS software use a frame pointer ($fp) to point to the first word of the frame of a procedure Comp 303 Computer Architecture 12

Stack allocation in MIPS High address $fp $fp $sp $fp Saved argument register (if any) $sp Saved return address Saved saved register (if any) $sp Local arrays and structures (if any) Low address Before procedure call During procedure call After procedure call Comp 303 Computer Architecture 13

Policy of use conventions Name Register number Usage Preserved on call? $zero 0 the constant value 0 n.a. $v0-$v1 2-3 values for results and expression evaluation no $a0-$a3 4-7 arguments yes $t0-$t7 8-15 temporaries no $s0-$s7 16-23 saved yes $t8-$t9 24-25 more temporaries no $gp 28 global pointer yes $sp 29 stack pointer yes $fp 30 frame pointer yes Register 1 ($at) reserved for assembler, 26-27 for operating system Comp 303 Computer Architecture 14

Global pointer C has two storage classes: automatic and static Automatic: variables that are local to a procedure and are discarded when the procedure exits Static: they exist across exits from and entries to procedures. C variables declared outside all procedures are considered static (or those declared with keyword static) To simplify access to static data MIPS uses global pointer or $gp Comp 303 Computer Architecture 15

MIPS addressing Register addressing where the operand is a register Comp 303 Computer Architecture 16

MIPS addressing Base or displacement addressing where the operand is at the memory location whose address is the sum of a register and a constant in the instruction Comp 303 Computer Architecture 17

MIPS addressing Immediate addressing where the operand is a constant within the instruction itself Comp 303 Computer Architecture 18

MIPS addressing PC-relative addressing where the address is the sum of the PC and a constant in the instruction Comp 303 Computer Architecture 19

MIPS addressing Pseudodirect addressing where the jump address is the 26 bits of the instruction concatenated with the upper bits of the PC Comp 303 Computer Architecture 20