CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Similar documents
CA Compiler Construction

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

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Topic Notes: MIPS Instruction Set Architecture

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Chapter 2: Instructions:

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

Chapter 3. Instructions:

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

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

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.

Course Administration

CSE Lecture In Class Example Handout

Run-time Environment

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

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

Instructions: Language of the Computer

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Functions in MIPS. Functions in MIPS 1

Chapter 2A Instructions: Language of the Computer

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:

ECE232: Hardware Organization and Design

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

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

Instructions: Assembly Language

CSE Lecture In Class Example Handout

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.

Today. Putting it all together

Compilers and computer architecture: A realistic compiler to MIPS

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

CS 316: Procedure Calls/Pipelining

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

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

Code Generation. Lecture 19

Course Administration

Instruction Set Architectures Part I: From C to MIPS. Readings:

Computer Architecture

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Computer Organization MIPS ISA

ECE232: Hardware Organization and Design

CENG3420 Lecture 03 Review

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

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

CS/COE1541: Introduction to Computer Architecture

Code Generation. Lecture 12

MIPS Functions and the Runtime Stack

Project Compiler. CS031 TA Help Session November 28, 2011

Chapter 2. Instruction Set Architecture (ISA)

Shift and Rotate Instructions

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

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Lecture 5: Procedure Calls

MIPS function continued

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

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

ECE 154A Introduction to. Fall 2012

MIPS (SPIM) Assembler Syntax

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

CS3350B Computer Architecture MIPS Introduction

ECE260: Fundamentals of Computer Engineering

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

CS 61c: Great Ideas in Computer Architecture

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

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

This section provides some reminders and some terminology with which you might not be familiar.

Compiling Code, Procedures and Stacks

Instruction Set Architectures (4)

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

Previously. CSE 2021: Computer Organization. Memory Organization. The Load/Store Family (1) 5/26/2011. Used to transfer data to/from DRAM.

CIT Week13 Lecture

Stack Frames. Remind Ourselves why stack-frames are required. Appreciate their role in register-based processors

ECE331: Hardware Organization and Design

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

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Instruction Set Architecture

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

MIPS Procedure Calls - Review

Wednesday, October 15, 14. Functions

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

EN164: Design of Computing Systems Lecture 09: Processor / ISA 2

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

MIPS Procedure Calls. Lecture 6 CS301

CS3350B Computer Architecture

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

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

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

The Activation Record (AR)

COMP2611: Computer Organization MIPS function and recursion

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

Implementing Procedure Calls

ECE369. Chapter 2 ECE369

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

Digital System Design Using Verilog. - Processing Unit Design

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Transcription:

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays 1

Memory Alignment Perhaps at some point in your MIPS assembly programming you tried to perform a lw and received an error that the data you tried to access was not aligned on a word boundary When accessing multi-byte quantities (e.g., words), the CPU will read or write data only in an aligned fashion 1-byte values can be read from or written to at any address 2-byte values can be accessed at even-numbered addresses 4-byte values can be accessed only at addresses that are multiples of 4 You will want to keep these rules in mind as you start to do more work with arrays in your MIPS assembly programs 2

Single-dimensional Arrays Recap To calculate the effective (actual) address of element i of a single-dimensional array we need to do some basic arithmetic: address = base_addr + i*elem_size_in_bytes We could then do a lb or lw at that address to get the value What about two-dimensional arrays? How do we calculate the effective address of an element? The answer depends on how we lay out the values in memory: row-major order or column-major order 3

Element Order Suppose we have this 3 5, 2D array: a b c d e f g h i j k l m n o If we use row-major order, elements are stored in memory like this: a b c d e f g h i j k l m n o But if we use column-major order, elements are stored in this order: a f k b g l c h m d i n e j o The technique that is used is usually dependent on the programming language With MIPS we can use either because we have to lay out everything ourselves, anyway! For simplicity we will use row-major order in this course 4

Accessing Array Elements Assuming we use row-major order to store elements in a 2D array, the memory address of element [i][j] is addr = base_addr + i * size_of_a_row_in_bytes + j * size_of_a_column_in_bytes This translates into: addr = base_addr + i * num_columns * elem_size_in_bytes + j * elem_size_in_bytes addr = base_addr + elem_size_in_bytes * (i * num_columns + j) 5

Example: 2d_array.asm This is a simple program that shows the calculation in practice It stores the values 65 through 79 (A through N) in a 3 5 2D array of words stored in row-major order 6

The Frame Pointer ($fp) You may recall this pair of pictures from earlier in the term. Figure (a) shows the stack before the callee is invoked In figure (b), the callee has been invoked 7

The Frame Pointer ($fp) Consider the activation record (stack frame): Suppose during execution of our function we need to make reference to the additional arguments section Normally we would simple use an offset relative to $sp However, there s a problem. What if the callee starts pushing/popping data on the stack during execution? 8

The Frame Pointer ($fp) Although a value buried in the stack frame (e.g., additional arguments) will have a fixed memory address, its location relative to $sp will change as $sp itself changes due to pushes and pops of the runtime stack 9

The Frame Pointer ($fp) As an example, perhaps when the function begins executing we can use 40($sp) to grab an extra argument from the additional arguments section But as time goes on, the function pushes and pops data to the stack, so that argument is sometimes at 56($sp), sometimes at 40($sp) again, then later it s at 48($sp) This is going to make our code really confusing So instead of using $sp for our references into the stack, we can use the frame pointer, which is register $fp At the start of the function s execution we move $fp, $sp and then do our accesses into the stack frame using $fp, which will not change during the duration of the function call 10

The Frame Pointer ($fp),$fp This is only just one example of how we could use $fp. The main idea here is not to rely only on $sp to get data from the stack because $sp can change during a function call. Note that $fp is a preserved register. Other data that has been pushed onto the stack by the called function (callee) $fp $sp 11

The Frame Pointer ($fp) Here s another example of how the frame pointer is useful Suppose we have a complex function with a lot of paths through it: an if-statement with a lot of else if clauses Now in each case of the if-statement we push a different number of items onto the stack When the function is ready to return, we need to pop off everything we pushed, but we have lost track of how much data we have pushed! If we had set $fp equal to $sp at the start of the function call, we could now simply do move $sp, $fp 12