MIPS Assembly Language

Similar documents
TSK3000A - Generic Instructions

SPIM Instruction Set

MIPS Instruction Format

The MIPS R2000 Instruction Set

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

MIPS Reference Guide

Adventures in Assembly Land

Computer Architecture. The Language of the Machine

F. Appendix 6 MIPS Instruction Reference

MIPS Instruction Reference

MIPS Instruction Set

Introduction to MIPS Processor

SPIM S20: A MIPS R2000 Simulator

The MIPS Instruction Set Architecture

M2 Instruction Set Architecture

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

Computer Architecture. MIPS Instruction Set Architecture

Reduced Instruction Set Computer (RISC)

MIPS Assembly Language. Today s Lecture

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

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

Question 0. Do not turn this page until you have received the signal to start. (Please fill out the identification section above) Good Luck!

Reduced Instruction Set Computer (RISC)

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

CSc 256 Midterm (green) Fall 2018

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

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

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

MIPS Assembly Language Programming

Q1: /30 Q2: /25 Q3: /45. Total: /100

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

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

MIPS Architecture and Assembly Language Overview

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

Examples of branch instructions

CSc 256 Midterm 2 Spring 2012

CSc 256 Midterm 2 Fall 2011

Compiling Techniques

ECE Exam I February 19 th, :00 pm 4:25pm

Week 10: Assembly Programming

Arithmetic for Computers

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

Flow of Control -- Conditional branch instructions

Computer Systems and Architecture

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

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

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.

ISA: The Hardware Software Interface

CSc 256 Final Fall 2016

MIPS Assembly Language Guide

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

Number Systems and Their Representations

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

ECE 15B Computer Organization Spring 2010

CS 61c: Great Ideas in Computer Architecture

MIPS Assembly Language Programming

MIPS Assembly Language Programming

ece4750-parc-isa.txt

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

CPS311 - COMPUTER ORGANIZATION. A bit of history

Outline. EEL-4713 Computer Architecture Multipliers and shifters. Deriving requirements of ALU. MIPS arithmetic instructions

INSTRUCTION SET COMPARISONS

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

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Lec 10: Assembler. Announcements

Programming the processor

Assembly Programming

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

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

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

CSc 256 Midterm 1 Fall 2011

Shift and Rotate Instructions

Reminder: tutorials start next week!

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

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

EE 109 Unit 8 MIPS Instruction Set

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

EE 109 Unit 13 MIPS Instruction Set. Instruction Set Architecture (ISA) Components of an ISA INSTRUCTION SET OVERVIEW

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

MIPS Assembly Programming

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Three 10 April 2013

Mark Redekopp, All rights reserved. EE 352 Unit 3 MIPS ISA

Mips Code Examples Peter Rounce

MIPS%Assembly% E155%

Assembly Language. Prof. Dr. Antônio Augusto Fröhlich. Sep 2006

A Processor. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter , 4.1-3

SPIM S20: A MIPS R2000 Simulator

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

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CS153: Compilers Lecture 2: Assembly

Anne Bracy CS 3410 Computer Science Cornell University. See P&H Chapter: , , Appendix B

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

SPIM S20: A MIPS R2000 Simulator

Midterm. Sticker winners: if you got >= 50 / 67

SPIM S20: A MIPS R2000 Simulator

ICS 233 COMPUTER ARCHITECTURE. MIPS Processor Design Multicycle Implementation

RISC-V Assembly and Binary Notation

MACHINE LANGUAGE. To work with the machine, we need a translator.

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 9 December 2015

Transcription:

MIPS Assembly Language Chapter 15 S. Dandamudi Outline MIPS architecture Registers Addressing modes MIPS instruction set Instruction format Data transfer instructions Arithmetic instructions Logical/shift/rotate/compare instructions Branch and jump instructions SPIM system calls SPIM assembler directive Illustrative examples Procedures Stack implementation Illustrative examples S. Dandamudi Chapter 15: Page 2 1

MIPS Processor Architecture MIPS follows RISC principles much more closely than PowerPC and Itanium Based on the load/store architecture Registers 32-general purpose registers ($0 $31)» $0 hardwired to zero» $31 used to store return address Program counter (PC)» Like IP in Pentium Two special-purpose registers (HI and LO)» Used in multiply and divide instructions S. Dandamudi Chapter 15: Page 3 MIPS Processor Architecture (cont d) S. Dandamudi Chapter 15: Page 4 2

MIPS Processor Architecture (cont d) MIPS registers and their conventional usage S. Dandamudi Chapter 15: Page 5 MIPS Processor Architecture (cont d) MIPS addressing modes Bare machine supports only a single addressing mode disp(rx) Virtual machine provides several additional addressing modes S. Dandamudi Chapter 15: Page 6 3

Memory Usage Placement of segments allows sharing of unused memory by both data and stack segments S. Dandamudi Chapter 15: Page 7 Instruction Format load, arithmetic/logical with immediate operands Higher order bits from PC are added to get absolute address S. Dandamudi Chapter 15: Page 8 4

MIPS Instruction Set Data transfer instructions Load and store instructions have similar format ld Rdest,address» Moves a byte from address to Rdest as a signed number Sign-extended to Rdest»Use ldu for unsigned move (zero-extended) Use lh, lhu, ld for moving halfwords (signed/unsigned) and words Pseudoinstructions la Rdest,address li Rdest,imm» Implemented as ori Rdest,$0,imm S. Dandamudi Chapter 15: Page 9 MIPS Instruction Set (cont d) Store byte sb Rsrc,address»Use sh and sw for halfwords and words Pseudoinstruction move Rdest,Rsrc» Copies Rsrc to Rdest Four additional data movement instructions are available» Related to HI and LO registers» Used with multiply and divide instructions Discussed later S. Dandamudi Chapter 15: Page 10 5

MIPS Instruction Set (cont d) Arithmetic instructions Addition add Rdest,Rsrc1,Rsrc2 Rdest Rsrc1 + Rsrc2 Numbers are treated as signed integers Overflow: Generates overflow exception Use addu if the overflow exception is not needed addi Rdest,Rsrc1,imm imm: 16-bit signed number Pseudoinstruction add Rdest,Rsrc1,Src2 Register or imm16 S. Dandamudi Chapter 15: Page 11 Subtract sub MIPS Instruction Set (cont d) Rdest,Rsrc1,Rsrc2 Rdest Rsrc1 Rsrc2 Numbers are treated as signed integers Overflow: Generates overflow exception Use subu if the overflow exception is not needed No immediate version Use addi with negative imm Pseudoinstruction sub Rdest,Rsrc1,Src2 Register or imm16 S. Dandamudi Chapter 15: Page 12 6

MIPS Instruction Set (cont d) Pseudoinstructions neg Rdest,Rsrc Negates Rsrc (changes sign) Implemented as sub Rdest,$0,Rsrc abs Rdest,Rsrc Implemented as bgez Rsrc,skip sub Rdest,$0,Rsrc skip: Constant 8 is used S. Dandamudi Chapter 15: Page 13 MIPS Instruction Set (cont d) Multiply» mult (signed)» multu (unsigned) mult Rsrc1,Rsrc2» 64-bit result in LO and HI registers» Special data move instructions for LO/HI registers mfhi Rdest mflo Rdest Pseudoinstruction mul Rdest,Rsrc1,Rsrc2 32-bit result in Rdest 64-bit result is not available Register or imm S. Dandamudi Chapter 15: Page 14 7

MIPS Instruction Set (cont d) mul is implemented as» If Rsrc2 is a register mult Rsrc1,Src2 mflo Rdest» If Rsrc2 is an immediate value (say 32) ori $1,$0,32 mult $5,$1 mflo $4 a0 = $4 a1 = $5 at = $1 S. Dandamudi Chapter 15: Page 15 MIPS Instruction Set (cont d) Divide» div (signed)» divu (unsigned) div Rsrc1,Rsrc2» Result = Rsrc1/Rsrc2» LO = quotient, HI = remainder» Result undefined if the divisor is zero Pseudoinstruction div Rdest,Rsrc1,Src2 quotient in Rdest rem Rdest,Rsrc1,Src2 remainder in Rdest Register or imm S. Dandamudi Chapter 15: Page 16 8

MIPS Instruction Set (cont d) Logical instructions Support AND, OR, XOR, NOR and Rdest,Rsrc1,Rsrc2 andi Rdest,Rsrc1,imm16 Also provides or, ori, xor, xori, nor No not instruction» It is provided as a pseudoinstruction not Rdest,Rsrc» Implemented as nor Rdest,Rsrc,$0 S. Dandamudi Chapter 15: Page 17 MIPS Instruction Set (cont d) Shift instructions Shift left logical sll Rdest,Rsrc1,count» Vacated bits receive zeros» Shift left logical variable sllv Rdest,Rsrc1,Rsrc2» Shift count in Rsrc2 Two shift right instructions» Logical (srl, srlv) Vacated bits receive zeros» Arithmetic (sra, srav) Vacated bits receive the sign bit (sign-extended) S. Dandamudi Chapter 15: Page 18 9

MIPS Instruction Set (cont d) Rotate instructions These are pseudoinstructions rol Rdest,Rsrc1,Src2 ror Rdest,Rsrc1,Src2» Example: ror $t2,$t2,31 is translated as sll $1,$10,31 srl $10,$10,1 t2 = $10 or $10,$10,$1 S. Dandamudi Chapter 15: Page 19 MIPS Instruction Set (cont d) Comparison instructions All are pseudoinstructions slt Rdest,Rsrc1,Rsrc2»Sets Rdest to 1 if Rsrc1 < Rsrc2» Unsigned version: sltu»others: seq sgt, sgtu sge, sgeu sle, sleu sne S. Dandamudi Chapter 15: Page 20 10

MIPS Instruction Set (cont d) Comparison instructions» Example: seq $a0,$a1,$a2 is translated as beq $6,$5,skip1 ori $4,$0,0 beq $0,$0,skip2 skip1: ori $4,$0,1 skip2: a0 = $4 a1 = $5 a2 = $6 S. Dandamudi Chapter 15: Page 21 MIPS Instruction Set (cont d) Branch and Jump instructions Jump instruction j target» Uses 26-bit absolute address Branch pseudoinstruction b target» Uses 16-bit relative address Conditional branches beq Rsrc1,Rsrc2,target» Jumps to target if Rsrc1 = Rsrc2 S. Dandamudi Chapter 15: Page 22 11

MIPS Instruction Set (cont d) Other branch instructions bne blt, bltu bgt, bgtu ble, bleu bge, bgeu Comparison with zero beqz Rsrc,target» Branches to target if Rsrc = 0»Others bnez, bltz, bgtz, blez, bgez» b target is implemented as bgez $0,target S. Dandamudi Chapter 15: Page 23 SPIM System Calls SPIM supports I/O through syscall Data types:» string, integer, float, double Service code: $v0 Required arguments: $a0 and $a1 Return value: $v0 print_string» Prints a NULL-terminated string read_string» Takes a buffer pointer and its size n» Reads at most n-1 characters in NULL-terminated string» Similar to fgets S. Dandamudi Chapter 15: Page 24 12

SPIM System Calls (cont d) S. Dandamudi Chapter 15: Page 25 SPIM System Calls (cont d).data prompt:.asciiz Enter your name: in-name:.space 31.TEXT... la $a0,prompt li $v0,4 syscall la $a0,in_name li $a1,31 li $v0,8 syscall S. Dandamudi Chapter 15: Page 26 13

SPIM Assembler Directives Segment declaration Code:.TEXT.TEXT <address> Data:.DATA String directives.ascii»not NULL-terminated.ASCIIZ» Null-terminated Uninitialized space.space n Optional; if present, segment starts at that address Example: ASCII This is a very long string ASCII spread over multiple ASCIIZ string statements. S. Dandamudi Chapter 15: Page 27 SPIM Assembler Directives (cont d) Data directives Provides four directives:.half,.word.float,.double.half h1, h2,..., hn Allocates 16-bit halfwords Use.WORD for 32-bit words» Floating-point numbers Single-precision.FLOAT f1, f2,..., fn Use.DOUBLE for double-precision S. Dandamudi Chapter 15: Page 28 14

SPIM Assembler Directives (cont d) Miscellaneous directives Data alignment» Default:.HALF,.WORD,.FLOAT,.DOUBLE align data» Explicit control:.align n aligns the next datum on a 2 n byte boundary» To turn off alignment, use.align 0.GLOBL declares a symbol global.text.globl main main:... S. Dandamudi Chapter 15: Page 29 Illustrative Examples Character to binary conversion binch.asm Case conversion toupper.asm Sum of digits string version addigits.asm Sum of digits number version addigits2.asm S. Dandamudi Chapter 15: Page 30 15

Two instructions Procedure call Procedures» jal (jump and link) jal proc_name Return from a procedure jr $ra Parameter passing Via registers Via the stack Examples» min-_max.asm» str_len.asm S. Dandamudi Chapter 15: Page 31 Stack Implementation No explicit support» No push/pop instructions» Need to manipulate stack pointer explicitly Stack grows downward as in Pentium Example: push registers a0 and ra sub $sp,$sp,8 #reserve 8 bytes of stack sw $a0,0($sp) #save registers sw $ra,4($sp) pop operation lw $a0,0($sp) #restore registers lw $a0,4($sp) addu $sp,$sp,8 #clear 8 bytes of stack S. Dandamudi Chapter 15: Page 32 16

Illustrative Examples Passing variable number of parameters to a procedure var_para.asm Recursion examples Factorial.asm Quicksort.asm Last slide S. Dandamudi Chapter 15: Page 33 17