MIPS Architecture and Assembly Language Overview

Similar documents
Programming in MIPS. Tutorial

Compiling Techniques

COMP2421 COMPUTER ORGANZATION. Lab 3

MIPS Architecture and Assembly Language Overview

SPIM Instruction Set

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.

CENG3420 Lab 1-1: MIPS assembly language programing

MIPS Assembly Language

Computer Systems and Architecture

MIPS Processor Overview

MIPS Assembly Language Guide

MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

Introduction to MIPS Processor

Course Administration

Assignment 1: Pipelining Implementation at SPIM Simulator

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

QtSPIM and MARS : MIPS Simulators

instructions aligned is little-endian

Computer Architecture. The Language of the Machine

We will study the MIPS assembly language as an exemplar of the concept.

MIPS Reference Guide

MIPS Instruction Set

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

MIPS Assembly Language Programming

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

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

Reduced Instruction Set Computer (RISC)

MIPS Assembly Language. Today s Lecture

MIPS Coding Snippets. Prof. James L. Frankel Harvard University. Version of 9:32 PM 14-Feb-2016 Copyright 2016 James L. Frankel. All rights reserved.

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

Chapter 2A Instructions: Language of the Computer

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

MIPS Assembly Language Programming

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

MIPS function continued

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

Reduced Instruction Set Computer (RISC)

Computer Architecture. MIPS Instruction Set Architecture

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

Lecture 7: Examples, MARS, Arithmetic

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Course Administration

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CS3350B Computer Architecture

ECE 250 / CS 250 Computer Architecture. Procedures

USING A SIMULATOR. QUICK INTRODUCTION From various sources For cs470

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

ECE 30 Introduction to Computer Engineering

ECE331: Hardware Organization and Design

CS3350B Computer Architecture MIPS Introduction

MIPS Instruction Format

MIPS Assembly Programming

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

The MIPS Instruction Set Architecture

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

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

MIPS (SPIM) Assembler Syntax

M2 Instruction Set Architecture

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

ECE232: Hardware Organization and Design

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

Kernel Registers 0 1. Global Data Pointer. Stack Pointer. Frame Pointer. Return Address.

ECE331: Hardware Organization and Design

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

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

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

EECS 322 The SPIM simulator

Computer Organization MIPS ISA

CENG3420 Lecture 03 Review

CPSC 330 Computer Organization

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

Course Administration

TSK3000A - Generic Instructions

Examples of branch instructions

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

Lecture 5: Procedure Calls

CS 61c: Great Ideas in Computer Architecture

Instructions: Language of the Computer

Arithmetic for Computers

Thomas Polzer Institut für Technische Informatik

Common Problems on Homework

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Flow of Control -- Conditional branch instructions

MIPS Assembly Language Programming

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Computer Architecture

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

MIPS%Assembly% E155%

Integer Multiplication and Division

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

MIPS Assembly Language

MIPS Assembly (Functions)

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

Lecture 6: Assembly Programs

Transcription:

MIPS Architecture and Assembly Language Overview Adapted from: http://edge.mcs.dre.g.el.edu/gicl/people/sevy/architecture/mipsref(spim).html [Register Description] [I/O Description] Data Types and Literals Data types: Literals: Registers Instructions are all 32 bits byte(8 bits), halfword (2 bytes), word (4 bytes) a character requires 1 byte of storage an integer requires 1 word (4 bytes) of storage numbers entered as is. e.g. 4 characters enclosed in single quotes. e.g. 'b' strings enclosed in double quotes. e.g. "A string" 32 general-purpose registers register preceded by $ in assembly language instruction two formats for addressing: using register number e.g. $0 through $31 using equivalent names e.g. $t1, $sp special registers Lo and Hi used to store result of multiplication and division not directly addressable; contents accessed with special instruction mfhi ("move from Hi") and mflo ("move from Lo") stack grows from high memory to low memory This is from Figure 9.9 in the Goodman&Miller text Register Number Alternative Name 0 zero the value 0 Description 1 $at (assembler temporary) reserved by the assembler 2-3 $v0 - $v1 (values) from expression evaluation and function results 4-7 $a0 - $a3 8-15 $t0 - $t7 16-23 $s0 - $s7 24-25 $t8 - $t9 (arguments) First four parameters for subroutine. Not preserved across procedure calls (temporaries) Caller saved if needed. Subroutines can use w/out saving. Not preserved across procedure calls (saved values) - Callee saved. A subroutine using one of these must save original and restore it before exiting. Preserved across procedure calls (temporaries) Caller saved if needed. Subroutines can use w/out saving. These are in addition to $t0 - $t7 above. Not preserved across procedure calls. 26-27 $k0 - $k1 reserved for use by the interrupt/trap handler 28 $gp 29 $sp 30 $s8/$fp 31 $ra return address global pointer. Points to the middle of the 64K block of memory in the static data segment. stack pointer Points to last location on the stack. saved value / frame pointer Preserved across procedure calls See also Britton section 1.9, Sweetman section 2.21, Larus Appendix section A.6 Program Structure just plain text file with data declarations, program code (name of file should end in suffix.s to be used with SPIM simulator) data declaration section followed by program code section Data Declarations Code placed in section of program identified with assembler directive declares variable names used in program; storage allocated in main memory (RAM) placed in section of text identified with assembler directive

contains program code (instructions) starting point for code e.g.ecution given label main: ending point of main code should use exit system call (see below under System Calls) Comments anything following # on a line # This stuff would be considered a comment Template for a MIPS assembly language program: # Comment giving name of program and description of function # Template.s # Bare-bones outline of MIPS assembly language program # variable declarations follow this line #... # instructions follow this line main: # indicates start of code (first instruction to execute) #... # End of program, leave a blank line afterwards to make SPIM happy Data Declarations format for declarations: name: storage_type value(s) create storage for variable of specified type with given name and specified value value(s) usually gives initial value(s); for storage type.space, gives number of spaces to be allocated Note: labels aays followed by colon ( : ) example var1:.word 3 # create a single integer variable with initial value 3 array1:.byte 'a','b' # create a 2-element character array with elements initialized # to a and b array2:.space 40 # allocate 40 consecutive bytes, with storage uninitialized # could be used as a 40-element character array, or a # 10-element integer array; a comment should indicate which! Load / Store Instructions load: RAM access only allowed with load and store instructions all other instructions use register operands register_destination, RAM_source #copy word (4 bytes) at source RAM location to destination register. lb register_destination, RAM_source #copy byte at source RAM location to low-order byte of destination register, # and sign-e.g.tend to higher-order bytes store word: register_source, RAM_destination #store word in source register into RAM destination sb register_source, RAM_destination #store byte (low-order) in source register into RAM destination load immediate: li register_destination, value #load immediate value into destination register example: var1:.word 23 # declare storage for var1; initial value is 23 start: $t0, var1 # load contents of RAM location into register $t0: $t0 = var1 li $t1, 5 # $t1 = 5 ("load immediate") $t1, var1 # store contents of register $t1 into RAM: var1 = $t1 done

Indirect and Based Addressing Used only with load and store instructions load address: la $t0, var1 copy RAM address of var1 (presumably a label defined in the program) into register $t0 indirect addressing: $t2, ($t0) load word at RAM address contained in $t0 into $t2 $t2, ($t0) store word in register $t2 into RAM at address contained in $t0 based or indexed addressing: $t2, 4($t0) load word at RAM address ($t0+4) into register $t2 "4" gives offset from address in register $t0 $t2, -12($t0) store word in register $t2 into RAM at address ($t0-12) negative offsets are fine Note: based addressing is especially useful for: arrays; access elements as offset from base address stacks; easy to access elements at offset from stack pointer or frame pointer example array1:.space 12 # declare 12 bytes of storage to hold array of 3 integers start: la $t0, array1 # load base address of array into register $t0 li $t1, 5 # $t1 = 5 ("load immediate") $t1, ($t0) # first array element set to 5; indirect addressing li $t1, 13 # $t1 = 13 $t1, 4($t0) # second array element set to 13 li $t1, -7 # $t1 = -7 $t1, 8($t0) # third array element set to -7 done Arithmetic Instructions most use 3 operands all operands are registers; no RAM or indirect addressing operand size is word (4 bytes) add $t0,$t1,$t2 # $t0 = $t1 + $t2; add as signed (2's complement) integers sub $t2,$t3,$t4 # $t2 = $t3 Ð $t4 addi $t2,$t3, 5 # $t2 = $t3 + 5; "add immediate" (no sub immediate) addu $t1,$t6,$t7 # $t1 = $t6 + $t7; add as unsigned integers subu $t1,$t6,$t7 # $t1 = $t6 + $t7; subtract as unsigned integers mult $t3,$t4 # multiply 32-bit quantities in $t3 and $t4, and store 64-bit # result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4 div $t5,$t6 # Lo = $t5 / $t6 (integer quotient) # Hi = $t5 mod $t6 (remainder) mfhi $t0 # move quantity in special register Hi to $t0: $t0 = Hi mflo $t1 # move quantity in special register Lo to $t1: $t1 = Lo # used to get at result of product or quotient move $t2,$t3 # $t2 = $t3 Control Structures Branches comparison for conditional branches is built into instruction b target # unconditional branch to program label target beq $t0,$t1,target # branch to target if $t0 = $t1 blt $t0,$t1,target # branch to target if $t0 < $t1 ble $t0,$t1,target # branch to target if $t0 <= $t1 bgt $t0,$t1,target # branch to target if $t0 > $t1

bge $t0,$t1,target # branch to target if $t0 >= $t1 bne $t0,$t1,target # branch to target if $t0 <> $t1 Jumps j target # unconditional jump to program label target jr $t3 # jump to address contained in $t3 ("jump register") Subroutine Calls subroutine call: "jump and link" instruction jal sub_label # "jump and link" copy program counter (return address) to register $ra (return address register) jump to program statement at sub_label subroutine return: "jump register" instruction jr $ra # "jump register" jump to return address in $ra (stored by jal instruction) Note: return address stored in register $ra; if subroutine will call other subroutines, or is recursive, return address should be copied from $ra onto stack to preserve it, since jal aays places return address in this register and hence will overwrite previous value System Calls and I/O (SPIM Simulator) used to read or print values or strings from input/output window, and indicate program end use operating system routine call first supply appropriate values in registers $v0 and $a0-$a1 result value (if any) returned in register $v0 The following table lists the possible services. Service Code in $v0 Arguments print_int 1 $a0 = integer to be printed print_float 2 $f12 = float to be printed print_double 3 $f12 = double to be printed print_string 4 $a0 = address of string in memory Results read_int 5 integer returned in $v0 read_float 6 float returned in $v0 read_double 7 double returned in $v0 read_string 8 $a0 = memory address of string input buffer $a1 = length of string buffer (n) sbrk 9 $a0 = amount address in $v0 exit 10 The print_string service expects the address to start a null-terminated character string. The directive.asciiz creates a null-terminated character string. The read_int, read_float and read_double services read an entire line of input up to and including the newline character. The read_string service has the same semantices as the UNIX library routine fgets. It reads up to n-1 characters into a buffer and terminates the string with a null character. If fewer than n-1 characters are in the current line, it reads up to and including the newline and terminates the string with a null character. The sbrk service returns the address to a block of memory containing n additional bytes. This would be used for dynamic memory allocation. The exit service stops a program from running. e.g. Print out integer value contained in register $t2 li $v0, 1 # load appropriate system call code into register $v0; # code for printing integer is 1 move $a0, $t2 # move integer to be printed into $a0: $a0 = $t2 # call operating system to perform operation e.g. Read integer value, store in RAM location with label int_value (presumably declared in data section) li $v0, 5 # load appropriate system call code into register $v0; # code for reading integer is 5 # call operating system to perform operation $v0, int_value # value read from keyboard returned in register $v0; # store this in desired location e.g. Print out string (useful for prompts) string1.asciiz "Print this.\n" # declaration for string variable, #.asciiz directive makes string null terminated main: li $v0, 4 # load appropriate system call code into register $v0;

# code for printing string is 4 la $a0, string1 # load address of string to be printed into $a0 # call operating system to perform print operation e.g. To indicate end of program, use exit system call; thus last lines of program should be: li $v0, 10 # system call code for exit = 10 # call operating sys