CS64 Week 5 Lecture 1. Kyle Dewey

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

Functions and Procedures

CSE Lecture In Class Example Handout

ECE232: Hardware Organization and Design

Functions in MIPS. Functions in MIPS 1

SPIM Procedure Calls

Course Administration

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

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

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

Today. Putting it all together

MIPS Procedure Calls. Lecture 6 CS301

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

ECE 30 Introduction to Computer Engineering

Lecture 5: Procedure Calls

ECE331: Hardware Organization and Design

6.004 Tutorial Problems L3 Procedures and Stacks

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

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

COMP2611: Computer Organization MIPS function and recursion

COMP 110/L Lecture 10. Kyle Dewey

Storage in Programs. largest. address. address

MIPS Functions and Instruction Formats

ECE260: Fundamentals of Computer Engineering

CSE Lecture In Class Example Handout

Code Generation. Lecture 19

CS 61c: Great Ideas in Computer Architecture

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.

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

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

Procedures and Stacks

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

Lectures 3-4: MIPS instructions

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

Instruction Set Architectures (4)

COE608: Computer Organization and Architecture

MIPS Assembly (Functions)

Lecture 6: Assembly Programs

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

Common Problems on Homework

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

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

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

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

Lecture 5: Procedure Calls

Lab 4 Prelab: MIPS Function Calls

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

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

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

Computer Architecture I Midterm I

Implementing Procedure Calls

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

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

MIPS Functions and the Runtime Stack

CS153: Compilers Lecture 8: Compiling Calls

CS 316: Procedure Calls/Pipelining

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

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

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

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

Code Generation. Lecture 12

EE 361 University of Hawaii Fall

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

Midterm II CS164, Spring 2006

CS222: Dr. A. Sahu. Indian Institute of Technology Guwahati

Compilers and computer architecture: A realistic compiler to MIPS

The Activation Record (AR)

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

CS61C : Machine Structures

Compiling Code, Procedures and Stacks

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

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

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

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

Thomas Polzer Institut für Technische Informatik

MIPS function continued

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

CS/COE1541: Introduction to Computer Architecture

Computer Systems and Networks

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

Compiling Techniques

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

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

Computer Hardware Engineering

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

CS 110 Computer Architecture MIPS Instruction Formats

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS 61c: Great Ideas in Computer Architecture

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

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

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

Chapter 2A Instructions: Language of the Computer

Transcription:

CS64 Week 5 Lecture 1 Kyle Dewey

Overview More branches in MIPS Memory in MIPS MIPS Calling Convention

More Branches in MIPS else_if.asm nested_if.asm nested_else_if.asm

Memory in MIPS

Accessing Memory Two base instructions: load-word (lw) and store-word (sw) MIPS lacks instructions that do more with memory than access it (e.g., retrieve something from memory and add) Mark of RISC architecture

Global Variables Typically, global variables are placed directly in memory, not registers Why might this be?

Global Variables Typically, global variables are placed directly in memory, not registers Why might this be? Not enough registers

Global Variable Example access_global.asm

Arrays Question: as far as memory is concerned, what is the major difference between an array and a global variable?

Arrays Question: as far as memory is concerned, what is the major difference between an array and a global variable? Arrays contain multiple elements

Array Examples print_array1.asm print_array2.asm print_array3.asm -print_array1.asm: typical index-based loop -print_array2.asm: slightly optimized form of print_array1.asm -print_array3.asm: pointer arithmetic based loop, with fewer instructions

MIPS Calling Convention

Functions Up until this point, we have not discussed functions Why not?

Functions Up until this point, we have not discussed functions Why not? Memory is a must for the call stack...though we can make some progress without it -Because of things like recursion, we generally don t even know ahead of time how many variables we are going to need. This is what we have the stack for.

Implementing Functions What capabilities do we need for functions?

Implementing Functions What capabilities do we need for functions? Ability to execute code elsewhere Way to pass arguments Way to return values

Implementing Functions What capabilities do we need for functions? Ability to execute code elsewhere - branches and jumps Way to pass arguments - registers Way to return values - registers

Jumping to Code We have ways to jump to code What about jumping back? void foo() { bar(); baz(); } void bar() { }... void baz() { }...

Jumping to Code We have ways to jump to code What about jumping back? Need a way to save where we were What might this entail on MIPS? void foo() { bar(); baz(); } void bar() { }... void baz() { }...

Jumping to Code We have ways to jump to code What about jumping back? Need a way to save where we were What might this entail on MIPS? A way to store the program counter void foo() { bar(); baz(); } void bar() { }... void baz() { }...

Calling Functions on MIPS Two crucial instructions: jal and jr jal (jump-and-link) will simultaneously jump to an address, and store the location of the next instruction in register $ra jr (jump-register) will jump to the address stored in a register, often $ra

Calling Functions on MIPS simple_call.asm

Passing and Returning Values We want to be able to call arbitrary functions without knowing the implementation details How might we achieve this?

Passing and Returning Values We want to be able to call arbitrary functions without knowing the implementation details How might we achieve this? Designate specific registers for arguments and return values

Passing and Returning Values on MIPS Registers $a0 - $a3: argument registers, for passing function arguments Registers $v0, $v1: return registers, for passing return values

Passing and Returning Values on MIPS print_ints.asm add_ints.asm

Problem What about this code makes this setup break? void foo() { bar(); } void bar() { baz(); } void baz() {}

Problem What about this code makes this setup break? Need multiple copies of $ra void foo() { bar(); } void bar() { baz(); } void baz() {} -We d have to copy the value of $ra to another register before calling another function -This can be done, but eventually we re going to run out of registers. Call stacks more than 32 functions deep are common in practice, so we can t possibly store everything in registers

Another Problem What about this code makes this setup break? void foo() { int a0, a1,..., a20; bar(); } void bar() { int a21, a22,..., a40; }

Another Problem -With knowing which registers are usable, both foo and bar can t use, say, $t0 at the same time, or else they might step on each other s toes. If foo sets $t0 to some value and then calls bar, if bar sets $t0 to some other value, then this might mess things up in foo when bar returns if foo still needs the value in $t0 What about this code makes this setup break? Can t fit all variables in registers at the same time. How do I know which registers are even usable without looking at the code? void foo() { int a0, a1,..., a20; bar(); } void bar() { int a21, a22,..., a40; }

Solution Store certain information in memory at certain times Ultimately, this is where the call stack comes from

Who saves what? Certain registers are designated to be preserved across a call Preserved registers are saved by the function called (e.g., $s0 - $s7) Non-preserved registers are saved by the caller of the function (e.g., $t0 - $t9) Question: why a split?

Who saves what? Certain registers are designated to be preserved across a call Preserved registers are saved by the function called (e.g., $s0 - $s7) Non-preserved registers are saved by the caller of the function (e.g., $t0 - $t9) Question: why a split? - not everything is worth saving

Saved where? Register values are saved on the stack The top of the stack is held in $sp (stackpointer) The stack grows from high addresses to low addresses

Register Saving Example save_registers.asm

Recursion This same setup handles nested function calls and recursion - we can save $ra on the stack Example: recursive_fibonacci.asm