CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 I´ve been getting this a lot lately So, what are you teaching this term? Computer.

Slides:



Advertisements
Similar presentations
Instruction Set-Intro
Advertisements

Chapter 2.
CS 61C L07 MIPS Intro (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Intro to MIPS
CS61C L05 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
CS 61C L06 MIPS Intro (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #6: Intro to MIPS
CS61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L6 Intro MIPS ; Load & Store (1) Garcia, Fall 2005 © UCB Hate EMACS? Love EMACS? Richard M. Stallman, a famous proponent of open- source software,
COMP3221: Microprocessors and Embedded Systems Lecture 2: Instruction Set Architecture (ISA) Lecturer: Hui Wu Session.
Normal C Memory Management °A program’s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 4: Arithmetic / Data Transfer Instructions Partially adapted from Computer Organization.
CS61C L08 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 3: Arithmetic Instructions Partially adapted from Computer Organization and Design, 4.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
CS1104 Assembly Language: Part 1
CIS 314 : Computer Organization Lecture 1 – Introduction.
CS 61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L06 More Memory Management, Intro to MIPS (1) Chae, Summer 2008 © UCB Albert Chae, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
Implementation of a Stored Program Computer
From C to Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
On Textbook CD: SPIM Jen-Chang Liu, Simulation of a virtual machine Virtual machine (simulator) Differences between SPIM and real MIPS? No delayed.
Lecture 7: Instruction Set Architecture CSE 30: Computer Organization and Systems Programming Winter 2014 Diba Mirza Dept. of Computer Science and Engineering.
CS 61C L2.1.2 MM and MIPS (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Garbage Collection & Intro to MIPS Kurt Meinz.
Instruction Set Architecture
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Reformatting a Machine 125 Million Miles Away! Mars rover “Opportunity” got its flash drive reformatted 9/8/14, reason: rover keeps rebooting. Happy news:
1 (Based on text: David A. Patterson & John L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 3 rd Ed., Morgan Kaufmann,
1 CS232: Computer Architecture II Fall 2011 Intel i7 Quad-core.
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
Chapter 2 Instructions: Language of the Computer Part I.
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Oct. 25, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Alternative Instruction Sets * Jeremy R. Johnson Wed. Oct. 25, 2000.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
Lecture 2: Instruction Set Architecture part 1 (Introduction) Mehran Rezaei.
Computer Organization Instructions Language of The Computer (MIPS) 2.
CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro 1 Instructors: Vladimir Stojanovic & Nicholas Weaver
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
CHAPTER 2 Instruction Set Architecture 3/21/
CS61C L05 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Spring 2014 © UCB TA/Guest Lecturer: Shreyas Chand inst.eecs.berkeley.edu/~cs61c.
Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Different CPUs.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 4, EE 334.
Computers’ Basic Organization
Computer Architecture & Operations I
Computer Architecture & Operations I
IT 251 Computer Organization and Architecture
Lecturer PSOE Dan Garcia
A Closer Look at Instruction Set Architectures
Instruction Set Architecture
A Closer Look at Instruction Set Architectures
IT 251 Computer Organization and Architecture
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
CS170 Computer Organization and Architecture I
The University of Adelaide, School of Computer Science
Hello to Jessy VanDivner listening from Ohio!
COMS 361 Computer Organization
Instructions in Machine Language
COMS 361 Computer Organization
Evolution of ISA’s ISA’s have changed over computer “generations”.
March 2006 Saeid Nooshabadi
Evolution of ISA’s ISA’s have changed over computer “generations”.
CPU Structure CPU must:
CS334: MIPS language _Mars simulator Lab 2_1
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Evolution of ISA’s ISA’s have changed over computer “generations”.
Chapter 4 The Von Neumann Model
Presentation transcript:

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 I´ve been getting this a lot lately So, what are you teaching this term? Computer Organization. Do you mean, like keeping your computer in place? … here´s the monitor, here goes the CPU, … Do you need a class for that? Well, not exactly.

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 OK, OK, I always have a mess of files, I think I should take that class. Not quite that, either.

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 What then? (-sigh!- do you really want to know?) Long story short: it is about how the CPU interacts with memory inside the computer, and all that … Abrupt topic change :-)

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Lecture 2: Instruction Set Architectures (ISA) and MIPS Assembly language

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Von Neumann architecture model of program execution: Stored program model: instructions and data are stored in memory Fetch/execute cycle: instructions are fetched from memory to the CPU and executed by the hardware

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Basic Instruction Cycle

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Instruction Execution

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Instruction Set Architectures Different CPUs implement different sets of instructions. The set of instructions a particular CPU implements is an Instruction Set Architecture (ISA). Examples: Intel 80x86 (Pentium 4), IBM/Motorola PowerPC (Macintosh), MIPS, Intel IA64,...

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Instruction Set Architectures - History Accumulator ISA One register in the CPU for arithmetic called the accumulator LOAD ACC,X ADD ACC, Y STORE ACC,Z Stack ISA A stack is used for arithmetic PUSH X PUSH Y ADD POP Z

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Instruction Set Architectures General Purpose Register ISA Three types based on where operands for arithmetic operations can come from. Memory-memory Register-memory Register-register (Load/Store) MIPS has a Load/Store ISA

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Instruction Set Architectures Early trend was to add more and more instructions to new CPUs to do elaborate operations VAX architecture had an instruction to multiply polynomials! RISC philosophy – Reduced Instruction Set Computing ( Cocke IBM, Patterson, Hennessy, 1980s ) Keep the instruction set small and simple, makes it easier to build fast hardware. Let software do complicated operations by composing simpler ones.

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 RISC Architectures Fixed instruction lengths Load/store instruction sets Limited addressing modes (ways to access variables in memory) Limited operations Sun SPARC, IBM PowerPC, MIPS

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 MIPS Architecture MIPS – semiconductor company that built one of the first commercial RISC architectures We will study the MIPS architecture in some detail in this class (also used in CIS 429 Computer Architecture - Spring ’05) Why MIPS instead of Intel 80x86? MIPS is simple, elegant. Don’t want to get bogged down in gritty details. MIPS widely used in embedded apps, x86 little used in embedded, and more embedded computers than PCs

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Variables: Registers (1/4) Unlike HLL like C or Java, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! Benefit: Since registers are directly in hardware, they are very fast (faster than 1 billionth of a second)

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Variables: Registers (2/4) Drawback: Since registers are in hardware, there are a predetermined number of them Solution: MIPS code must be very carefully put together to efficiently use registers 32 registers in MIPS Why 32? Smaller is faster Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Variables: Registers (3/4) Registers are numbered from 0 to 31 Each register can be referred to by number or name Number references: $0, $1, $2, … $30, $31

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Variables: Registers (4/4) By convention, each register also has a name to make it easier to code For now: $16 - $23  $s0 - $s7 (correspond to C variables) $8 - $15  $t0 - $t7 (correspond to temporary variables) Later will explain other 16 register names In general, use names to make your code more readable

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 CPU, Registers, and Memory ALUControl Registers Memory

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 C, Java variables vs. registers In C (and most High Level Languages) variables declared first and given a type Example: int fahr, celsius; char a, b, c, d, e; Each variable can ONLY represent a value of the type it was declared as (cannot mix and match int and char variables). In Assembly Language, the registers have no type; operation determines how register contents are treated

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 MIPS data in memory vs. registers In MIPS, you can declare memory variables using.data Each item is one word Give it a symbolic name Give it an initial value.data One:.word 1 # first value, initialized to 1 Two:.word 2 # second value, initialized to 2

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Comments in Assembly Another way to make your code more readable: comments! Hash ( # ) is used for MIPS comments anything from hash mark to end of line is a comment and will be ignored Note: Different from C. C comments have format /* comment */ so they can span many lines

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Instructions In assembly language, each Instruction executes exactly one of a small set of simple commands Different from C (and most other High Level Languages)

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Assembly Instructions for memory access LW $s3, X # Load Word Loads one word of data from memory location X into register $s3 SW $s4, Y # Store Word Stores one word of data from register $s4 into memory location Y

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 MIPS Addition and Subtraction (1/4) Syntax of Instructions: OPDest, Src1, Src2 where: OP) operation name Dest) operand getting result (“destination”) Src1) 1st operand for operation (“source1”) Src2) 2nd operand for operation (“source2”) Syntax is rigid: 1 operator, 3 operands Why? Keep Hardware simple via regularity

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Addition and Subtraction of Integers (2/4) Addition in Assembly Example: add$s0,$s1,$s2 (in MIPS) Equivalent to: s0=s1+s2 (in C) Subtraction in Assembly Example: sub$s3,$s4,$s5 (in MIPS) Equivalent to: s3=s4-s5 (in C)

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Addition and Subtraction of Integers (3/4) How do the following C statement? a = b + c + d - e; Break into multiple instructions add $t0, $s1, $s2 # temp = b + c add $t0, $t0, $s3 # temp = temp + d sub $s0, $t0, $s4 # a = temp - e Notice: A single line of C may break up into several lines of MIPS. Notice: Everything after the hash mark on each line is ignored (comments)

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Addition and Subtraction of Integers (4/4) How do we do this? f = (g + h) - (i + j); Use intermediate temporary register add $t0,$s1,$s2# temp = g + h add $t1,$s3,$s4# temp = i + j sub $s0,$t0,$t1# f=(g+h)-(i+j)

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Register Zero One particular immediate, the number zero (0), appears very often in code. So we define register zero ($0 or $zero ) to always have the value 0 e.g. add $s0,$s1,$zero (MIPS) f = g (C) where MIPS registers $s0 and $s1 are associated with C variables f and g defined in hardware, so an instruction add $zero,$zero,$s0 #does nothing

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Immediates Immediates are numerical constants. They appear often in code, so there are special instructions for them. Add Immediate: addi $s0,$s1,10 (in MIPS) f = g + 10 (in C) where MIPS registers $s0,$s1 are associated with C variables f, g Syntax similar to add instruction, except that last argument is a number instead of a register.

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Immediates There is no Subtract Immediate in MIPS: Why? Limit types of operations that can be done to absolute minimum if an operation can be reduced to a simpler operation, don’t include it addi …, -X  subi …, X  no subi addi $s0,$s1,-10 (MIPS) f = g - 10 (C) where MIPS registers $s0,$s1 are associated with C variables f, g

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Summary Instruction set architecture (ISA) The design of the basic machine level instructions understood by a computer RISC (Reduced Instruction Set Computers) Simple, efficient design for the basic building blocks Push complexity up a level to SW and compiler optimizations

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Summary (cont.) In MIPS Assembly Language: Registers replace C and Java variables One Instruction (simple operation) per line Simpler is Better Smaller is Faster New Instructions: lw, sw, add, addi, sub New Registers: Persistent Variables: $s0 - $s7 Temporary Variables: $t0 - $t9 Zero: $zero

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 I. Instruction Execution

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Basic Instruction Cycle

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Program Execution Load940 Add 941 Store 941

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Program Execution (Text) 1. FETCH: The PC contains 300, the address of the first instruction. This instruction is loaded into the IR. PC is incremented to 301. Comment: The FETCH:The PC contains 300, the address first four bits in the IR ("1") give the opcode for "LOAD." The remaining twelve bits ("940") specify the memory address where the data is to be loaded from. 2. EXECUTE: The data is loaded into the accumulator. AC now has the value "003" 3. FETCH: Instruction 301 is fetched. PC is incremented to 302. Comment: The first four bits ("5") give the opcode for "ADD". The remaining twelve bits ("941") give the address of the data to be added.

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 Program Execution (Text) 4. EXECUTE: The contents of the AC and the contents of location 941 are added and stored back into the AC. AC now has the value "005“. 5. FETCH: Instruction 303 is fetched. PC is incremented to 303. Comment: The first four bits ("2") give the opcode for "STORE." The remaining twelve bits ("941") give the address of where to store the result. 6. EXECUTE: The contents of the AC are stored at address 941.

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 # # My First MIPS program #.data One:.word 1 # first value, initialized to 1 Two:.word 2# second value, initialized to 2 # program to add 1+2 # load constants and add values lw $s0,One # first operand lw $s1,Two # second operand add $s2,$s0,$s1 # add First MIPS Program

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 # # Simple example of a SPIM program -- compute the polynomial # y = ax^2 + bx + c # # code section.text.globl __start __start: # load registers lw $s0, X # "lw" means "load word" lw $s1, A # note register names begin with "$" lw $s2, B lw $s3, C

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 # compute polynomial mul $t0, $s0, $s0 # t0 = x^2 mul $t0, $t0, $s1 # t0 = a*x^2 mul $t1, $s0, $s2 # t1 = b*x add $t0, $t0, $t1 # t0 = a*x^2 + b*x add $t0, $t0, $s3 # t0 = a*x^2 + b*x + c sw $t0, Y # done -- store final result in y # print result move $a0, $t0 # copy $t0 to $a0 li $v0, 1# "li" = "load immediate" syscall # print the result # exit program li $v0, 10 # "li" = "load immediate" syscall # syscall = "system call"

CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 # data section.data X:.word 7 A:.word 3 B:.word 4 C:.word 5 Y:.word 0