EE 109 Unit 10 MIPS Instruction Set. MIPS Processor and Bus Interface. Instruction Set Architecture (ISA) MIPS INSTRUCTION OVERVIEW

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

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

EE 109 Unit 8 MIPS Instruction Set

EE 109 Unit 10 MIPS Instruction Set

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

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

EE 109 Unit 10 Assembler Directives and Control Flow

Mark Redekopp, All rights reserved. EE 352 Unit 4. Assembly and the MARS Simulator Control Flow (Branch Instructions)

The MIPS Instruction Set Architecture

Reduced Instruction Set Computer (RISC)

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

Reduced Instruction Set Computer (RISC)

CPS311 - COMPUTER ORGANIZATION. A bit of history

MIPS Reference Guide

Computer Architecture. The Language of the Machine

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

Computer Architecture. MIPS Instruction Set Architecture

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

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

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

MIPS Assembly Language. Today s Lecture

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

MIPS Instruction Set

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

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

Computer Architecture

MIPS Instruction Format

ECE260: Fundamentals of Computer Engineering

Instructions: Language of the Computer

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

M2 Instruction Set Architecture

ECE260: Fundamentals of Computer Engineering

Chapter 2A Instructions: Language of the Computer

Assembly Programming

MIPS%Assembly% E155%

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

CS3350B Computer Architecture MIPS Instruction Representation

Concocting an Instruction Set

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

TSK3000A - Generic Instructions

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

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

MIPS Instruction Reference

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

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

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

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

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

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

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

Examples of branch instructions

Instructions: Language of the Computer

Computer Organization MIPS ISA

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

INSTRUCTION SET COMPARISONS

F. Appendix 6 MIPS Instruction Reference

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

ECE 15B Computer Organization Spring 2010

Flow of Control -- Conditional branch instructions

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

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

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

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

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

SPIM Instruction Set

Course Administration

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

COMPUTER ORGANIZATION AND DESIGN

Concocting an Instruction Set

CS 61c: Great Ideas in Computer Architecture

Arithmetic for Computers

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

MIPS Instruction Set Architecture (2)

Review: MIPS Organization

ECE 154A Introduction to. Fall 2012

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Unsigned Binary Integers

Unsigned Binary Integers

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

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

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

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

CSCI 402: Computer Architectures

MIPS Assembly Language

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

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

CENG3420 Lecture 03 Review

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

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

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

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Concocting an Instruction Set

ECE331: Hardware Organization and Design

Forecast. Instructions (354 Review) Basics. Basics. Instruction set architecture (ISA) is its vocabulary. Instructions are the words of a computer

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

CMPE324 Computer Architecture Lecture 2

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

Chapter 2. Instructions: Language of the Computer

ece4750-parc-isa.txt

Transcription:

10.1 10.2 EE 109 Unit 10 MIPS Instruction Set MIPS INSTRUCTION OVERVIEW 10.3 10.4 Instruction Set Architecture (ISA) Defines the of the processor and memory system Instruction set is the the HW can understand and the SW is composed with 2 approaches = instruction set computer Large, rich vocabulary More work per instruction but slower HW = instruction set computer Small, basic, but sufficient vocabulary Less work per instruction but faster HW MIPS Processor and Bus Interface The MIPS processor can execute software instructions that will cause it to: Load (Read) and Store (Write) data to and from memory or I/O devices Perform arithmetic and logic operations (add, sub, and, etc.) Make decisions to move around in the code (loops, ifs, call a function, etc.) MIPS Processor out op. ALU ADD, SUB, AND, OR (32-bits ALU) in1 in2 PC $0-$31 (32-bits each) Addr (32-bits) Data (32-bits) Control Memory 0 instruc data

10.5 10.6 Which Instructions In this class we'll focus on assembly to do the following tasks (shown with the corresponding MIPS assembly mnemonics) Load variables (data) from memory (or I/O) [LW,LH,LB] Perform arithmetic, logical, and shift instructions in the CPU [ADD,SUB,AND,OR,SLL,SRL,SRA] Store variables (data) back to memory after computation is complete [SW, SH, SB] Compare data [SLT] "Branch" to other code (to implement if and loops) [BEQ,BNE,J] Call subroutines/functions [JAL, JR] MIPS Instruction Set -style internal / external data size Registers and ALU are 32-bits wide Memory bus is logically 32-bits wide (though may be physically wider) Registers General Purpose Registers (GPR s) For integer and address values A few are used for specific tasks/values Fixed size instructions All instructions encoded as a single -bit word Three operand instruction format (dest, src1, src2) Load/store architecture (all data operands must be in registers and thus loaded from and stored to memory explicitly) 10.7 10.8 MIPS GPR s MIPS Programmer-Visible Registers Assembler Name Reg. Number Description $zero $0 Constant 0 value $at $1 Assembler temporary $v0-$v1 $2-$3 Procedure return values or expression evaluation $a0-$a3 $4-$7 Arguments/parameters $t0-$t7 $8-$15 Temporaries $s0-$s7 $16-$23 Saved Temporaries $t8-$t9 $24-$25 Temporaries $k0-$k1 $26-$27 Reserved for OS kernel $gp $28 Global Pointer (Globaland static variables/data) $sp $29 Stack Pointer $fp $30 Frame Pointer $ra $31 Return address for current procedure Avoid using the yellow (highlighted) registers for anything other than its stated use General Purpose Registers (GPR s) Hold data operands or addresses (pointers) to data stored in memory Special Purpose Registers : (32-bits) Holds the address of the next instruction to be fetched from memory & executed HI: Hi-Half Reg. (32-bits) For MUL, holds 32 MSB s of result. For DIV, holds 32-bit remainder LO: Lo-Half Reg. (32-bits) For MUL, holds 32 LSB s of result. For DIV, holds 32-bit quotient PC: GPR s $0 - $31 32-bits Recall multiplying two 32-bit numbers yields a 64-bit result HI: LO: MIPS Core Special Purpose Registers

10.9 R-Type Arithmetic/Logic Instructions 10.10 Performing Arithmetic, Logic, and Shift Operations IMPORTANT R-TYPE INSTRUCTIONS C operator Assembly Notes + ADD $d, $s, $t d=destination, s = src1, t = src2 - SUB $d, $s, $t Order: $s $t. SUBU for unsigned * MUL $d, $s, $t If multiply won t overflow 32-bit result & AND $d, $s, $t OR $d, $s, $t ^ XO$ $d, $s, $t ~( ) NOR $d, $s, $t Can be used for bitwise-not (~) << SLL $d, $s, shamt SLLV $d, $s, $t >> (signed) SRA $d, $s, shamt SRAV $d, $s, $t >> (unsigned) SRL $d, $s, shamt SRLV $d, $s, $t <, >, <=, >= SLT $d, $s, $t SLTU $d, $s, $t * MULT $s, $t MULTU $s, $t / DIV $s, $t DIVU $s, $t Shifts $s left by shamt (shift amount) or $t bits Shifts $s right by shamt or $t bits replicating sign bit to maintain sign Shifts $s left by shamt or $t bits shifting in 0 s Comparison. Order: $s $t. Sets $d=1 if $s < $t, $d=0 otherwise Result in HI/LO. Use mfhi and mflo instruction to move results $[s] / $[t]. Remainder in HI, quotient in LO R-Type Instructions To perform arithmetic or logic operations in many processors (MIPS included) a copy of the operand MUST be loaded into a register first Consider the following operations F = X + Y Z G = F Z Complete the assembly code to perform these operations Remember to load/store your operands to/from registers out op. ALU ADD, SUB, AND, OR (32-bits ALU) in1 in2 $4 $5 $6 $7 $8 MIPS Processor $0-$31 (32-bits each) PC C Code F = X + Y Z; G = F Z; Addr (32-bits) Data (32-bits) Control MIPS Assembly 10.11 LOAD* $4, X # Get X from mem. LOAD # Get Y from mem. LOAD # Get Z from mem. ADD # Tmp = X+Y SUB # Tmp = Tmp - Z STORE # Store to F in mem OR # Tmp2 = F Z STORE $8, G # Store to G in mem * LOAD/STORE are not actual instructions. We will learn the actual syntax soon. 00400000 00400004 10010000 10010004 Load Load Store X (e.g. 12) Y (e.g. 7) Z (e.g. 3) F G Memory Format 6-bits opcode R-Type Instructions rs (src1) rs, rt, rdare 5-bit fields for register numbers shamt= shift amount and is used for shift instructions indicating # of places to shift bits opcode and func identify actual operation (e.g. ADD, SUB) Example: ADD $5, $24, $17 rt (src2) rd (dest) opcode rs rt rd 000000 shamt shamt 00000 6-bits function func 100000 Arith. Inst. $24 $17 $5 unused ADD 10.12

10.13 10.14 Logical Operations Logical Operations Should already be familiar with (sick of) these! Logic operations are usually performed on a pair of bits X1 X2 AND 0 0 0 0 1 0 1 0 0 1 1 1 AND Output is true if both inputs are true 0 AND x = 0 1 AND x = x x AND x = x X1 X2 OR 0 0 0 0 1 1 1 0 1 1 1 1 OR Output is true if any input is true 0 OR x = x 1 OR x = 1 x OR x = x X1 X2 XOR 0 0 0 0 1 1 1 0 1 1 1 0 XOR Output is true if exactly one input is true 0 XOR x = x 1 XOR x = NOT x x XOR x = 0 X1 NOT 0 1 1 0 NOT Output is inverse of input Logic operations on numbers means performing the operation on each pair of bits 1 2 3 Initial Conditions: $1 = 0xF0, $2 = 0x3C AND $2,$1,$2 R[2] = 0x30 OR $2,$1,$2 R[2] = 0xFC XOR $2,$1,$2 R[2] = 0xCC 0xF0 AND 0x3C 0x30 0xF0 OR 0x3C 0xFC 0xF0 XOR 0x3C 0xCC 1111 0000 AND 0011 1100 0011 0000 1111 0000 OR 0011 1100 1111 1100 1111 0000 XOR 0011 1100 1100 1100 Tip: Unless you're very good w/ hex, convert to binary then perform these operations! Logical Operations Logic operations on numbers means performing the operation on each pair of bits 4 Initial Conditions: $1= 0xF0, $2 = 0x3C NOR $2,$1,$2 R[2] = 0x03 0xF0 NOR 0x3C 0x03 1111 0000 NOR 0011 1100 0000 0011 Bitwise NOT operation can be performed by NOR ing register with itself 10.15 Shift Operations Shifts data bits either left or right Bits shifted out and dropped on one side Usually (but not always) 0 s are shifted in on the other side In addition to just moving bits around, shifting is a fast way to multiply or divide a number by (see next slides) 2 kinds of shifts Logical shifts (used for numbers) Arithmetic shifts (used for numbers) Right Shift by 2 bits: Left Shift by 2 bits: 10.16 NOR $2,$1,$1 R[2] = 0x0F 0xF0 NOR 0xF0 0x0F 1111 0000 NOR 1111 0000 0000 1111 0 0 0 0 1 1 0 0 Original Data 0 0 0 0 1 0 1 0 Original Data 0 s shifted in 0 s shifted in 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0 Shifted by 2 bits Shifted by 2 bits

10.17 10.18 Logical Shift Arithmetic Shift 0 s shifted in Only use for operations on unsigned data Right shift by n-bits = Dividing by 2 n Left shift by n-bits = Multiplying by 2 n 0 x 0 0 0 0 0 0 0 C 0 0 1 1 0 0 = +12 Use for operations on signed data Arithmetic Right Shift replicate MSB Right shift by n-bits = Dividing by 2 n Arithmetic Left Shift shifts in 0 s Left shift by n-bits = Multiplying by 2 n 0 x F F F F F F F C 1 1 1 1 0 0 = -4 Arithmetic Right Shift by 2 bits: Arithmetic Left Shift by 2 bits: Logical Right Shift by 2 bits: Logical Left Shift by 3 bits: 0 s shifted in 0 s shifted in = +3 = +96 0 x 0 0 0 0 0 0 0 3 0 x 0 0 0 0 0 0 6 0 MSB replicated and shifted in 1 1 1 1 0 x Notice if we shifted in 0 s (like a logical right shift) our result would be a positive number and the division wouldn t work 0 s shifted in = -1 1 1 0 0 0 0 = -16 0 x _ Notice there is no difference between an arithmetic and logical left shift. We always shift in 0 s. 10.19 10.20 Logical Shift vs. Arithmetic Shift MIPS Logical Shift Instructions Logical Shift Use for unsigned or nonnumeric data Will always shift in whether it be a left or right shift Left shift Arithmetic Shift Use for signed data Left shift will shift in Right shift will ( the sign bit) rather than shift in 0 s If negative number stays negative by shifting in If positive stays positive by shifting in Left shift SRL instruction Shift Right Logical SLL instruction Shift Left Logical Format: SxLrd, rt, shamt (shamt = shift amount and is a constant; e.g. x << 7) SxLVrd, rt, rs (rsis the shift amount and is variable; e.g. x << y) Notes: shamt limited to a 5-bit value (0-31) SxLVshifts data in rt by number of places specified in rs Examples SRL $5, $12, 7 // Shifts data in reg. $12 right by 7 places SLLV $5, $12, $20 // If $20=5, shift data in $12 left by 5 places opcode rs 000000 00000 rt 01100 rd 00101 shamt 00111 func 000010 Arith. Inst. unused $12 $5 7 SRL Right shift Right shift 000000 10100 01100 00101 00000 000100 Arith. Inst. $20 $12 $5 unused SLLV

MIPS Arithmetic Shift Instruction 10.21 Immediate Operands 10.22 SRA instruction Shift Right Arithmetic No arithmetic left shift (use SLL for arithmetic left shift) Format: SRA rd, rt, shamt (shamt = shift amount and is a constant; e.g. x >> 7) SRAV rd, rt, rs (rs is the shift amount and is variable; e.g. x >> y) Notes: shamt limited to a 5-bit value (0-31) SRAV shifts data in rt by number of places specified in rs Examples SRA $5, $12, 7 SRAV $5, $12, $20 opcode rs 000000 00000 rt 01100 rd 00101 shamt 00111 func 000011 Arith. Inst. unused $12 $5 7 SRA 000000 10100 01100 00101 00000 000111 Arith. Inst. $20 $12 $5 unused SRAV Most ALU instructions also have an immediate form to be used when one operand is a constant value Syntax: ADDI Rs, Rt, imm Because immediates are limited to 16-bits, they must be extended to a full 32- bits when used the by the processor instructions always sign-extendto a full 32-bits even for unsigned instructions (addiu) instructions always zero-extendto a full 32-bits Examples: ADDI $4, $5, -1 // R[4] = R[5] + 0xFFFFFFFF ORI $10, $14, -4 // R[10] = R[14] 0x0000FFFC Arithmetic ADDI SLTI Logical ANDI ORI XORI Note: SUBI is unnecessary since we can use ADDI with a negative immediate value Set If Less-Than 10.23 10.24 SLT $rd, $rs, $rt Compares $rsvalue with $rtvalue and stores Boolean (1 = true, 0 = false) value into $rd C code equivalent: $rdcan only be 0x0000001 or 0x00000000 after execution Assumes signed integer comparison SLTI $rd, $rs, immediate Same as above but now 2 nd source is a constant SLTU $rd, $rs, $rt Same as SLT but interprets values as unsigned Initial Conditions: $1= 0xffffffff, $2 = 0x00000000 $3 = 0x000000ff SLT $4, $1, $2 $4 = 0x0000000_ SLT $4, $3, $3 $4 = 0x0000000_ SLT $4, $3, $1 $4 = 0x0000000_ SLTI $4, $2, 35 $4 = 0x0000000_ SLTU $4, $1, $2 $4 = 0x0000000_ Loading (Reading) and Storing (Writing) Data From and To Memory DATA TRANSFER AND MEMORY ACCESS INSTRUCTIONS

Physical Memory Organization 10.25 MIPS Supported Data Sizes 10.26 Physical view of memory as large 2-D array of bytes (8K rows by 1KB columns) per chip (and several chips) Address is broken into fields of bits that are used to identify where in the array the desired 32-bit word is Processor always accesses memory chunks the size of the data bus, selecting only the desired bytes as specified by the instruction Proc. A D 0x00000404 32 32 Physical View of Memory 0x000800 0x000400 0x000000 Assume each unit is a word Integer 3 Sizes Defined Byte (B) 8-bits Halfword (H) 16-bits = 2 bytes Word (W) 32-bits = 4 bytes Floating Point 3 Sizes Defined Single (S) 32-bits = 4 bytes Double (D) 64-bits = 8 bytes (For a 32-bit data bus, a double would be accessed from memory in 2 reads) 0x0404 = Rank/Bank Row Col XX 00000 0000000000001 00000001 00 Sample Address Breakdown MIPS Memory Organization We can logically picture memory in the units (sizes) that we actually access them Most processors are Every byte (8-bits) has a unique address 32-bit address bus => 4 GB address space However, 32-bit logical data bus allows us to access of data at a time Logical view of memory arranged in rows of 4-bytes Still with separate addresses for each byte int x,y=5;z=8; x = y+z; A Proc. D 32 32 F8 13 5A Mem. 0x000002 0x000001 0x000000 Logical Byte-Oriented View of Mem. 8E 7C AD F8 33 13 29 5A 0x000008 0x000004 0x000000 Logical Word-Oriented View 10.27 Recall variables live in memory & need to be loaded into the processor to be used Memory & Data Size Little-endian memory can be thought of as right justified Always provide the LS-Byte address of the desired data Size is explicitly defined by the instruction used Memory Access Rules Halfword or Word access must start on an address that is a multiple of that data size (i.e. half = multiple of 2, word = multiple of 4) Registers: Byte 31 15 0 Half 31 0 Word (Assume start address = N) LB Used to load a 1- byte var. (char) LH N+3 N+2 N+1 Byte operations only access the byte at the specified address N+3 N+2 N+1 Memory Halfword operations access the 2-bytes starting at the specified address LW N+3 N+2 N+1 N Used to load a 4- Word operations access the byte variable (int) 4-bytes starting at the specified address N N 10.28

10.29 10.30 Memory Read Instructions (Signed) Memory Read Instructions (Unsigned) GPR Memory GPR Memory 31 7 0 Sign Extend Byte If address = 0x02 Reg. = 0x LB (Load Byte) Provide address of desired byte 5A 13 000004 F8 7C 000000 31 7 0 Zero Extend If address = 0x01 Reg. = 0x Byte LBU (Load Byte) Provide address of desired byte 5A 13 000004 F8 7C 000000 31 15 0 Sign Extend Half If address = 0x00 Reg. = 0x LH (Load Half) Provide address of starting byte 5A 13 000004 F8 7C 000000 31 15 0 Zero Extend Half If address = 0x00 Reg. = 0x LHU (Load Half) Provide address of starting byte 5A 13 000004 F8 7C 000000 31 0 Word If address = 0x00 Reg. = 0x LW (Load Word) Provide address of starting byte 5A 13 000004 F8 7C 000000 31 0 Word If address = 0x00 Reg. = 0x LW (Load Word) Provide address of starting byte 5A 13 000004 F8 7C 000000 10.31 10.32 Memory Write Instructions MIPS Memory Alignment Limitations GPR 31 7 0 Byte 31 Reg. = 0x12345678 15 0 Half 31 Reg. = 0x12345678 0 Word Reg. = 0x12345678 SB (Store Byte) Provide address of desired byte SH (Store Half) Provide address of starting byte SW (Store Word) Provide address of starting byte Memory 000004 000000 if address = 0x02 000004 000000 if address = 0x02 000004 000000 if address = 0x00 Bytes can start at any address Halfwords must start on an even address Words must start on an address that is a multiple of 4 Examples: Word @ A18C Halfword@ FFE6 Word @ A18E Halfword@ FFE5 EA 52 7C C1 29 BD CF F8 13 4B 49 5A 00FFE4 00A18C Addr

10.33 10.34 Load Format (LW,LH,LB) More LOAD Examples Syntax: LW $rt, offset($rs) $rt = Destination register offset($rs) = Address of desired data Operation: $rt= Mem[ offset + $rs] offset limited to 16-bit signed number Examples LW $2, 0x40($3) // $2 = LBU $2, -1($4) // $2 = LH $2, 0xFFFC($4) // $2 = Examples LB $2,0x45($3) // $2 = LH $2,-6($4) // $2 = LHU $2, -2($4) // $2 = $2 old val. F8BE97CD 0x002048 $2 old val. F8BE97CD 0x002048 $3 00002000 134982FE 0x002044 $3 00002000 134982FE 0x002044 $4 0000204C Registers 5A12C5B7 Memory 0x002040 Address $4 0000204C Registers 5A12C5B7 Memory 0x002040 Address 10.35 10.36 Store Format (SW,SH,SB) Loading an Immediate SW $rt, offset($rs) $rt = Source register offset($rs) = Address to store data Operation: Mem[ offset + $rs] = $rt offset limited to 16-bit signed number Examples SW $2, 0x40($3) SB $2, -5($4) SH $2, 0xFFFE($4) $2 $3 $4 123489AB 00002000 0000204C Registers Memory 0x002048 0x002044 0x002040 Address If immediate (constant) Use or instruction with register Examples ADDI $2, $0, 1 // $2 = 0 + 1 = 1 (Loads const. 1 into $2) ORI $2, $0, 0xF110 // $2 = 0 0xF110 = 0xF110 (Loads 0xF110) If immediate more than 16-bits Immediateslimited to 16-bits so we must load constant with a 2 instruction sequence using the special instruction To load $2 with 0x12345678 $2 $2

10.37 10.38 I-Type Instructions I-Type (Immediate) Format 6-bits opcode rs, rt are 5-bit fields for register numbers I = Immediate is a 16-bit constant opcode identifies actual operation Example: rs (src1) rt (src/dst) ADDI $5, $24, 1 LW $5, -8($3) LW is explained in the next section but is an example of an instruction using the I-type format opcode 16-bits immediate rs 001000 11000 rt 00101 ADDI $24 $5 010111 00011 00101 Fill me in immediate LW $3 $5-8 1 Translating to Machine Code 32-bit Fixed Size Instructions broken into 3 types (R-, I-, and J-) based on Arithmetic/Logic instructions 3 register operands or shift amount Use for data transfer, branches, etc. 2 registers + 16-bit const. 26-bit jump address We'll cover this later -Type -Type -Type 6-bits opcode 6-bits opcode 6-bits opcode rs (src1) rt (src2) rs (src1) rt (src/dst) rd (dest) 26-bits Jump address shamt 16-bits immediate 6-bits function add $5,$7,$8 000000 100000 lw $18, -4($3) 100011 j 0x0400018 000010 0000 0100 0000 0000 0000 0001 10 Each type uses portions of the instruction to "code" certain aspects of the instruction. But they all start with an opcode that helps determine which type will be used. 10.39 10.40 Tips for Translating to Assembly "Be the Compiler" COMPILING HIGH-LEVEL CODE We will now translate C code to assembly A few things to remember: Data variables live in memory Data must be brought into registers before being processed You must have an address/pointer in a register to load/store data to/from memory Generally, you will need 4 steps to translate C to assembly: in a register (LUI + ORI) data from memory to a register ( ) data (ADD, SUB, AND, OR, etc.) data back to memory ( )

Translating HLL to Assembly 10.41 Translating HLL to Assembly 10.42 HLL variables are simply locations in memory A variable name really translates to an address in C assembly operator Assembly Notes int x,y,z; x = y + z; char a[100]; a[1]--; LUI $8, 0x1000 ORI $8, $8, 0x0004 Assume x @ 0x10000004 & y @ 0x10000008 & z @ 0x1000000C Assume array a starts @ 0x1000000C C operator Assembly Notes int dat[4],x; x = dat[0]; x += dat[1]; unsigned int y; short z; y = y / 4; z = z << 3; LUI $8, 0x1000 ORI $8, $8, 0x0010 LW $9, 0($8) LUI $8, 0x1000 ORI $8, $8, 0x0010 LW $9, 0($8) SW $9, 0($8) LH $9, 4($8) SH $9, 4($8) Assume dat @ 0x10000010 & x @ 0x10000020 Assume y @ 0x10000010 & z @ 0x10000014 10.43 Writing Assembly 10.44 Directives Pseudo-instructions ASSEMBLERS Code written at the assembly level needs some additional help for specifying certain things Where and should be placed in memory Easy ways to reference memory locations To help us do this assemblers provide some additional statements that we can use

10.45 10.46 Our Simulator - MARS Assembler Syntax In MARS and most assemblers each line of the assembly program may be one of three possible options Comment Instruction / Pseudo-instruction Download at: http://courses.missouristate.edu/kenvollmar/mars/ 10.47 10.48 Comments In MARS an entire line can be marked as a comment by starting it with a pound (#) character: Example: Instructions In MARS each instruction is written on a separate line and has the following syntax: (Label:) Instruc. Op. Operands Comment Example: # This line will be ignored by the assembler LW $2,8($3) ADDI $2,$2,1 START: ADD $2,$3,$4 # R[2]=R[3] + R[4] Notes: Label is optional and is a text identifier for the address where the instruction is placed in memory. (These are normally used to identify the target of a branch or jump instruction.) In MARS, a comment can be inserted after an instruction by using a # sign A label can be on a line by itself in which case it refers to the address of the first instruction listed after it

10.49 10.50 Labels and Instructions Assembler Directives The optional label in front of an instruction evaluates to the where the instruction or data starts in memory and can be used in other instructions.text START: LW $4,8($10) L1: ADDI $4,$4,-1 BNE $4,$0,L1 J START Assembly Source File Note: The BNE instruc. causes the program to branch (jump) to the instruction at the specified address if the two operands are Not Equal. The J(ump) instruction causes program execution to jump to the specified label (address). LW ADDI BNE J 0x400000 = START 0x400004 = L1 0x400008 0x40000C Assembler finds what address each instruction starts at.text LW $4,8($10) ADDI $4,$4,-1 BNE $4,$0,0x400004 J 0x400000 and replaces the labels with their corresponding address Similar to pre-processor statements (#include, #define, etc.) and global variable declarations in C/C++ Text and data segments Reserving & initializing global variables and constants Compiler and linker status Direct the assembler in how to assemble the actual instructions and how to initialize memory when the program is loaded 10.51 10.52 An Example Text and Static Data Segments This is output from an actual MIPS gcc/g++ compiler Actual instructions are at the bottom (addiu, srl, etc.) Directives are the things starting with. Labels are names ending with : Let's learn about some of the directives x:.word 5.globl nums.section.bss.align 2.type nums, @object.size nums, 40 nums:.space 40.text.align 2.globl _Z6calleei $LFB2:.ent _Z6calleei _Z6calleei:.frame $sp,0,$31.mask 0x00000000,0.fmask 0x00000000,0 addiu $2,$4,3 srl $3,$2,31 addu $2,$2,$3.text directive indicates the following instructions should be placed in the program area of memory.data directive indicates the following data declarations will be placed in the data memory segment I/O Space Stack Dynamic Data Segment Static Data Segment Text Segment Unused 0xFFFF_FFFC 0x8000_0000 0x7FFF_FFFC 0x1000_8000 0x1000_0000 0x0040_0000 0x0000_0000

10.53 10.54 Static Data Directives Fills memory with specified data when program is loaded Format: (Label:).type_id val_0,val_1,,val_n type_id = {.byte,.half,.word,.float,.double} Each value in the comma separated list will be stored using the indicated size Example: myval:.word 1, 2, 0x0003 Each value 1, 2, 3 is stored as a word (i.e. 32-bits) Label myval evaluates to the start address of the first word (i.e. of the value 1) More Static Data Directives Can be used to initialize ASCII strings Format: (Label:).ascii string (Label:).asciiz string.asciiz adds a null-termination character (0) at the end of the string while.ascii does not Example: myval:.asciiz Hello world\n C-strings are just character arrays terminated with a null character ('\0' ASCII = 00 decimal) Each character stored as a byte (including '\n' = Line Feed) Label myval evaluates to the start address of the first byte of the string 10.55 10.56 Reserving Memory Reserves space in memory but leaves the contents unchanged Format: (Label:).space num_bytes.data dat1:.word 0x12345678 array:.space 4 dat2:.word 0xFEDCBA98 Skipped 00 00 00 00 FE DC BA 98 00 00 00 00 0x1000000C 0x10000008 = dat2 0x10000004 = array 12 34 56 78 0x10000000 = dat1 Alignment Directive Used to skip to the next, correctly-aligned address for the given data size Format:.align 0,1,2, or 3 0 = byte-, 1 = half-, 2 = word-, 3 = double-alignment.data dat1:.byte 1, 2, 3.align 1 dat2:.half 0x4567.align 2 dat3:.word 0x89ABCDEF Note: The number after.align is not how many bytes to skip, it indicates what type of data will come next and thus the size to be aligned Skipped Skipped 00 00 00 00 89 AB CD EF 00 00 45 67 0x1000000C 0x10000008 = dat3 0x10000004 = dat2 00 03 02 01 0x10000000 = dat1

.data example Examples 10.57 C/C++ and Directives 10.58.data C1:.byte 0xFE,0x05 MSG:.asciiz SC\n DAT:.half 1,2.align 2 VAR:.word 0x12345678 C1 evaluates to MSG evaluates to (Note: \n = Line Feed char. = 0x0A) DAT evaluates to VAR evaluates to 0x1001000C 0x10010008 0x10010004 0x10010000 Directives are used to initialize or reserve space for global variables in C short int count = 7; char message[16]; int table[8] = {0,1,2,3,4,5,6,7}; void main() { } count: message:.data.align table:.word.text main:.globl main C/C++ style global declarations Assembly equivalent Summary & Notes Assembler Directives: Tell the assembler how to build the program memory image Where instructions and data should be placed in memory when the program is loaded How to initialize certain global variables Recall, a compiler/assembler simply outputs a memory IMAGE of the program. It must then be loaded into memory by the OS to be executed. Key: Directives are NOT instructions! They are used by the assembler to create the memory image and then removed The MIPS processor never sees these directives! 10.59 PC Program Executing Directives in the Software Flow SLT BNE High Level Language Description int n = 0xC259; void main(){ if (x > 0) x = x + y - z; a = b*x;.c/.cpp files Compiler the processor NEVER sees/executes these directives Loader / OS.data MOVE.W X,D0 n:.word CMPI.W 0xC259 #0,D0.text BLE SKIP SLT ADD $4,$2,$0 Y,D0 BNE SUB SKIP Z,D0 SKIP MUL SKIP: MUL Assembly (.asm/.s files) 1110 0010 0101 1001 0110 1011 0000 1100 0100 1101 0111 1111 1010 1100 0010 1011 0001 0110 0011 1000 Executable Binary Image Assembler 1110 1110 0010 0010 0101 0101 1001 1001 0110 0110 1011 1011 0000 0000 1100 1100 0100 0100 1101 1101 0111 0111 1111 1111 1010 1010 1100 1100 0010 0010 1011 1011 0001 0001 0110 0110 0011 0011 1000 1000 Object/Machine Code (.o files) Linker 10.60 Assembler Directives are used to create the object code (executable) image

10.61 10.62 Pseudo-instructions Macros translated by the assembler to instructions actually supported by the HW Simplifies writing code in assembly Example LI (Load-immediate) pseudoinstruction translated by assembler to 2 instruction sequence (LUI & ORI) Pseudo-instruction NOT Rd,Rs NEG Rd,Rs LI Rt, immed. # Load Immediate LA Rt, label # Load Address BLT Rs,Rt,Label Pseudo-instructions Actual Assembly NOR Rd,Rs,$0 SUB Rd,$0,Rs LUI Rt, {immediate[31:16], 16 b0} ORI Rt, {16 b0, immediate[15:0]} LUI Rt, {immediate[31:16], 16 b0} ORI Rt, {16 b0, immediate[15:0]} SLT $1,Rs,Rt BNE $1,$0,Label Note: Pseudoinstructions are assembler-dependent. See MARS Help for more details. li $2, 0x12345678 lui $2, 0x1234 ori $2, $2, 0x5678 With pseudo-instruction After assembler 10.63 10.64 Support for Pseudo-instructions Pseudo-instructions often expand to several instructions and there is a need for usage of a temporary register Assembler reserves register $1 In the assembler, $1 = $at (assembler temp.) You can use $1 but it will be overwritten when you use certain pseudo-instructions Coding Exercise with MARS int x = 7, y = 5, z = 3; z = x * z + (x y++) # #DEFINE MASK 0xe0; # PORTD &= ~(MASK) # PORTD = ((x << 5) & MASK);

10.65 10.66 Components of an ISA What are the common features of all processor instruction sets? INSTRUCTION SET ARCHITECTURE 1. and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support SUBtract instruc. vs. NEGate + ADD instrucs. 3. of instructions How is the operation and operands represented with 1 s and 0 s 4. accessible to the instructions Faster than accessing data from memory 5. Addressing Modes How instructions can specify location of data operands 10.67 10.68 Historic Progression of Data Size & Registers General Instruction Format Issues Processor Year Trans. Count Data Size GPRs 8088 1979 29K 8 8 80286 1982 134K 16 8 80386/486 85/ 89 275K/1.1 8M 32 8 Pentium 1993 3.1M 32 >8 Pentium 4 2000 42M 32 >= 128 Core 2 Duo 2006 291M 64 >= 128 Instructions must specify three things: Source operands Usually 2 source operands (e.g. X+Y) Example: ADD $8, $9, $10 ($8 = $9 + $10 where $ = Register) Binary (machine-code) representation broken into fields of bits for each part 6-core Core i7 2011 2.27B 64 >= 128 MIPS 1999 var. 32 32 OpCode Src. 1 Src. 2 Dest. Shift Amount 000000 01001 01010 01000 00000 Arith. $9 $10 $8 Unused Function 100000 Add

10.69 10.70 Historical Instruction Formats Different instruction sets specify these differently 3 operand instruction set (MIPS, PPC, ARM) Similar to example on previous page Format: ADD DST, SRC1, SRC2 (DST = SRC1 + SRC2) 2 operand instructions (Intel / Motorola 68K) Second operand doubles as source and destination Format: ADD SRC1, S2/D (S2/D = SRC1 + S2/D) 1 operand instructions (Old Intel FP, Low-End Embedded) Implicit operand to every instruction usually known as the Accumulator (or ACC) register Format: ADD SRC1 (ACC = ACC + SRC1) Historical Instruction Format Examples Consider the pros and cons of each format when performing the set of operations F = X + Y Z G = A + B Simple embedded computers often use single operand format Smaller data size (8-bit or 16-bit machines) means limited instruc. size Modern, high performance processors use 2- and 3-operand formats Single-Operand Two-Operand Three-Operand LOAD X ADD Y SUB Z STORE F LOAD A ADD B STORE G (+) Smaller size to encode each instruction (-) Higher instruction count to load and store ACC value MOVE F,X ADD F,Y SUB F,Z MOVE G,A ADD G,B Compromise of two extremes ADD F,X,Y SUB F,F,Z ADD G,A,B (+) More natural program style (+) Smaller instruction count (-) Larger size to encode each instruction 10.71 MIPS Instruction Format operand format Most ALU instructions use 3 registers as their operands All operations are performed on entire 32- bits (no size distinction) Example: ADD $t0, $t1, $t2 architecture Load (read) data values from memory into a register Perform operations on registers Store (write) data values back to memory Different load/store instructions for different operand sizes (i.e. byte, half, word) Load/Store Architecture Proc. Mem. 1.) Load operands to proc. registers Proc. Mem. 2.) Proc. Performs operation using register values Proc. Mem. 3.) Store results back to memory