Mips Code Examples Peter Rounce

Similar documents
MIPS Instruction Reference

Examples of branch instructions

The MIPS Instruction Set Architecture

MIPS Reference Guide

Flow of Control -- Conditional branch instructions

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

F. Appendix 6 MIPS Instruction Reference

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

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

CS3350B Computer Architecture MIPS Instruction Representation

MIPS%Assembly% E155%

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

Computer Architecture. The Language of the Machine

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

MIPS Instruction Set

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

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

MIPS Instruction Format

SPIM Instruction Set

EE108B Lecture 3. MIPS Assembly Language II

Midterm. CS64 Spring Midterm Exam

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

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

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

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

M2 Instruction Set Architecture

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

Processor. Han Wang CS3410, Spring 2012 Computer Science Cornell University. See P&H Chapter , 4.1 4

Computer Architecture

Project Part A: Single Cycle Processor

Week 10: Assembly Programming

Anne Bracy CS 3410 Computer Science Cornell University. [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

ECE 30 Introduction to Computer Engineering

Computer Architecture Experiment

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

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

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

CSc 256 Midterm 2 Fall 2011

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

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

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

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

Exam in Computer Engineering

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC)

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

CSc 256 Midterm (green) Fall 2018

CSc 256 Midterm 2 Spring 2012

INSTRUCTION SET COMPARISONS

Concocting an Instruction Set

Computer Architecture. MIPS Instruction Set Architecture

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

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

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

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

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

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

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

MIPS Assembly Language. Today s Lecture

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

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

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

A General-Purpose Computer The von Neumann Model. Concocting an Instruction Set. Meaning of an Instruction. Anatomy of an Instruction

MIPS Instruction Set Architecture (2)

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

CS 4200/5200 Computer Architecture I

Assembly Programming

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

Lecture 5: Procedure Calls

Lecture 4: MIPS Instruction Set

TSK3000A - Generic Instructions

EE 361 University of Hawaii Fall

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

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

RTL Model of a Two-Stage MIPS Processor

Chapter 2A Instructions: Language of the Computer

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

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

Concocting an Instruction Set

Thomas Polzer Institut für Technische Informatik

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

CS222: MIPS Instruction Set

Unsigned Binary Integers

Unsigned Binary Integers

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

Course Administration

Announcements. EE108B Lecture MIPS Assembly Language III. MIPS Machine Instruction Review: Instruction Format Summary

ICS 233 COMPUTER ARCHITECTURE. MIPS Processor Design Multicycle Implementation

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

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

Concocting an Instruction Set

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

ECE260: Fundamentals of Computer Engineering


ECE232: Hardware Organization and Design

COMPUTER ORGANIZATION AND DESIGN

ECE 15B Computer Organization Spring 2010

Transcription:

Mips Code Examples Peter Rounce P.Rounce@cs.ucl.ac.uk

Some C Examples Assignment : int j = 10 ; // space must be allocated to variable j Possibility 1: j is stored in a register, i.e. register $2 then the MIPS assembler for this is :- addi $2, $0, 10 : $2 <- $0 + sign-extend[10] Possibility 2: j is stored in memory, i.e. memory 0x12345678 then the MIPS assembler for this might be:- lui $1, 0x1234 : $1 0x12340000 ori $1, $1, 0x5678 : $1 0x12345678 addi $8, $0, 10 : $8 $0 + sign-extend[10] Get address in $1 Get 10 in $8 sw $8, 0($1) : Mem[$1 + 0] $8 Store 100x12345678

Program to calculate Absolute value of difference between 2 input numbers: A - B (demonstrates if) Program reads A from 4 bytes of memory starting at address 12345670 16. Program reads B from 4 bytes of memory starting at address 12345674 16. Program writes A-B to 4 bytes of memory starting at address 12345678 16. Assembler lui $10, 0x1234 ori $10, $10, 0x5670 lw $4, 0($10) lw $5, 4($10) sub $12, $5, $4 bgez $12,+1 sub $12, $4, $5 sw $12, 8($10) # Comment # put address of A into register $10 # read A from memory into register $4 # read B from memory into register $5 (A address+4) # subtract A from B => B-A into register $12 # branch if B-A is positive to sw instruction # subtract B from A => A-B into register $12 # store register $12 value, A-B, into memory N.B. program uses displacement to access other locations from address of memory storing value of A

code in $4: in $5: R s bits in $6: in $6: Given the binary for an instruction e.g.: 10101101111010001000000000000000 What code would you write to get the r s register number into a register on its own, and into the low bits of this register? 10101101111010001000000000000000 00000011111000000000000000000000 00000001111000000000000000000000 Shift right logical 00000000000000000000000000001111 What is wanted. R s bits and Code: assume code in $4 // get masking value in $5 lui $5, 0x03e0 and $6, $5, $4 // masked value in $6 // so shift $6 right srl $6, $6, 21

Change r s field in instruction to value 21 10 (10101 2 ):- Code: 10101101111010001000000000000000 code R s bits in $4: in $5: in $6: in $5: in $5: in $6: 10101101111010001000000000000000 11111100000111111111111111111111 10101100000010001000000000000000 00000000000000000000000000010101 00000010101000000000000000000000 10101110101010001000000000000000 and or Code: assume code in $4 // get masking value in $5 lui ori $5, 0xfc1f $5, $5, 0xffff and $6, $5, $4 // new value into $5 addiu $5, $0, 0x15 sll $5, $5, 21 or $6, $6, $5 What is wanted.

Shift Instructions: Shift left logical: sll rd, rt, shift-amount rd rt << shift-amount : 0s placed on right Example: Let $4 == 2, then sll $5, $4, 3 shifts the contents of $4 left 3 places: (2<<3) 16 which is stored in $5. 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1:shift left 1 2:shift left 1 3:shift left 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0

shift left logical variable: sllv rd, rt, rs : rs holds shift- amount for shifting rt with result into rd : rd rt << rs shift right logical: reverse of shift left logical srl rd, rt, shift-amount : 0s placed on left e.g. $5 = 16 then srl $6, $5, 3 : $6 16 >> 3 $6 == 2 after instruction shift right logical variable: srlv rd, rt, rs as sllv but shift right Shift right arithmetic: shift right with sign duplication shift right arithmetic: sra rd, rt, shift-amount shift right arithmetic variable: srav rd, rt, rs arithmetic shifts duplicate the sign bit : 1s are placed on right for -ve values 1111110000 (>> 2) 1111111100 0011110000 (>> 2) 0000111100

Branches - a Reminder!!!!! Instructions are always 4 bytes long in Mips. Instructions are always stored at addresses that are an integer multiple of 4:- 0, 4, 8, 0x2C, 0x30,. 0x12345678, 0x1234567C.. pc always points at an instruction, i.e. pc always holds a multiple of 4 Branches always change pc by a multiple of 4 Branch offset is number of instructions to branch, not number of addresses! Branch target address calculation:- pc + (offset *4)

Conditional Branch Instructions using labels calculating offsets is difficult use a label instead! you write this Branch Equal assembler calculates this beq rs, rt, Label : if rs == rt pc <- pc + (address of label pc) : if rs == rt pc <- pc + offset*4 Branch Not-Equal bne reg1, reg2, Label : if rs!= rt pc <- pc + (address of label pc) : if rs!= rt pc <- pc + offset*4 Assembler Program calculates difference between address of instruction following the branch and the address of Label (label address pc), divides by 4 and stores this value, the number of insructions to branch, in offset field of instruction. 6 Bits 5 Bits 5 Bits 16-bit op rs rt offset

Other Branches These branches test the contents of a single register against 0. branch on greater than or equal zero: bgez register, label : if (register >= 0) pc address of label : if (register >= 0) pc pc + offset*4 branch on greater than zero: bgtz register, label : if (register > 0) pc address of label : if (register > 0) pc pc + offset*4 branch on less than or equal zero: blez register, label : if (register <= 0) pc address of label : if (register <= 0) pc pc + offset*4 branch on less than zero: bltz register, label : if (register < 0) pc address of label : if (register > 0) pc pc + offset*4 Note: branches can only go 32768 instructions back & 32767 forward memory address space in Mips is 1G instructions!!!!!!!!!!

What about comparing 2 registers for < and >=? Use a Set instruction followed by a conditional branch. Comparison Instructions R-Format versions: compare 2 register and put result into 3 rd register Set less than (signed): slt rd, rs, rt : if rs<rt set rd=1 else set rd=0 Set less than unsigned: sltu rd, rs, rt : if rs<rt set rd=1 else set rd=0 I-Format versions: compare register and constant, put result into 2 nd register Set less than immediate (signed): slti rd, rs, imm : if rs<imm set rd=1 else set rd=0 Set less than unsigned immediate: sltui rd, rs, imm : if rs<imm set rd=1 else set rd=0 The immediate value, (imm), is 16-bits and is sign-extended to 32 bits before comparison. Use beq or bne against reg $0 to test result register rd after set.

MIPS for loop example Setting the elements of an array to zero Data declarations:- unsigned i ; int array[10] ; N.B. C creates the space for both these automatically no new required. for (i=0; i<10; i++) { array[i] = 0 ; }

i=0 MIPS for loop example Let the variable i be stored in register $4 Let int array start at address 12345678 16 Each integer occupies 4 addresses Use $8 and $9 for temporary storage of intermediate values add $4, $0, $0 : set $4=0 : 0 i loop: slti $8, $4, 10 : set $8=1 if $4 < 10 otherwise $8=0 beq $8, $0, end : if $8=0 ($4>=10) branch to end label lui $8, 0x1234 : $8 0x12340000 ori $8, $8, 0x5678 : $8 $8 0x5678 : $8 =0x12345678 sll $9, $4, 2 : $9 $4 << 2 : $9 i*4 add $8, $8, $9 : form address of array[i] in $8 sw $0, 0($8) : store 32-bits of zero from $0 into array[i] addui $4, $4, 1 : i++ i++ end: beq $0, $0, loop : branch to label loop - always branches i<10

array 0 0 0 0 0 0 0 0 0 0 0x12345678 0x12345678+0 0x12345678+4 0x12345678+8 0x12345678+0xc 0x12345678+0x10 0x12345678+0x14 0x12345678+0x18 0x12345678+0x1C 0x12345678+0x20 0x12345678+0x24

MIPS for loop example Setting the elements of an array to zero, but using pointers to memory addresses! Data declarations (C code NOT Java!!!):- Variable ap is of type pointer to integer and will hold an address (a pointer in C) unsigned i ; int array[10] ; int *ap ; ap = array ; // put the address of array into ap for (i=0; i<10; i++) { } *ap = 0 ; // store 0 in the location pointed to by ap ap++ ; // increment the address in ap by 4 // ap now points at the next element of array

array 0 0 0 0 0 0 0 0 0 0 ap 0x12345678 0x1234567C 0x12345680 0x12345684 0x12345688 0x1234568C 0x12345690 0x12345694 0x12345698 0x1234569C

i=0 Let the variable i be stored in register $4, and variable ap in $6 Let array of integers be stored at addresses 12345678 16-1234569F 16 lui $6, 0x1234 : $6 <- 0x12340000 ori $6, $6, 0x5678 : $6 <- $6 0x5678 : $6 =0x12345678 add $4, $0, $0 : set $4=0 : 0 i loop: slti $8, $4, 10 : set $8=1 if $4 < 10 otherwise 0 beq $8, $0, end : if $8=0 ($4>=10) branch to end label sw $0, 0($6) : store 32-bits of zero in $0 into array[i] *ap=0 ap++ i++ end: MIPS for loop example Use $8 for temporary storage addui $6, $6, 4 : ap++; add4 to $6 to point to array[i+1] addui $4, $4, 1 : i++ ; increment loop variable beq $0, $0, loop : branch to label loop - always branches ap = array i<10

Other instructions that change the PC: jump register : jr rs : pc <- rs : register contents into pc Register value must be multiple of 4 (or processor stops) pc can be set to anywhere in memory (greater range than branches). This is used to perform function return, e.g. jr $31, N.B. Jumps can go a greater distance than branches. However jumps are never conditional unlike branches. Both are therefore necessary. jump and link register : jalr rs, rd : rd <- pc ; pc <- rs pc saved to register rd and then rs written into pc Used for function (method) calls to anywhere in the address space.

Function (Method or Subroutine) Call Some lines of program 0x0001AB2C 0x0001AB30 0x0001AB34 lui $1, 0x04 ; $1 <- 0x00040000 0x0001AB38 ori $1, 0x5678 ; $1 <- 0x00045678 0x0001AB3C jalr $1, $31 ; pc -> $31, $1->PC i.e.0x0001ab40->$31 0x00045678 -> PC Function call 0x0001AB40 Function return Have to be sure that $31 has the value store by the jalr when the jr is executed! Code of method 0x00045678 0x0004567C 0x00045680 0x00045684 0x00045688 jr $31 ; $31->PC i.e.0x01ab40 -> PC

Jump Instructions - J Format 6 Bits op 26 Bits target jump to target : j target : pc[bits 27:0]target*4 jump and link target : jal target register 31 <- contents of pc ; pc[bits 27:0]target*4 In both cases lower 28 bits of PC register are loaded with (26 bits of target field * 4) jal is a method call instruction saving the PC before changing it. Detail : pc is always an integer multiple of 4: therefore value stored in target field of instruction for j and jal is target address divided by 4, i.e. least 2 bits are dropped, since they are always 00. Note: the upper 4-bits of PC are unchanged by these instructions.