CSE315 Computer Organization. Lecture 4 MIPS ISA

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

Computer Systems Architecture

MIPS Assembly Code Layout

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

Typy danych. Data types: Literals:

More MIPS: Recursion. Computer Science 104 Lecture 9

Reduced Instruction Set Computer (RISC)

Introduction to MIPS Assembly Programming

Translating C code to MIPS

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

Instruction Set Architecture

Review: MIPS Addressing Modes/Instruction Formats

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

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

Instruction Set Architecture (ISA)

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

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

Assembly Language Programming

Intel 8086 architecture

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

Figure 1: Graphical example of a mergesort 1.

Instruction Set Design

5 MIPS Assembly Language

X86-64 Architecture Guide

Stack Allocation. Run-Time Data Structures. Static Structures

Chapter 7D The Java Virtual Machine

PROBLEMS (Cap. 4 - Istruzioni macchina)

MIPS Assembler and Simulator

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

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

Instruction Set Architecture (ISA) Design. Classification Categories

EE361: Digital Computer Organization Course Syllabus

EC 362 Problem Set #2

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

Machine-Code Generation for Functions

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

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

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

Keil C51 Cross Compiler

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

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

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

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

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

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

Chapter 5 Instructor's Manual

Compilers I - Chapter 4: Generating Better Code

İSTANBUL AYDIN UNIVERSITY

An Introduction to the ARM 7 Architecture

Computer Organization and Architecture

Lab Work 2. MIPS assembly and introduction to PCSpim

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

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

MACHINE ARCHITECTURE & LANGUAGE

1 Classical Universal Computer 3

THUMB Instruction Set

Notes on Assembly Language

MICROPROCESSOR AND MICROCOMPUTER BASICS

Computer organization

Computer Organization and Components

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

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

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

Summary of the MARIE Assembly Language

Computer Architectures

OAMulator. Online One Address Machine emulator and OAMPL compiler.

Lecture 27 C and Assembly

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

PART B QUESTIONS AND ANSWERS UNIT I

Computer Organization and Components

CPU Organization and Assembly Language

EECS 427 RISC PROCESSOR

x64 Cheat Sheet Fall 2015

The programming language C. sws1 1

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

7.1 Our Current Model

Assembly Language: Function Calls" Jennifer Rexford!

Lecture 2. Binary and Hexadecimal Numbers

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

Java Interview Questions and Answers

MACHINE INSTRUCTIONS AND PROGRAMS

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

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

The Operating System and the Kernel

1 The Java Virtual Machine

A deeper look at Inline functions

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

Habanero Extreme Scale Software Research Project

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

LC-3 Assembly Language

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

Introduction to MIPS Programming with Mars

Instruction Set Architecture

Lecture 22: C Programming 4 Embedded Systems

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

Microprocessor & Assembly Language

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Transcription:

CSE315 Computer Organization Lecture 4 MIPS ISA

Loading, Storing bytes In addition to word data transfers (lw, sw), MIPS has byte data transfers: load byte: lb store byte: sb Same format as lw, sw What to do with the remaining 24 bits? lb: sign extends to fill upper 24 bits MIPS instruction that doesn't sign extend when loading bytes: load byte unsigned: lbu Spring 2009 CSE315 - Lec 4 2

True Assembly Language Pseudo-instruction: A MIPS instruction that doesn t turn directly into a machine language instruction. What happens with pseudoinstructions? They re broken up by the assembler into several real MIPS instructions. But what is a real MIPS instruction? Spring 2009 CSE315 - Lec 4 3

Example: Pseudoinstructions Register Move move reg2,reg1 Expands to: add reg2,$zero,reg1 Load Immediate li reg,value If value fits in 16 bits: ori reg,$zero,value else: lui ori reg,upper 16 bits of value reg,$zero,lower 16 bits Spring 2009 CSE315 - Lec 4 4

True Assembly Language MAL (MIPS Assembly Language): the set of instructions that a programmer may use to code in MIPS; this includes pseudoinstructions TAL (True Assembly Language): the set of instructions that can actually get translated into a single machine language instruction (32-bit binary string) A program must be converted from MAL into TAL before it can be translated into machine code (1s and 0s). Spring 2009 CSE315 - Lec 4 5

Function or Procedure A separate piece of code Possibly separately compiled Located at some address in the memory used for code, away from main and other functions (main is itself a function called by the system) May return a value to caller Function may use parameters provided by caller Spring 2009 CSE315 - Lec 4 6

Function call Store parameters where function can find them Store address where control should return after call Go to function code Store information in registers that need to be used by function, so they can be restored Get parameters Execute function Put result where it can be found by caller Spring 2009 CSE315 - Lec 4 7

Function call continued Restore information in registers Return control to caller at appropriate address Caller get results Spring 2009 CSE315 - Lec 4 8

Function call -- issues How to save return address? How to provide parameters for function? How to save information stored in registers? How to store return value of function so it is available to caller after function terminates Spring 2009 CSE315 - Lec 4 9

MIPS solution Mixed: keep as much as possible in registers Spill register contents to memory stack when needed Single function calls often use only registers Deeper function calls always use stack how much depends on register usage by functions Spring 2009 CSE315 - Lec 4 10

MIPS Register Usage ra (31) used for function return address sp (29) used as stack pointer v0, v1 function return value(s) a0, a1, a2, a3 function parameters s registers guaranteed to hold same values across function calls (e.g. value after call is the same as it was before the call) t registers values may be changed by function call without being restored after Spring 2009 CSE315 - Lec 4 11

Function Call Caller saves the registers that are not preserved across call Of course, needed only if caller wants some value to be preserved $a0 to $a3, $v0, $v1, $t0 to $t9 Callee saves the registers that are required to be preserved across call Needed only if callee uses these registers $s0 to $s7, $gp, $sp, $fp, $ra Spring 2009 CSE315 - Lec 4 12

Instructions Supporting Procedure Calls Jump and link jal procedure address note: return address is stored in R 31 Return jr R 31 Spring 2009 CSE315 - Lec 4 13

Example swap Procedure (1/2) swap (int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } Spring 2009 CSE315 - Lec 4 14

Example swap Procedure (2/2) Register assignment: $a0 base address v of the integer array $a1 index k $t0 local variable temp No stack operation. void swap (int v[], int k) { int temp; temp = v[k]; } v[k] = v[k+1]; v[k+1] = temp; swap: add $t0, $a1, $a1 add $t0, $t0, $t0 add $t0, $t0, $a0 lw lw sw sw jr $t1, 0($t0) $t2, 4($t0) $t2, 0($t0) $t1, 4($t0) $ra Spring 2009 CSE315 - Lec 4 15

c code: Leaf Procedure Example 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 Spring 2009 CSE315 - Lec 4 16

Leaf Procedure Example MIPS code: 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 Spring 2009 CSE315 - Lec 4 17

Non-Leaf Procedures Procedures that call other procedures are known as non-leaf 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 Spring 2009 CSE315 - Lec 4 18

C code: Non-Leaf Procedure Example int fact (int n) { if (n < 1) return f; else return n * fact(n-1); } Argument n in $a0 Result in $v0 Spring 2009 CSE315 - Lec 4 19

Non-Leaf Procedure Example MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 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 n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $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 Spring 2009 CSE315 - Lec 4 20

MIPS operands Name Example Comments $s0-$s7, $t0-$t9, Fast locations for data. In MIPS, data must be in registers to perform 32 registers $zero, $a0-$a3, $v0- arithmetic. MIPS register $zero always equals 0. $gp (28) is the global $v1, $gp, $fp, $sp, $ra pointer, $sp(29) is the stack pointer, $fp (30) is the frame pointer, and $ra (31) is the return address. Memory [0], Accessed only by data transfer instructions. MIPS uses byte addresses, so 2 30 memory words Memory [4],, sequential words differ by 4. Memory holds data structures, such as arrays, Memory[42949672920 and spilled register, such as those saved on procedure calls. MIPS assembly language Category Instruction Example Meaning Comments Arithmetic add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers Data transfer load word lw $s1,100 ($s2) &s1 = Memory [$s2 + 100] Data from memory to register store word sw $s1,100 ($s2) Memory [$s2 + 100] = $s1 Data from register to memory branch on equal beq $s1, $s2, L if ($s1 == $s2) go to L Equal test and branch branch on not bne $s1, $s2, L if ($s1!= $s2) go to L Not equal test and branch Conditional branch equal set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else Compare less than: for beq, bne $s1 = 0 jump j 2500 go to 10000 jump to target address Unconditional jump jump register jr $ra go to $ra For switch, procedure return jump and link jal 2500 $ra = PC + 4; go to 1000 For procedure call Spring 2009 CSE315 - Lec 4 21