Docsity

MIPS Assembly Arithmetic Instructions at UC Santa Barbara - Prof. Ra Singh, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
ECE 15B COMPUTER ORGANIZATION
Dr. Rahul Singh!
April 7, 2009
Lecture 3!
Arithmetic Instructions !
Electrical and Computer Engineering
University of California, Santa Barbara!April 7, 2009!
Lecture 3: Arithmetic Instructions!1!
Annoucements!
• Homework #1 due Friday at 5:00 p.m.!
• Quiz 1 will be moved to Tuesday April 21
from April 23!
Electrical and Computer Engineering
University of California, Santa Barbara!April 7, 2009!
Lecture 3: Arithmetic Instructions!2!
Overview!
• C operators, operands!
• Variables in Assembly: Registers!
• Comments in Assembly!
• Addition and Subtraction in Assembly!
• Memory Access in Assembly!
Electrical and Computer Engineering
University of California, Santa Barbara!April 7, 2009!
Lecture 3: Arithmetic Instructions!3!
Assembly Language!
• Basic job of a CPU: execute lots of
instructions!
• Instructions are the primitive operations that the
CPU may execute.!
• Different CPUs implement different sets of
instructions. !
• Instruction Set Architecture (ISA) is the set of
instructions a particular CPU implements!
• Examples: Intel 80x86 (Pentium 4), IBM/Motorola
PowerPC (Macintosh), MIPS, Intel IA64, ...!
pf3
pf4
pf5

Partial preview of the text

Download MIPS Assembly Arithmetic Instructions at UC Santa Barbara - Prof. Ra Singh and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE 15B COMPUTER ORGANIZATION

Dr. Rahul Singh April 7, 2009

Lecture 3

Arithmetic Instructions Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 1

Annoucements

  • Homework #1 due Friday at 5:00 p.m.
  • Quiz 1 will be moved to Tuesday April 21

from April 23

Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^2

Overview

  • C operators, operands
  • Variables in Assembly: Registers
  • Comments in Assembly
  • Addition and Subtraction in Assembly
  • Memory Access in Assembly Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^3

Assembly Language

  • Basic job of a CPU: execute lots of

instructions

  • Instructions are the primitive operations that the CPU may execute.
  • Different CPUs implement different sets of

instructions.

  • Instruction Set Architecture (ISA) is the set of instructions a particular CPU implements
  • Examples: Intel 80x86 (Pentium 4), IBM/Motorola PowerPC (Macintosh), MIPS, Intel IA64, ...

Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 4 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 (Cocke IBM, Patterson, Hennessy, 1980s) Reduced Instruction Set Computing - Keep the instruction set small and simple - makes it easier to build fast hardware. - Let software (compiler) do complicated operations by composing simpler ones. Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 5 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
  • 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
  • There are more embedded computers than PCs Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^6 Assembly Variables: Registers
  • 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) Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^7 Assembly Variables: Registers
  • Drawback:
  • 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

Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 12 Assembly Instructions

  • In assembly language, each statement

(called an Instruction), executes exactly one

of a short list of simple commands

  • Unlike in C (and most other High Level

Languages), each line of assembly code

contains at most 1 instruction

  • Instructions are related to operations (=, +, -,

*, /) in C or Java

Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 13 MIPS Syntax

  • Instruction Syntax: [label:] Op-code [oper. 1], [oper. 2], [oper. 3] [#comment] (0) (1) (2) (3) (4) (5) - where: 1) operation name 2,3,4) operands
  1. Comments
  2. label field is optional, will discuss later. - for arithmetic and logic instruction 2) operand getting result (“destination”) 3) 1st operand for operation (“source1”) 4) 2nd operand for operation (“source2”)
  • Syntax is rigid:
    • 1 operator, 3 operands
    • Why? Keep hardware simple via regularity Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^14 Addition and Subtraction of Integers
  • Addition in Assembly
  • Example: add $s0, $s1, $s2 (in MIPS)
  • Equivalent to: a = b + c (in C)
  • Where MIPS registers $s0, $s1, $s2 are associated with C variables a, b, c
  • Subtraction in Assembly
  • Example: sub $s3, $s4, $s5 (in MIPS)
  • Equivalent to: d = e - f (in C)
  • Where MIPS registers $s3, $s4, $s5 are associated with C variables d, e, f Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^15 Addition and Subtraction of Integers
  • 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
  • Notes:
  • A single line of C may break up into several lines of MIPS.
  • Everything after the hash mark on each line is ignored (comments)

Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 16 Addition and Subtraction of Integers

  • 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) Lecture 3: Arithmetic Instructions^ Electrical and Computer Engineering University of California, Santa Barbara April 7, 2009 17 Immediates
  • Immediates are numerical constants.
  • They appear often in code, so there are

special instructions for them.

  • Add Immediate: addi $s0, $s1, 10 # f = g + 10 (in C) - where MIPS registers $s0 and $s1 are associated with C variables f and g
  • Syntax similar to add instruction, except that last argument is a number instead of a register. Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^18 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 decomposed into a simpler operation, exclude it
  • addi …, -X is equivalent to subi …, X  so no subi
  • Example addi $s0,$s1,-10 (in MIPS)
  • where MIPS registers $s0 and $s1 are associated with C variables f and g
  • f = g - 10 (in C) Electrical and Computer Engineering Lecture 3: Arithmetic Instructions (^) University of California, Santa Barbara April 7, 2009^19 Register Zero
  • One particular immediate, the number zero (0), appears very often in code. - So define register zero ($0 or $zero) to always have the value 0.
  • Example add $s0,$s1,$zero (in MIPS) - where MIPS registers $s0 and $s1 are associated with C variables f, g - f = g (in C)
  • Defined in hardware, so an instruction addi $zero,$zero, will not do anything!