MIPS Memory Organization

Similar documents
Computer Systems Architecture

Typy danych. Data types: Literals:

Introduction to MIPS Assembly Programming

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

More MIPS: Recursion. Computer Science 104 Lecture 9

Translating C code to MIPS

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Instruction Set Architecture

Figure 1: Graphical example of a mergesort 1.

MIPS Assembly Code Layout

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

How To Write An Array In A Microsoft Zil

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Reduced Instruction Set Computer (RISC)

Lab Work 2. MIPS assembly and introduction to PCSpim

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Instruction Set Architecture (ISA)

2) Write in detail the issues in the design of code generator.

Intel 8086 architecture

DATA STRUCTURE - STACK

Instruction Set Architecture (ISA) Design. Classification Categories

PROBLEMS (Cap. 4 - Istruzioni macchina)

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

CHAPTER 7: The CPU and Memory

Instruction Set Design

The Operating System and the Kernel

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Stack Allocation. Run-Time Data Structures. Static Structures

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Assembly Language Programming

Compilers I - Chapter 4: Generating Better Code

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Parameter Passing in Pascal

Stacks. Linear data structures

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Linked Lists Linked Lists, Queues, and Stacks

1 Classical Universal Computer 3

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

MICROPROCESSOR AND MICROCOMPUTER BASICS

Review: MIPS Addressing Modes/Instruction Formats

5 MIPS Assembly Language

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Data Structures and Data Manipulation

CSE 141 Introduction to Computer Architecture Summer Session I, Lecture 1 Introduction. Pramod V. Argade June 27, 2005

1 The Java Virtual Machine

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Chapter 1 Computer System Overview

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

CS2210: Compiler Construction. Runtime Environment

A Tiny Guide to Programming in 32-bit x86 Assembly Language

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Data Structures. Level 6 C Module Descriptor

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Broadband Networks. Prof. Karandikar. Department of Electrical Engineering. Indian Institute of Technology, Bombay. Lecture - 26

RecoverIt Frequently Asked Questions

Using the Game Boy Advance to Teach Computer Systems and Architecture

Symbol Tables. Introduction

Lecture 2 Mathcad Basics

Computer organization

MIPS Assembler and Simulator

Habanero Extreme Scale Software Research Project

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Introduction to MIPS Programming with Mars

The stack and the stack pointer

Instruction Set Architecture

A3 Computer Architecture

İSTANBUL AYDIN UNIVERSITY

An Introduction to the ARM 7 Architecture

Common Operating-System Components

COMPUTER ORGANIZATION ARCHITECTURES FOR EMBEDDED COMPUTING

The Little Man Computer

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

Q. Consider a dynamic instruction execution (an execution trace, in other words) that consists of repeats of code in this pattern:

The Design of the Inferno Virtual Machine. Introduction

Operating System Components

Implementation Aspects of OO-Languages

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

C# and Other Languages

General Introduction

Functions and Parameter Passing

Analysis of Micromouse Maze Solving Algorithms

MACHINE INSTRUCTIONS AND PROGRAMS

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

PARALLELIZED SUDOKU SOLVING ALGORITHM USING OpenMP

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Online Sharing User Manual

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

VLIW Processors. VLIW Processors

Transcription:

MIPS Memory Organization 1 In addition to memory for static data and the program text (machine code), MIPS provides space for the run-time stack (data local to procedures, etc.) and for dynamically-allocated data: Stack $sp # last word alloc on stack Dynamic data Static data Text Reserved $gp # ptr into global data $pc # ptr to next instruction Dynamic data is accessed via pointers held by the program being executed, with addresses returned by the memory allocator in the underlying operating system.

The System Stack 2 MIPS provides a special register, $sp, which holds the address of the most recently allocated word on a stack that user programs can employ to hold various values: Note that this run-time stack is "upside-down". That is, $sp, decreases when a value is added to the stack and increases when a value is removed. So, you decrement the stack pointer by 4 when pushing a new value onto the stack and increment it by 4 when popping a value off of the stack.

Using the System Stack MIPS programs use the runtime stack to hold: 3 - parameters to be passed to a called procedure - register values that need to be preserved during the execution of a called procedure and restored after the return - saved procedure return address, if necessary - local arrays and structures, if any activation record or stack frame for called procedure

Finding the Median Value 4 Consider implementing a MIPS procedure to find the median value in an array of integers. The only efficient way to do this is to partially sort the array elements until the median value is revealed. For example, given the list of eleven values below below 17 43 21 19 6 34 32 25 45 29 13 we could sort it into the following form 6 13 17 19 21 25 and then see that the median is clearly 25.

Stack Layout 5 What we do not want to do is destroy the original list while finding the median. So, we need to give our MIPS procedure a copy of the list, and the place to do that is the runtime stack. Before calling the procedure, we will organize the stack like this: +---------+ A[N-1] +---------+ +---------+ A[0] +---------+ N <-- $sp +---------+ The procedure can then manipulate the list elements directly on the stack, and use an appropriate register to return the median value.

Creating the Stack Layout The caller must create room on the stack to hold the necessary data: 6 Size:.word 10 List:.word 17, 43, 21, 19, 6, 34, 32, 25, 45, 29, 13.text main: lw $s0, Size # get size of list li $s4, 4 # size of word mul $s4, $s4, $s0 # compute size of array on stack addi $s4, $s4, 4 # allow space for array dimension sub $sp, $sp, $s4 # make room on stack for list and dimension used old $sp--> free space for array and dimension $sp--> free

Creating the Stack Layout 7 The caller must then write the necessary data to the stack in the correct places: sw $s0, ($sp) # put size onto the stack move $s1, $sp # get pointer to top of stack la $s2, List # get pointer to first list element li $t0, 0 # count list elements as they are copied loop: beq $t0, $s0, done addi $s1, $s1, 4 # step to location for next list element lw $s3, ($s2) sw $s3, ($s1) # put element onto stack addi $t0, $t0, 1 addi $s2, $s2, 4 # step to next list element b loop done: old 29 $sp--> 17 10 free

Called Procedure's View The called procedure retrieves/writes data on the stack as needed: find_median: 8 lw addi $t3, ($sp) # get list dimension $t5, $sp, 4 # get address of beginning of list ## code to perform partial sort of array elements on the stack ## using any suitable algorithm lw $t0, ($sp) # get array size li $t1, 2 div $t0, $t1 # divide it by 2 mfhi $t1 # get the remainder from the divisio beq $t1, $zero, even # check whether size was even or odd # list has odd number of elements; set median odd: # median is in middle cell of array b store # list has even number of elements; calculate median even: # median is average of two elements store: swc1 $f0, ($sp) # put median onto stack for caller jr $ra # return to caller

Cleaning Up the Stack 9 In this case, it was the caller who put stuff onto the stack, so the caller's responsible for popping it off: jal find_median l.s $f12, ($sp) # retrieve return value add $sp, $sp, $s4 # pop stack pointer to original position old used $sp--> space for array and dimension free $sp--> free

Local Storage on the Stack 10 In some cases, the called procedure needs more local storage space than the available registers can provide. For example, a procedure might need to create an array whose dimension is determined by a parameter to the procedure: proc: sub $sp, $sp, $t7 # need a local array of size $t7 move $t0, $sp # $t0 points to the array on the stack ## do stuff with the array, BUT better not mess up $t7 add $sp, $sp, $t7 # pop the array off the stack

The Frame Pointer 11 The frame pointer register, $fp, is intended as a "bookmark" to keep track of where the stack pointer was when a procedure was entered: proc: move $fp, $sp # $fp points to original stack top sub $sp, $sp, $a0 # need a local array of size $a0 move $t0, $sp # $t0 points to the array on the stack ## do stuff with the array; mess with $a0 all you like move $sp, $fp # restore stack pointer to original value jr $ra # right before you return

The $sp Contract 12 The implementation of a procedure must guarantee that the value of $sp is the same when the return is executed as it was when the procedure was entered. Failure to do this can cause all sorts of problems

Register Backup on the Stack 13 In some cases, the caller and/or the called procedure must use the stack to preserve values of $s or $t registers: ## Assume the caller has used the registers $t0 and $t5, and needs for those ## registers to have the same values after a procedure call: addi $sp, $sp, -8 # make room on stack for two register values sw $t0, 4($sp) # back up $t0 at $sp + 4 sw $t5, 0($sp) # back up $t5 at $sp ## Now push anything on the stack need to prepare for the procedure call: ## And then make the call: jal find_median ## Retrieve results from stack, if necessary, and pop all the call-related ## stuff from the stack: ## And then retrieve the pointer values: lw $t5, 0($sp) lw $t0, 4($sp) ## And then pop them off the stack: add $sp, $sp, 8

Leaf and Non-Leaf Procedures 14 A leaf procedure is one that doesn't all any other procedures. A non-leaf procedure is one that does call another procedure. Non-leaf procedures pose an additional, but simple, challenge; we make procedure calls by executing a jump-and-link instruction: jal procedure_0 # puts PC+4 into $ra for return But, if procedure_0 also makes a call, say jal procedure_1 # puts PC+4 into $ra for return then the original return address just got overwritten the effect is fascinating

Preserving the Return Address 15 Non-leaf procedures must back up the value of their return address before making a call to another procedure: addi sw $sp, $sp, -4 # make room on stack $ra, 0($sp) # save return address And they must restore the return address before they attempt to return: lw addi $ra, 0($sp) # retrieve return address $sp, $sp, 4 # pop it off the stack Failure to do this will almost certainly lead to a catastrophic runtime failure. The safest way to do this is to back up the address immediately when the procedure is entered, and to restore it immediately before the return is executed. Of course, you must keep careful track of the stack pointer during all of this