Implementing Algorithms in MIPS Assembly

Similar documents
Introduction to MIPS Processor

Computer Systems Architecture

TSK3000A - Generic Instructions

Arithmetic for Computers

MIPS Instruction Reference

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

CS 61c: Great Ideas in Computer Architecture

F. Appendix 6 MIPS Instruction Reference

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

MIPS Instruction Format

Assembly Programming

Week 10: Assembly Programming

Lecture 6 Decision + Shift + I/O

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

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

The MIPS Instruction Set Architecture

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

Number Systems and Their Representations

ISA: The Hardware Software Interface

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

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

Topic Notes: MIPS Instruction Set Architecture

MIPS Assembly Language

Reminder: tutorials start next week!

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

Chapter 3. Instructions:

MIPS%Assembly% E155%

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

Chapter 2A Instructions: Language of the Computer

Programming the processor

Chapter 2. Instructions:

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

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

ECE260: Fundamentals of Computer Engineering

R-type Instructions. Experiment Introduction. 4.2 Instruction Set Architecture Types of Instructions

Concocting an Instruction Set

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

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

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

Instruction Set Architecture of. MIPS Processor. MIPS Processor. MIPS Registers (continued) MIPS Registers

ECE260: Fundamentals of Computer Engineering

MIPS Assembly Programming

ECE 15B Computer Organization Spring 2010

CSSE232 Computer Architecture. Logic and Decision Opera:ons

MIPS Instruction Set

Examples of branch instructions

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

ECE232: Hardware Organization and Design

Sparse Notes on an MIPS Processor s Architecture and its Assembly Language

A Model RISC Processor. DLX Architecture

Review of instruction set architectures

CS3350B Computer Architecture MIPS Instruction Representation

Flow of Control -- Conditional branch instructions

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

CS 61c: Great Ideas in Computer Architecture

COMPUTER ORGANIZATION AND DESIGN

CPE 335 Computer Organization. MIPS Arithmetic Part I. Content from Chapter 3 and Appendix B

Adventures in Assembly Land

Instructions: Language of the Computer

SpartanMC. SpartanMC. Instruction Set Architecture

Reduced Instruction Set Computer (RISC)

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

Introduction to Scientific Computing Languages

Unsigned Binary Integers

Unsigned Binary Integers

Concocting an Instruction Set

Course Administration

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

Introduction to Scientific Computing Languages

Concocting an Instruction Set

Chapter Two MIPS Arithmetic

Chapter 2: Instructions:

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

CS222: MIPS Instruction Set

Instruction Set Architecture

Computer Organization and Components

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

SPIM Instruction Set

Review: MIPS Organization

MIPS Assembly Language Programming

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

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

LAB B Translating Code to Assembly

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

MIPS Reference Guide

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

Five classic components

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

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

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

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

Assembly Language Programming

CSc 256 Midterm (green) Fall 2018

CS 61C: Great Ideas in Computer Architecture Running a Program - CALL (Compiling, Assembling, Linking, and Loading)

Reduced Instruction Set Computer (RISC)

Computer Systems and Architecture

CSc 256 Midterm 2 Spring 2012

The MIPS R2000 Instruction Set

Lec 10: Assembler. Announcements

Transcription:

1 / 18 Implementing Algorithms in MIPS Assembly (Part 1) January 28 30, 2013

2 / 18 Outline Effective documentation Arithmetic and logical expressions Compositionality Sequentializing complex expressions Bitwise vs. logical operations

3 / 18 Documenting your code # Author: your name # Date: current date # Description: high-level description of your program.data.text (constant and variable definitions) # Section 1: what this part does # Pseudocode: # (algorithm in pseudocode) # Register mappings: # (mapping from pseudocode variables to registers) Inline comments should relate to the pseudocode description (MARS demo: Circle.asm)

4 / 18 Just to reiterate... (required from here on out) Once at top of file: Header block 1. author name 2. date of current version 3. high-level description of your program Once for each section: Section block 1. section name 2. description of algorithm in pseudocode 3. mapping from pseudocode variables to registers... and inline comments that relate to pseudocode

5 / 18 Outline Effective documentation Arithmetic and logical expressions Compositionality Sequentializing complex expressions Bitwise vs. logical operations

6 / 18 Compositionality Main challenge math expressions are compositional assembly instructions are not compositional Compositionality in math: use expressions as arguments to other expressions example: a * (b + 3) Non-compositionality in assembly: can t use instructions as arguments to other instructions not valid MIPS: mult $t0 (addi $t1 3)

7 / 18 Significance of compositionality (PL aside) Compositionality is extremely powerful and useful leads to high expressiveness can do more with less code leads to nice semantics to understand whole: understand parts + how combined promotes modularity and reuse extract recurring subexpressions Pulpit: less compositional more compositional assembly imperative (C, Java) functional (Haskell)

Sequentializing expressions Goal Find a sequence of assembly instructions that implements the pseudocode expression Limited by available instructions: in math, add any two expressions in MIPS, add two registers, or a register and a constant not all instructions have immediate variants can use li to load constant into register Limited by available registers: might need to swap variables in/out of memory (usually not a problem for us, but a real problem in practice) 8 / 18

9 / 18 Finding the right sequence of instructions Strategy 1: Decompose expression 1. separate expression into subexpressions respect grouping and operator precedence! 2. translate each subexpression and save results 3. combine results of subexpressions (assume C-like operator precedence in pseudocode) Example # Pseudocode: # d = (a+b) * (c+4) # Register mappings: # a: t0, b: t1, c: t2, d: t3 add $t4, $t0, $t1 # tmp1 = a+b addi $t5, $t2, 4 # tmp2 = c+4 mul $t3, $t4, $t5 # d = tmp1 * tmp2 tmp1 = a+b tmp2 = c+4 d = tmp1 * tmp2

10 / 18 Finding the right sequence of instructions Strategy 2: Parse and translate 1. parse expression into abstract syntax tree 2. traverse tree in post-order 3. store subtree results in temp registers This is essentially what a compiler does! Example + d # Pseudocode: # c = a + 3*(b+2) # Register mappings: # a: t0, b: t1, c: t2 addi $t3, $t1, 2 # tmp1 = b+2 mul $t4, $t3, 3 # tmp2 = 3*tmp1 add $t2, $t0, $t4 # c = a + tmp2 tmp2 * a 3 + tmp1 b 2

11 / 18 Optimizing register usage Can often use fewer registers by accumulating results # Pseudocode: # c = a + 3*(b+2) # Register mappings: # a: $t0, b: $t1, c: $t2 # tmp1: $t3, tmp2: $t4 # tmp1 = b+2 # tmp2 = 3*tmp1 # c = a + tmp2 addi $t3, $t1, 2 mul $t4, $t3, 3 add $t2, $t0, $t4 # c = b+2 # c = 3*c # c = a + c addi $t2, $t1, 2 mul $t2, $t2, 3 add $t2, $t0, $t2

12 / 18 Exercise # Pseudocode: # d = a - 3 * (b + c + 8) # Register mappings: # a: t0, b: t1, c: t2, d: t3 addi $t3, $t2, 8 # d = b + c + 8 add $t3, $t1, $t3 li $t4, 3 # d = 3 * d mul $t3, $t4, $t3 sub $t3, $t0, $t3 # d = a - d

Logical expressions In high-level languages, used in conditions of control structures branches (if-statements) loops (while, for) Logical expressions values: True, False boolean operators: not (!), and (&&), or ( ) relational operators: ==,!=, >, >=, <, <= In MIPS: conceptually, False = 0, True = 1 non-relational logical operations are bitwise, not boolean 13 / 18

14 / 18 Bitwise logic operations and $t1, $t2, $t3 # $t1 = $t2 & $t3 (bitwise and) or $t1, $t2, $t3 # $t1 = $t2 $t3 (bitwise or) xor $t1, $t2, $t3 # $t1 = $t2 ^ $t3 (bitwise xor) Example: 0110 op 0011 1 0 1 0 and 0 0 1 1 0 0 1 0 1 iff both are 1 1 0 1 0 or 0 0 1 1 1 0 1 1 1 iff either is 1 1 0 1 0 xor 0 0 1 1 1 0 0 1 1 iff exactly one 1 Immediate variants andi $t1, $t2, 0x0F # $t1 = $t2 & 0x0F (bitwise and) ori $t1, $t2, 0xF0 # $t1 = $t2 0xF0 (bitwise or) xori $t1, $t2, 0xFF # $t1 = $t2 ^ 0xFF (bitwise xor)

15 / 18 Bitwise logic vs. boolean logic For and, or, xor: equivalent when False = 0 and True = 1 not equivalent when False = 0 and True 0! (as in C) Careful: MARS provides a macro instruction for bitwise not this is not equivalent to logical not inverts every bit, so not True 0xFFFFFFFE How can we implement logical not? xori $t1, $t2, 1 # $t1 = not $t2 (logical not)

Relational operations Logical expressions values: True, False boolean operators: not (!), and (&&), or ( ) relational operators: ==,!=, >, >=, <, <= seq $t1, $t2, $t3 # $t1 = $t2 == $t3? 1 : 0 sne $t1, $t2, $t3 # $t1 = $t2!= $t3? 1 : 0 sge $t1, $t2, $t3 # $t1 = $t2 >= $t3? 1 : 0 sgt $t1, $t2, $t3 # $t1 = $t2 > $t3? 1 : 0 sle $t1, $t2, $t3 # $t1 = $t2 <= $t3? 1 : 0 slt $t1, $t2, $t3 # $t1 = $t2 < $t3? 1 : 0 slti $t1, $t2, 42 # $t1 = $t2 < 42? 1 : 0 (MARS provides macro versions of many of these instructions that take immediate arguments) 16 / 18

17 / 18 Exercise # Pseudocode: # c = (a < b) ((a+b) == 10) # Register mappings: # a: t0, b: t1, c: t2 add $t3, $t0, $t1 # tmp = a+b li $t4, 10 # tmp = tmp == 10 seq $t3, $t3, $t4 slt $t2, $t0, $t1 # c = a < b or $t2, $t2, $t3 # c = c tmp

18 / 18 Exercise # Pseudocode: # c = (a < b) && ((a+b) % 3) == 2 # Register mappings: # a: t0, b: t1, c: t2 # tmp1: t3, tmp2: t4 add $t3, $t0, $t1 # tmp1 = a+b li $t4, 3 # tmp1 = tmp1 % 3 div $t3, $t4 mfhi $t3 seq $t3, $t3, 2 # tmp1 = tmp1 == 2 slt $t4, $t0, $t1 # tmp2 = a < b and $t2, $t3, $t4 # c = tmp2 & tmp1