MIPS and Basic Assembly Language CS 64: Computer and Design Logic Lecture #4

Similar documents
MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Welcome to Computer Organization and Design Logic

Binary Arithme-c 2 Intro to Assembly CS 64: Computer Organiza-on and Design Logic Lecture #3

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

Dynamic Arrays Makefiles and Mul3ple File Compiles

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Lecture # 1. SPIM & MIPS Programming

Welcome to Solving Problems with Computers I

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Software and Documentation

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

SPIM & MIPS Programming

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Computer Systems and Networks

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Lecture 6 Decision + Shift + I/O

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Python Lists and Dictionaries CS 8: Introduction to Computer Science, Winter 2019 Lecture #13

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Computer Systems and Networks

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

ECE2049 E17 Lecture 4 MSP430 Architecture & Intro to Digital I/O

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

More Examples Using Lists Tuples and Dictionaries in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #11

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

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

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Assembly Language Programming

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

SPIM & MIPS. Department of Information and Management. Computer Organization and Structure 年 9 月 29 日星期 一

Homework #2 Assembly Programming

Reversing. Time to get with the program

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

CS 64 Week 0 Lecture 1. Kyle Dewey

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

ECE 375: Computer Organization and Assembly Language Programming

CS61C : Machine Structures

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

CS 61c: Great Ideas in Computer Architecture

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

Outline. policies. with some potential answers... MCS 260 Lecture 19 Introduction to Computer Science Jan Verschelde, 24 February 2016

CENG3420 Lab 1-1: MIPS assembly language programing

CSC 220: Computer Organization Unit 12 CPU programming

Lab 2 Building on Linux

2 Introduction to Processes

Administrivia. Minute Essay From 4/11

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

CS187 - Science Gateway Seminar for CS and Math

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

Lecture 9: Disassembly

CS 152 Computer Architecture and Engineering Lecture 1 Single Cycle Design

Floating Point. What can be represented in N bits? 0 to 2N-1. 9,349,398,989,787,762,244,859,087, x 1067

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

9/9/12. New- School Machine Structures (It s a bit more complicated!) CS 61C: Great Ideas in Computer Architecture IntroducMon to Machine Language

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to Machine Language

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

Many ways to build logic out of MOSFETs

Lecture 14: more class, C++ streams

1. Introduction EE108A. Lab 1: Combinational Logic: Extension of the Tic Tac Toe Game

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

INTRODUCTION TO MICROSOFT EXCEL: DATA ENTRY AND FORMULAS

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

Pre- Defined Func-ons in C++ Review for Midterm #1

CS354 gdb Tutorial Written by Chris Feilbach

GET TO KNOW YOUR HOME PHONE

COMP 122/L Lecture 1. Kyle Dewey

Review. Lecture 18 Running a Program I aka Compiling, Assembling, Linking, Loading

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

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

CS 61C: Great Ideas in Computer Architecture Direct- Mapped Caches. Increasing distance from processor, decreasing speed.

Communication Patterns

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Tips from the experts: How to waste a lot of time on this assignment

Transcription:

MIPS and Basic Assembly Language CS 64: Computer Organiza@on and Design Logic Lecture #4 Ziad Matni Dept. of Computer Science, UCSB

This Week on Didja Know Dat?! 1/25/18 Matni, CS64, Wi18 2

Lecture Outline MIPS core processing blocks Basic programming in assembly ArithmeLc programs Control logic programs 1/25/18 Matni, CS64, Wi18 3

Any QuesLons From Last Lecture? 1/25/18 Matni, CS64, Wi18 4

MIPS 1/25/18 Matni, CS64, Wi18 5

Why MIPS? MIPS: a reduced instruclon set computer (RISC) architecture developed by MIPS Technologies (1981) Relevant in the embedded systems domain Computer systems with a dedicated funclon within a larger mechanical or electrical system Example: low power and low cost processors in MP3 players or Smart Appliances All modern commercial processors share the same core concepts as MIPS, just with extra stuff...but most importantly... 1/25/18 Matni, CS64, Wi18 6

MIPS is Simpler than other instruclon sets for CPUs So it s a great learning tool Dozens of instruclons (as opposed to hundreds) Lack of redundant instruclons or special cases 5 stage pipeline versus 24 stages Pipelining is a design in CPUs that allows mullple instruclons to go on at once (a.k.a instruclon- level parallelism) on one CPU 1/25/18 Matni, CS64, Wi18 7

Code on MIPS Original x = 5; y = 7; z = x + y; MIPS li $t0, 5 li $t1, 7 add $t3, $t0, $t1 1/25/18 Matni, CS64, Wi18 8

Code on MIPS Original x = 5; y = 7; z = x + y; MIPS li $t0, 5 li $t1, 7 add $t3, $t0, $t1 load immediate: put the given value into a register $t0: temporary register 0 1/25/18 Matni, CS64, Wi18 9

Code on MIPS Original x = 5; y = 7; z = x + y; MIPS li $t0, 5 li $t1, 7 add $t3, $t0, $t1 load immediate: put the given value into a register $t1: temporary register 1 1/25/18 Matni, CS64, Wi18 10

Code on MIPS Original x = 5; y = 7; z = x + y; MIPS li $t0, 5 li $t1, 7 add $t3, $t0, $t1 add: add the rightmost registers, pueng the result in the first register $t3: temporary register 3 1/25/18 Matni, CS64, Wi18 11

Available Registers in MIPS 32 registers in all Refer to your MIPS Reference Card For the moment, let s only consider registers $t0 thru $t9 1/25/18 Matni, CS64, Wi18 12

Assembly The code that you see is MIPS assembly li $t0, 5 li $t1, 7 add $t3, $t0, $t1 Assembly is *almost* what the machine sees. For the most part, it is a direct translalon to binary from here (known as machine code) An assembler takes assembly code and changes it into the actual 1 s and 0 s for machine code Analogous to a compiler for HL code 1/25/18 Matni, CS64, Wi18 13

Machine Code/Language This is what the process actually executes and accepts as input Each instruclon is represented with 32 bits Three different instruclon formats (R, I, J). For the moment, we ll only look at the R format 1/25/18 Matni, CS64, Wi18 14

1/25/18 Matni, CS64, Wi18 15

1/25/18 Matni, CS64, Wi18 16

1/25/18 Matni, CS64, Wi18 17

1/25/18 Matni, CS64, Wi18 18

1/25/18 Matni, CS64, Wi18 19

1/25/18 Matni, CS64, Wi18 20

1/25/18 Matni, CS64, Wi18 21

1/25/18 Matni, CS64, Wi18 22

1/25/18 Matni, CS64, Wi18 23

1/25/18 Matni, CS64, Wi18 24

1/25/18 Matni, CS64, Wi18 25

Adding More FuncLonality What about: display results???? Yes, that s kinda important What would this entail? Engaging with Input / Output part of the computer i.e. talking to devices Q: What usually handles this? A: the opera@ng system So we need a way to tell the operalng system to kick in 1/25/18 Matni, CS64, Wi18 26

Talking to the OS We are going to be running on MIPS emulators called SPIM through a program called QtSPIM (GUI based) We re not actually running our commands on a MIPS processor!!! so, in other words we re faking it Ok, so how might we print something? 1/25/18 Matni, CS64, Wi18 27

SPIM RouLnes MIPS features a syscall instruclon, which triggers a so*ware interrupt, or excep3on Outside of an emulator (i.e. in the real world), these pause the program and tell the OS to check something Inside the emulator, it tells the emulator to check something 1/25/18 Matni, CS64, Wi18 28

syscall So we have the OS/emulator s alenlon, but how does it know what we want? The OS/emulator has access to the registers Put special values in the registers to indicate what you want These are codes that can t be used for anything else, so they re understood to be just for syscall 1/25/18 Matni, CS64, Wi18 29

Program Files for MIPS Assembly The files have to be text Typical file extension type is.asm To leave comments, use # at the start of the line 1/25/18 Matni, CS64, Wi18 30

(Finally) PrinLng an Integer For SPIM, if register $v0 contains 1, then it will print whatever integer is stored in register $a0 ß this is a specific rule Other values in $v0 indicate other types to syscall Example $v0 = 3 means double, $v0 = 4 means string Remember, the usual syntax to load immediate a value into a register is: li <register>, <value> To make sure that the register $a0 has the value of what you want to print out, use the move command: move <to register>, <from register> Note that $v0 and $a0 are dislnct from $t0 - $t9 1/25/18 Matni, CS64, Wi18 31

Ok So About Those Registers MIPS has 32 registers, each 32 bits 1/25/18 Matni, CS64, Wi18 32

AugmenLng with PrinLng # Main program li $t0, 5 li $t1, 7 add $t3, $t0, $t1 # Print an integer to std.output li $v0, 1 move $a0, $t3 syscall 1/25/18 Matni, CS64, Wi18 33

ExiLng an Assembly Program in SPIM If you are using SPIM, then you need to say when you are done as well Like a return 0; statement in C++ How is this done? Issue a syscall with a special value in $v0 = 10 (decimal) 1/25/18 Matni, CS64, Wi18 34

AugmenLng with ExiLng # Main program li $t0, 5 li $t1, 7 add $t3, $t0, $t1 # Print to standard output li $v0, 1 move $a0, $t3 syscall # End program li $v0, 10 syscall 1/25/18 Matni, CS64, Wi18 35

Examples We ve Seen So Far MIPS System Services stdout stdin File I/O 1/25/18 Matni, CS64, Wi18 36

Now Let s Make it a Full Program (almost) We need to tell the assembler (and its simulator) which bits should be placed where in memory Bits? (remember: everything ends up being a bunch of 1 s and 0 s!) Allocated as program RUN Constants to be used in the program (like strings) Allocated at program LOAD mutable global variables the text of the program 1/25/18 Matni, CS64, Wi18 37

Marking the Code For the simulator, you ll need a.text direclve to specify code Allocated as program RUN.text # Main program li $t0, 5 li $t1, 7 add $t3, $t0, $t1 # Print to standard output li $v0, 1 move $a0, $t3 syscall # End program li $v0, 10 syscall Constants to be used in the program (like strings) Allocated at program LOAD mutable global variables the text of the program 1/25/18 Matni, CS64, Wi18 38

The move instruclon The move instruclon does not actually show up in SPIM It is a pseudo- instruc3on for us to use, but it s actually translated into an actual instruclon ORIGINAL: move $a0, $t3 ACTUAL: addu $a0, $zero, $t3 # what s addu? what s $zero? 1/25/18 Matni, CS64, Wi18 39

Why Pseudocodes? And what s this $zero?? $zero Specified like a normal register, but does not behave like a normal register Writes to $zero are not saved Reads from $zero always return 0 value Why have move as a pseudo- instruc@on instead of as an actual instruclon? It s one less instruclon to worry about One design goal of RISC is to cut out redundancy move isn t the only one! li is another one too! 1/25/18 Matni, CS64, Wi18 40

List of all Core Instruc@ons in MIPS 1/25/18 Matni, CS64, Wi18 41

List of the Arithme@c Core Instruc@ons in MIPS 1/25/18 Matni, CS64, Wi18 42

List of all PsuedoInstruc@ons in MIPS ALL OF THIS AND MORE IS ON YOUR HANDY MIPS REFERENCE CARD NOW FOUND ON THE CLASS WEBSITE 1/25/18 Matni, CS64, Wi18 43

MIPS Peculiarity: NOR used a NOT How to make a NOT funclon using NOR instead Recall: NOR = NOT OR Truth- Table: A B A NOR B 0 0 0 1 1 0 1 1 1 0 0 0 Note that: 0 NOR x = NOT x So, in the absence of a NOT funclon, use a NOR with a 0 as one of the inputs! 1/25/18 Matni, CS64, Wi18 44

Let s Run This Program Already! Using SPIM We ll call it simpleadd.asm Run it on CSIL as: $ spim f simpleadd.asm We ll also run other arithmelc programs and explain them as we go along TAKE NOTES! 1/25/18 Matni, CS64, Wi18 45

YOUR TO- DOs Assignment/Lab #2 Due on MONDAY, 1/29, by 11:59 PM Submit it via turnin Assignment/Lab #3 Will post online on WEDNESDAY eve Your lab is on THURSDAY Will be due on FRIDAY 1/25/18 Matni, CS64, Wi18 46

1/25/18 Matni, CS64, Wi18 47