Example: Array Access

Similar documents
Computer Systems Architecture

Instruction Set Architecture

Reduced Instruction Set Computer (RISC)

More MIPS: Recursion. Computer Science 104 Lecture 9

Typy danych. Data types: Literals:

Translating C code to MIPS

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

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

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

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

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

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

MIPS Assembly Code Layout

Intel 8086 architecture

Review: MIPS Addressing Modes/Instruction Formats

Introduction to MIPS Assembly Programming

Assembly Language Programming

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

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

Computer organization

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

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

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

Stack Allocation. Run-Time Data Structures. Static Structures

Figure 1: Graphical example of a mergesort 1.

PROBLEMS (Cap. 4 - Istruzioni macchina)

Sources: On the Web: Slides will be available on:

Computer Organization and Components

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

Instruction Set Architecture

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

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

The Operating System and the Kernel

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

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

An Introduction to the ARM 7 Architecture

X86-64 Architecture Guide

Chapter 7D The Java Virtual Machine

The stack and the stack pointer

5 MIPS Assembly Language

Exception and Interrupt Handling in ARM

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

Tutorial on C Language Programming

What to do when I have a load/store instruction?

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

Instruction Set Design

Instruction Set Architecture (ISA)

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Module 2 Stacks and Queues: Abstract Data Types

Keil C51 Cross Compiler

Machine-Code Generation for Functions

C++ INTERVIEW QUESTIONS

MIPS Assembler and Simulator

Instruction Set Architecture (ISA) Design. Classification Categories

How To Write An Array In A Microsoft Zil

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

1 Classical Universal Computer 3

Introducción. Diseño de sistemas digitales.1

6. Control Structures

EC 362 Problem Set #2

CS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e

Computer Architectures

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

THUMB Instruction Set

Lecture 2 Notes: Flow of Control

8085 INSTRUCTION SET

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

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

MACHINE ARCHITECTURE & LANGUAGE

Assembly Language: Function Calls" Jennifer Rexford!

CHAPTER 7: The CPU and Memory

HC12 Assembly Language Programming

MICROPROCESSOR AND MICROCOMPUTER BASICS

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

Efficient Low-Level Software Development for the i.mx Platform

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

umps software development

Laboratorio di Sistemi Operativi Anno Accademico

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

Notes on Assembly Language

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

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

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

GSPIM: Graphical Visualization Tool for MIPS Assembly

CS521 CSE IITG 11/23/2012

The C Programming Language course syllabus associate level

Java Virtual Machine Locks

Bypassing Browser Memory Protections in Windows Vista

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

Introduction to MIPS Programming with Mars

(Refer Slide Time: 00:01:16 min)

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

10CS35: Data Structures Using C

Computer Organization and Architecture

Systems I: Computer Organization and Architecture

We will use the accumulator machine architecture to demonstrate pass1 and pass2.

CS2210: Compiler Construction. Runtime Environment

ELEC 377. Operating Systems. Week 1 Class 3

CS61: Systems Programing and Machine Organization

A3 Computer Architecture

Transcription:

ECE232: Hardware Organization and Design Part 7: MIPS Instructions III http://www.ecs.umass.edu/ece/ece232/ Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Example: Array Access Access the i-th element of an array A (each element is 32-bit long) # $t0 = address of start of A # $t1 = i sll $t1,$t1,2 # $t1 = 4*i add $t2,$t0,$t1 # add offset to the address of A[0] # now $t2 = address of A[i] lw $t3,0($t2) # $t3 = whatever is in A[i] ECE232: MIPS Instructions-III 2

if statement if ( condition ) { statements # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, if_end_label # MIPS code for the statements if_end_label: ECE232: MIPS Instructions-III 3 if else statement if ( condition ) { if-statements else { else-statements # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, else_label # MIPS code for the if-statements j if_end_label else_label: # MIPS code for the else-statements if_end_label: ECE232: MIPS Instructions-III 4

while statement while ( condition ) { statements while_start_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, while_end_label # MIPS code for the statements j while_start_label while_end_label: ECE232: MIPS Instructions-III 5 do-while statement do { statements while ( condition ); do_start_label: # MIPS code for the statements do_cond_label: # MIPS code for the condition expression do_end_label: #(if condition satisfied set $t0=1) beq $t0, $zero, do_end_label j do_start_label ECE232: MIPS Instructions-III 6

for loop for ( init ; condition ; incr ) { statements # MIPS code for the init expression for_start_label: # MIPS code for the condition expression #(if condition satisfied set $t0=1) beq $t0, $zero, for_end_label # MIPS code for the statements # MIPS code for the incr expression j for_start_label for_end_label: ECE232: MIPS Instructions-III 7 switch statement switch ( expr ) { case const1: statement1 case const2: statement2... case constn: statementn default: default-statement ECE232: MIPS Instructions-III 8

MIPS code for switch statement # MIPS code for $t0=expr beq $t0, const1, switch_label_1 beq $t0, const2, switch_label_2... beq $t0, constn, switch_label_n j switch_default switch_label_1: # MIPS code to compute statement1 switch_label_2: # MIPS code to compute statement2... switch_default: # MIPS code to compute default-statement switch_end_label: ECE232: MIPS Instructions-III 9 Logical AND in expression if (cond1 && cond2){ statements # MIPS code to compute cond1 # Assume that this leaves the value in $t0 # If cond1=false $t0=0 beq $t0, $zero, and_end # MIPS code to compute cond2 # Assume that this leaves the value in $t0 # If cond2=false $t0=0 beq $t0, $zero, and_end # MIPS code for the statements and_end: ECE232: MIPS Instructions-III 10

Switch Example switch (i) { //Assume i is in $s1 and j is in $s2; case 0: j = 3; break; case 1: j = 5; break; case 2: ; case 3: j = 11; break; case 4: j = 13; break; default: j = 17; main: add $t0, $zero, $zero # $t0 = 0, temp. variable beq $t0, $s1, case0 # go to case0 addi $t0, $t0, 1 # $t0 = 1 beq $t0, $s1, case1 # go to case1 addi $t0, $t0, 1 # $t0 = 2 beq $t0, $s1, case2 # go to case2 addi $t0, $t0, 1 # $t0 = 3 beq $t0, $s1, case3 # go to case3 addi $t0, $t0, 1 # $t0 = 4 beq $t0, $s1, case4 # go to case4 j default # go to default case case0: addi $s2, $zero, 3 # j = 3 j finish # exit switch block ECE232: MIPS Instructions-III 11 Example: Conditional and unconditional branches Conditional branch: Jump to instruction L1 if register1 equals register2: beq $s1, $s2, L1 Similarly, bne Unconditional branch: j L1 jr $s5 (useful for large case statements and big jumps) Convert to assembly: if (i == j) f = g+i; else f = g-i; ELSE: EXIT: bne $s0, $s1, ELSE add $s3, $s2, $s0 j EXIT sub $s3, $s2, $s0 ECE232: MIPS Instructions-III 12

Example 2 Convert to assembly: while (save[i] == k) i += 1; i and k are in $s3 and $s5 and base of array save[] is in $s6 Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: ECE232: MIPS Instructions-III 13 Procedures Each procedure (function, subroutine) maintains a scratchpad of register values when another procedure is called (the callee), the new procedure takes over the scratchpad values may have to be saved so we can safely return to the caller parameters (arguments) are placed where the callee can see them control is transferred to the callee acquire storage resources for callee execute the procedure place result value where caller can access it return control to caller ECE232: MIPS Instructions-III 14

Registers The 32 MIPS registers are partitioned as follows: Register 0 : $zero always stores the constant 0 Regs 2-3 : $v0, $v1 return values of a procedure Regs 4-7 : $a0-$a3 input arguments to a procedure Regs 8-15 : $t0-$t7 temporaries Regs 16-23: $s0-$s7 variables Regs 24-25: $t8-$t9 more temporaries Reg 28 : $gp global pointer Reg 29 : $sp stack pointer Reg 30 : $fp frame pointer Reg 31 : $ra return address ECE232: MIPS Instructions-III 15 Jump-and-Link A special register (not part of the register file) maintains the address of the instruction currently being executed this is the program counter (PC) The procedure call is executed by invoking the jump-and-link (jal) instruction the current PC (actually, PC+4) is saved in the register $ra and jump to the procedure s address (the PC is accordingly set to this address) jal NewProcedureAddress Since jal may over-write a relevant value in $ra, it must be saved somewhere (in memory?) before invoking the jal instruction How do we return control back to the caller after completing the callee procedure? ECE232: MIPS Instructions-III 16

The Stack The register scratchpad for a procedure seems volatile It may be modified every time we switch procedures A procedure s values are therefore backed up in memory on a stack Stack grows this way Proc A s values Proc B s values Proc C s values High address Low address Proc A call Proc B call Proc C return return return ECE232: MIPS Instructions-III 17 What values are saved? Preserved Saved registers: $s0-$s7 Stack Pointer: $sp Return Address Register: $ra Stack above the stack pointer Not Preserved Temporary registers: $t0-$t9 Argument registers: $a0-$a3 Return registers: $v0-$v1 Stack below the pointer $sp 7fff fffc Stack $gp 1000 8000 1000 0000 pc 0040 0000 0 Dynamic Data Static Data Text Reserved ECE232: MIPS Instructions-III 18

Storage Management on a Call/Return Arguments are copied into $a0-$a3; the jal is executed The new procedure (callee) must create space for all its variables on the stack After the callee creates stack space, it updates the value of $sp Once the callee finishes, it copies the return value into $v0, frees up stack space, and $sp is incremented On return, the caller may bring in its stack values, ra, temps into registers The responsibility for copies between stack and registers may fall upon either the caller or the callee ECE232: MIPS Instructions-III 19 Leaf Procedure Example Procedures that don t call other procedures C code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; Arguments g,, j in $a0,, $a3 f in $s0 (hence, need to save $s0 on stack) Result in $v0 ECE232: MIPS Instructions-III 20

Leaf Procedure Example MIPS code: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return ECE232: MIPS Instructions-III 21 Non-Leaf Procedures Procedures that call other procedures For nested call, caller needs to save on the stack: Its return address Any arguments and temporaries needed after the call Restore from the stack after the call Example - Recursion (C code): int factorial (int n) { if (n < 1) return 1; else return n * factorial (n - 1); Argument n in $a0 Result in $v0 ECE232: MIPS Instructions-III 22

Non-Leaf Procedure Example MIPS code: factorial: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for i < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and return L1: addi $a0, $a0, -1 # else decrement i jal factorial # recursive call lw $a0, 0($sp) # restore previous i lw $ra ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return Notes: The callee saves $a0 and $ra in its stack space. Temps are never saved. ECE232: MIPS Instructions-III 23 Recursion: Factorial int factorial (int n) { if (n < 1) return 1; else return n * factorial (n - 1); ECE232: MIPS Instructions-III 24

Memory Organization The space allocated on stack by a procedure is termed the activation record (includes saved values and data local to the procedure) Frame pointer points to the start of the record and stack pointer points to the end Variable addresses are specified relative to $fp as $sp may change during the execution of the procedure $gp points to area in memory that saves global variables Dynamically allocated storage (with malloc()) is placed on the heap Stack Dynamic data (heap) Static data (globals) Text (instructions) ECE232: MIPS Instructions-III 25 Summary The jal instruction is used to jump to the procedure and save the current PC (+4) into the return address register Arguments are passed in $a0-$a3; return values in $v0-$v1 Since the callee may over-write the caller s registers, relevant values may have to be copied into memory Each procedure may also require memory space for local variables A stack is used to organize the memory needs for each procedure ECE232: MIPS Instructions-III 26