The Instruction Set Architecture (ISA)

Similar documents
EEC 581 Computer Architecture Lecture 1 Review MIPS

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

All instructions have 3 operands Operand order is fixed (destination first)

Communicating with People (2.8)

Chapter 2A Instructions: Language of the Computer

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Systems Architecture I

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

MIPS ISA-II: Procedure Calls & Program Assembly

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

Computer Architecture

CS3350B Computer Architecture MIPS Instruction Representation

Chapter 2. Instruction Set Architecture (ISA)

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

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

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

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

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

ECE 486/586. Computer Architecture. Lecture # 8

CENG3420 Lecture 03 Review

Lecture Topics. Branch Condition Options. Branch Conditions ECE 486/586. Computer Architecture. Lecture # 8. Instruction Set Principles.

Instruction Set Architectures

Instructions: Language of the Computer

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

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

Lecture 3: The Instruction Set Architecture (cont.)

Lecture 3: The Instruction Set Architecture (cont.)

Architecture I. Computer Systems Laboratory Sungkyunkwan University

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

Instruction Set Architecture. "Speaking with the computer"

Chapter 2: Instructions:

Chapter 2. Instructions:

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Computer Organization MIPS ISA

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

Computer Architecture

Thomas Polzer Institut für Technische Informatik

Lecture 3: Instruction Set Architecture

History of the Intel 80x86

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

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Instruction Set Principles. (Appendix B)

CS222: MIPS Instruction Set

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

Unsigned Binary Integers

Unsigned Binary Integers

Computer Architecture. MIPS Instruction Set Architecture

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

COMPUTER ORGANIZATION AND DESIGN

ENE 334 Microprocessors

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

Rechnerstrukturen. Chapter 2. Instructions: Language of the Computer

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Course Administration

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

2.7 Supporting Procedures in hardware. Why procedures or functions? Procedure calls

Instructions: Language of the Computer

CS/COE1541: Introduction to Computer Architecture

LECTURE 2: INSTRUCTIONS

Chapter 2. Instructions: Language of the Computer

CO Computer Architecture and Programming Languages CAPL. Lecture 13 & 14

Chapter 3. Instructions:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

CENG3420 L03: Instruction Set Architecture

MIPS Instruction Set Architecture (2)

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

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

CS3350B Computer Architecture

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

Chapter 2. Baback Izadi Division of Engineering Programs

Reduced Instruction Set Computer (RISC)

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Computer Systems Laboratory Sungkyunkwan University

Topic Notes: MIPS Instruction Set Architecture

ECE 154A Introduction to. Fall 2012

Chapter 2. Instructions: Language of the Computer. Jiang Jiang

MIPS Instruction Set Architecture (1)

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

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

ECE232: Hardware Organization and Design

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

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

CS3350B Computer Architecture MIPS Introduction

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

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

Computer Architecture Computer Science & Engineering. Chapter 2. Instructions: Language of the Computer BK TP.HCM

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

Reduced Instruction Set Computer (RISC)

Instruction Set Design and Architecture

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

Transcription:

The Instruction Set Architecture (ISA) Lecture notes from MKP, H. H. Lee and S. Yalamanchili Understand how programs are encoded What does the OS (loader) see? Impact of ISA on program encodings Why are ISAs different? Goals (2) 1

Reading Chapter 2 2.1, Figure 2.1, 2.2 2.7 2.9, Figure 2.15 (2.9.1 in online text) 2.10, 2.16, 2.17 Appendix A9, A10 Practice Problems: 1,2,8, 15 Note: Appendices A-E in the hardcopy text correspond to chapters 7-11 in the online text. (3) Module Outline Reiew ISA (MIPS) and understand instruction encodings Arithmetic and Logical Instructions Reiew memory organization Memory (data moement) instructions Control flow instructions Procedure/Function calls Not Coered here - Reiew on your own from ECE 2035 (will not be tested) (4) 2

Instruction Set Architecture A ery important abstraction interface between hardware and low-leel software standardizes instructions, machine language bit patterns, etc. adantage: different implementations of the same architecture disadantage: sometimes preents using new innoations Modern instruction set architectures: 80x86 (aka ia32), PowerPC (e.g. G4, G5) Xscale, ARM, MIPS Intel/HP EPIC (ia64), AMD64, Intel s EM64T, SPARC, HP PA-RISC, DEC/Compaq/HP Alpha (5) Economics of an ISA Thermal Design Power 130W 3.6 GHz Thermal Design Power 4W 1.6 GHz Software/binary portability (6) 3

Instructions We ll be working with the MIPS instruction set architecture Representatie of Reduced Instruction Set Computer (RISC) Similar to other architectures deeloped since the 1980's Used by NEC, Nintendo, Silicon Graphics, Sony Design goals: Maximize performance and Minimize cost, Reduce design time (7) Instruction Set Architecture (ISA) byte addressed memory 0x00 0x01 0x02 0x03 Register File Data flow for transfers 0x00000000 0x00000001 0x00000002 0x00000003 0x1F Data flow for computation Address Space compiler ISA HW Arithmetic Logic Unit (ALU) RISC s. CISC 0xFFFFFFFF (8) 4

Basic Principles What are the operations supported in hardware à opcodes Where are the source and destination data or operands for the instruction? Memory, registers, caches, etc. How is the location of the operands determined Address in memory, and register name Addressing modes How is control flow supported? (9) Module Outline Reiew ISA and understand instruction encodings Arithmetic and Logical Instructions Reiew memory organization Memory (data moement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding (10) 5

MIPS Programmer Visible Registers Register Names Usage by Software Conention $0 $zero Hardwired to zero $1 $at Resered by assembler $2 - $3 $0 - $1 Function return result registers $4 - $7 $a0 - $a3 Function passing argument alue registers $8 - $15 $t0 - $t7 Temporary registers, caller saed $16 - $23 $s0 - $s7 Saed registers, callee saed $24 - $25 $t8 - $t9 Temporary registers, caller saed $26 - $27 $k0 - $k1 Resered for OS kernel $28 $gp Global pointer $29 $sp Stack pointer $30 $fp Frame pointer $31 $ra Return address (pushed by call instruction) $hi $hi High result register (remainder/di, high word/mult) $lo $lo Low result register (quotient/di, low word/mult) (11) MIPS Register View Arithmetic instruction operands must be registers Compiler associates ariables with registers Other registers that are not isible to the programmer, Program counter Status register Kernel registers Page table pointer Program execution control Operating system state (12) 6

Instruction Set Architecture (ISA) byte addressed memory 0x00 0x01 0x02 0x03 Register File Data flow for transfers 0x00000000 0x00000001 0x00000002 0x00000003 0x1F Data flow for computation Address Space compiler ISA HW Arithmetic Logic Unit (ALU) RISC s. CISC 0xFFFFFFFF (13) MIPS arithmetic Design Principle 1: simplicity faors regularity. Of course this complicates some things... C code: A = B + C + D; E = F - A; RA & scheduling compiler ISA HW MIPS code: add $t0, $s1, $s2 add $s0, $t0, $s3 sub $s4, $s5, $s0 andi $3, $4, $5.. Note the need for intermediate registers Operands must be registers, only 32 registers proided All memory accesses accomplished ia loads and stores A common feature of RISC processors (14) 7

Logical Operations Instructions for bitwise manipulation Operation C Jaa MIPS Shift left << << sll Shift right >> >>> srl Bitwise AND & & and, andi Bitwise OR or, ori Bitwise NOT ~ ~ nor n Useful for extracting and inserting groups of bits in a word (15) Encoding: Instruction Format R-Format op rs rt rd shamt func Opcode (operation) source operand 1 source operand 2 shift amount destination operand function field Instructions, like registers and words of data, are also 32 bits long Example: add $t0, $s1, $s2 registers hae numbers, $t0=9, $s1=17, $s2=18 Opcodes on page A-50 (Figure 7.10.2) Encodings Section A10 (7.10) (16) 8

MIPS Encoding: R-Type 31 26 25 21 20 16 15 11 10 6 5 0 opcode rs rt rd rd shamt funct add $4, $3, $2 rt rs 31 0 0 26 25 21 20 16 15 11 10 6 5 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 opcode rs rt rd shamt funct 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 Encoding = 0x00622020 (17) MIPS Encoding: R-Type 31 26 25 21 20 16 15 11 10 6 5 0 opcode rs rt rd shamt funct rd sll $3, $5, 7 rt shamt 31 0 0 26 25 21 20 16 15 11 10 6 5 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 1 1 1 0 0 0 0 0 0 opcode rs rt rd shamt funct 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 1 1 1 0 0 0 0 0 0 Encoding = 0x000519C0 SPIM Example (18) 9

Module Outline Reiew ISA and understand instruction encodings Arithmetic and Logical Instructions Reiew memory organization Memory (data moement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding (19) Memory Organization Viewed as a large, single-dimension array, with an address. A memory address is an index into the array "Byte addressing" means that the index points to a byte of memory. 0 1 2 3 4 5 6... 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data 8 bits of data (20) 10

Memory Organization Bytes are nice, but most data items use larger "words MIPS proides lw/lh/lb and sw/sh/sb instructions For MIPS, a word is 32 bits or 4 bytes. 0 4 8 12 32 bits of data 32 bits of data 32 bits of data 32 bits of data Registers hold 32 bits of data 2 32 bytes... with byte addresses from 0 to 2 32-1 2 30 words with byte addresses 0, 4, 8,... 2 32-4 Words are aligned i.e., what are the least 2 significant bits of a word address? (21) Endianness [defined by Danny Cohen 1981] Byte ordering ¾ How is a multiple byte data word stored in memory Endianness (from Gullier s Traels) Big Endian o Most significant byte of a multi-byte word is stored at the lowest memory address o e.g. Sun Sparc, PowerPC Little Endian o Least significant byte of a multi-byte word is stored at the lowest memory address o e.g. Intel x86 Some embedded & DSP processors would support both for interoperability (22) 11

Example of Endian o Store 0x87654321 at address 0x0000, byte-addressable 0x0000 0x0001 0x87 0x65 Lower Memory Address 0x0000 0x0001 0x21 0x43 Lower Memory Address 0x0002 0x43 0x0002 0x65 0x0003 0x21 0x0003 0x87 BIG ENDIAN Higher Memory Address LITTLE ENDIAN Higher Memory Address (23) For placement of data in memory.data.word 0x1234.byte 0x08.asciiz Hello World.ascii Hello World.align 2.space 64 Data Directies 0x7FFFFFFF 0x10010000 0x00400000 0x00000000 Memory Map Stack Heap Data Text resered Example: See page A-47 (7.10.1) (24) 12

Instruction Set Architecture (ISA) byte addressed memory 0x00 0x01 0x02 0x03 Register File Data flow for transfers 0x00000000 0x00000001 0x00000002 0x00000003 words 0x1F Data flow for computation Address Space compiler Arithmetic Logic Unit (ALU) ISA HW 0xFFFFFFFF (25) Module Outline Reiew ISA and understand instruction encodings Arithmetic and Logical Instructions Reiew memory organization Memory (data moement) instructions Control flow instructions Procedure/Function calls ECE 2035 (26) 13

Memory Instructions Load & store instructions: Orthogonal ISA Example: C code: long A[100]; A[9] = h + A[8]; MIPS code: lw $t0, 32($s3) #load word add $t0, $s2, $t0 sw $t0, 36($s3) A[0] A[1] A[2] 4 bytes 32 bits of data 32 bits of data 32 bits of data 32 bits of data index base register Remember arithmetic operands are registers, not memory! compiler data layout ISA HW (27) MIPS Registers Register Names Usage by Software Conention $0 $zero Hardwired to zero $1 $at Resered by assembler $2 - $3 $0 - $1 Function return result registers $4 - $7 $a0 - $a3 Function passing argument alue registers $8 - $15 $t0 - $t7 Temporary registers, caller saed $16 - $23 $s0 - $s7 Saed registers, callee saed $24 - $25 $t8 - $t9 Temporary registers, caller saed $26 - $27 $k0 - $k1 Resered for OS kernel $28 $gp Global pointer $29 $sp Stack pointer $30 $fp Frame pointer $31 $ra Return address (pushed by call instruction) $hi $hi High result register (remainder/di, high word/mult) $lo $lo Low result register (quotient/di, low word/mult) ISA defines what registers can be used for address calculations (28) 14

Encoding Memory Instructions Consider the load-word and store-word instructions, What would the regularity principle hae us do? Design Principle 3: Good design demands a compromise Introduce a new type of instruction format I-type for data transfer instructions other format was R-type for register Example: lw $t0, 32($s2) 35 18 8 32 op rs rt 16 bit number Orthogonal ISA (29) MIPS Encoding: I-Type 31 26 25 21 20 16 15 0 opcode rs rt Immediate Value rt lw $5, 3000($2) Immediate rs 31 1 0 26 25 21 20 16 15 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0 opcode rs rt 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0 Encoding = 0x8C450BB8 Immediate Value (30) 15

MIPS Encoding: I-Type 31 26 25 21 20 16 15 0 opcode rs rt Immediate Value rt sw $5, 3000($2) Immediate rs 31 1 0 26 25 21 20 16 15 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0 opcode rs rt 1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0 Encoding = 0xAC450BB8 Immediate Value SPIM Example (31) Instruction Set Architecture (ISA) 0x00 0x01 0x02 0x03 Register File Data flow for transfers 0x00000000 0x00000001 0x00000002 0x00000003 byte addressed memory 0x1F Data flow for computation Address Space compiler ISA HW Arithmetic Logic Unit (ALU) RISC à Orthogonal ISA 0xFFFFFFFF (32) 16

Constants Small constants are used quite frequently (50% of operands) e.g., A = A + 5; B = B + 1; C = C - 18; Solutions? put 'typical constants' in memory and load them. create hard-wired registers (like $zero) for constants like one. Use immediate alues MIPS Instructions: addi $29, $29, 4 slti $8, $18, 10 andi $29, $29, 6 ori $29, $29, 4 compiler ISA HW HW support (33) Immediate Operands No subtract immediate instruction Just use a negatie constant addi $s2, $s1, -1 Hardwired alues useful for common operations E.g., moe between registers add $t2, $s1, $zero Design Principle 4: Make the common case fast Small constants are common Immediate operand aoids a load instruction (34) 17

How about larger constants? We'd like to be able to load a 32 bit constant into a register Must use two instructions, new "load upper immediate" instruction lui $t0, 1010101010101010 filled with zeros 1010101010101010 0000000000000000 Then must get the lower order bits right, i.e., ori $t0, $t0, 1010101010101010 1010101010101010 0000000000000000 Now consider la $t0, L1 (a pseudo instruction) ori 0000000000000000 1010101010101010 1010101010101010 1010101010101010 compiler ISA HW Instruction synthesis (35) 2s-Complement Signed Integers Bit 31 is sign bit 1 for negatie numbers 0 for non-negatie numbers ( 2 n 1 ) can t be represented Non-negatie numbers hae the same unsigned and 2s-complement representation Some specific numbers 0: 0000 0000 0000 1: 1111 1111 1111 Most-negatie: 1000 0000 0000 Most-positie: 0111 1111 1111 (36) 18

Sign Extension Representing a number using more bits Presere the numeric alue In MIPS instruction set addi: extend immediate alue lb, lh: extend loaded byte/halfword beq, bne: extend the displacement Replicate the sign bit to the left c.f. unsigned alues: extend with 0s Examples: 8-bit to 16-bit +2: 0000 0010 => 0000 0000 0000 0010 2: 1111 1110 => 1111 1111 1111 1110 (37) Encoding: Constants & Immediates Use the I-format Compromise: Use instruction sequences to construct larger constants Aoid another adding another format à impact on the hardware? Example (38) 19

Module Outline Reiew ISA and understand instruction encodings Arithmetic and Logical Instructions Reiew memory organization Memory (data moement) instructions Control flow instructions Procedure/Function calls Program assembly, linking, & encoding (39) Control Decision making instructions alter the control flow, i.e., change the "next" instruction to be executed MIPS conditional branch instructions: bne $t0, $t1, Label beq $t0, $t1, Label Example: if (i==j) h = i + j; bne $s0, $s1, Label add $s3, $s0, $s1 Label:... (40) 20

Control MIPS unconditional branch instructions: j label Example: Assembler calculates address if (i!=j) beq $s4, $s5, Lab1 h=i+j; add $s3, $s4, $s5 else j Lab2 h=i-j; Lab1: sub $s3, $s4, $s5 Lab2:... Can you build a simple for loop? compiler ISA HW HW support (41) Compiling Loop Statements C code: while (sae[i] == k) i += 1; i in $s3, k in $s5, address of sae in $s6 Compiled MIPS code: Loop: sll $t1, $s3, 2 #multiply by 4 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j Loop Exit: (42) 21

Control Flow We hae: beq, bne, what about Branch-if-less-than? New instruction: if $s1 < $s2 then $t0 = 1 slt $t0, $s1, $s2 else $t0 = 0 Can use this instruction to build "blt $s1, $s2, Label" can now build general control structures For ease of assembly programmers, the assembler allows blt as a pseudo-instruction assembler substitutes them with alid MIPS instructions there are policy of use conentions for registers compiler ISA HW blt $4 $5 loop Þ slt $1 $4 $5 bne $1 $0 loop pseudo-instructions (43) Signed comparison: slt, slti Signed s. Unsigned Unsigned comparison: sltu, sltui Example $s0 = 1111 1111 1111 1111 1111 1111 1111 1111 $s1 = 0000 0000 0000 0000 0000 0000 0000 0001 slt $t0, $s0, $s1 # signed o 1 < +1 Þ $t0 = 1 sltu $t0, $s0, $s1 # unsigned o +4,294,967,295 > +1 Þ $t0 = 0 (44) 22

Instructions: bne $t4,$t5,label Next instruction is at Label if $t4 ¹ $t5 beq $t4,$t5,label Next instruction is at Label if $t4 = $t5 j Label Formats: I J Next instruction is at Label op rs rt 16 bit address op Encoding: Branches & Jumps Opcodes on page A-50 (Figure 7.10.2) Encodings Section A10 (7.10) 26 bit address Use Instruction Address Register (PC = program counter) Most branches are local (principle of locality) Jump instructions just use high order bits of PC address boundaries of 256 MB (45) BEQ/BNE uses I-Type 31 26 25 21 20 16 15 0 opcode rs rt Signed Offset Value (encoded in words, e.g. 4-bytes) rs beq $0, $9, 40 rt Offset Encoded by 40/4 = 10 31 0 0 26 25 21 20 16 15 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 opcode rs rt 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 Encoding = 0x1009000A Immediate Value (46) 23

MIPS Encoding: J-Type 31 26 25 0 opcode Target Address jal will jump and push return address in $ra ($31) jal 0x00400030 Target 0000 0000 0100 0000 0000 0000 0011 0000 X Target Address Instruction=4 bytes 31 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 opcode 26 25 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 Encoding = 0x0C10000C Target Address SPIM Example (47) JR JR (Jump Register) Unconditional jump 31 26 25 21 20 16 15 11 10 6 5 0 jr $2 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 opcode rs 0 0 0 funct (48) 24

Target Addressing Example Loop code from earlier example Assume Loop at location 80000 Loop: sll $t1, $s3, 2 80000 0 0 19 9 4 0 add $t1, $t1, $s6 80004 0 9 22 9 0 32 lw $t0, 0($t1) 80008 35 9 8 0 bne $t0, $s5, Exit 80012 5 8 21 2 addi $s3, $s3, 1 80016 8 19 19 1 j Loop 80020 2 20000 Exit: 80024 (49) Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites the code Example beq $s0,$s1, L1 bne $s0,$s1, L2 j L1 L2: (50) 25

Addressing Modes 1. Immediate addressing op rs rt Immediate compiler Operand is constant 2. Register addressing op rs rt rd... funct Registers ISA HW HW support Operand is in register 3. Base addressing Register op rs rt Address Memory lb $t0, 48($s0) Register + Byte Halfword Word 4. PC-relatie addressing op rs rt Address Memory bne $4, $5, Label (label will be assembled into a distance) PC + Word 5. Pseudodirect addressing j Label What does this imply about targets? op Address PC Concatenation w/ PC[31..28] Memory Word (51) MIPS operands Name Example Comments $s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform 32 registers $a0-$a3, $0-$1, $gp, arithmetic. MIPS register $zero always equals 0. Register $at is $fp, $sp, $ra, $at resered for the assembler to handle large constants. To Summarize Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so 2 30 memory Memory[4],..., sequential words differ by 4. Memory holds data structures, such as arrays, words Memory[4294967292] and spilled registers, such as those saed on procedure calls. MIPS assembly language Category Instruction Example Meaning Comments $s1, $s2, $s2 + $s3 operands; data in add add $s3 $s1 = Three registers Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to register store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory load upper immediate lui $s1, 100 16 $s1 = 100 * 2 Loads constant in upper 16 bits branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to PC + 4 + 100 branch on not equal bne $s1, $s2, 25 if ($s1!= $s2) go to PC + 4 + 100 Conditional branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else $s1 = 0 Equal test; PC-relatie branch Not equal test; PC-relatie Compare less than; for beq, bne set less than immediate slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; else $s1 = 0 Compare less than constant jump j 2500 go to 10000 Jump to target address Uncondi- jump register jr $ra go to $ra For switch, procedure return tional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call (52) 26

Summary To Date: MIPS ISA Simple instructions all 32 bits wide Very structured Only three instruction formats R I J op rs rt rd shamt funct op rs rt 16 bit address op 26 bit address Rely on compiler to achiee performance what are the compiler's goals? Help compiler where we can compiler ISA HW Full Example Opcodes on page A-50 (Figure 7.10.2) Encodings Section A10 (7.10) (53) Stored Program Computers The BIG Picture Instructions represented in binary, just like data Instructions and data stored in memory Programs can operate on programs e.g., compilers, linkers, Binary compatibility allows compiled programs to work on different computers Standardized ISAs (54) 27

Instruction Set Architecture (ISA) Register File (Programmer Visible State) 0x00 0x01 0x02 0x03 Memory Interface byte addressed memory stack 0x1F Processor Internal Buses Dynamic Data Data segment (static) Text Segment Program Counter Programmer Inisible State 0xFFFFFFFF Resered Instruction register Arithmetic Logic Unit (ALU) Memory Map Kernel registers Who sees what? (55) Instruction Set Architectures (ISA) Instruction set architectures are characterized by seeral features 1. Operations Types, precision, size 2. Organization of internal storage Stack machine Not coered here Accumulator General Purpose Registers (GPR) 3. Memory addressing Operand location and addressing (56) 28

Instruction Set Architectures 4. Memory abstractions Virtual address spaces (more later) Memory mapped I/O (later) 5. Control flow Condition codes Types of control transfers conditional s. unconditional ISA design is the result of many tradeoffs Decisions determine hardware implementation Impact on time, space, and energy Check out ISAs for PowerPC, ARM, x86, SPARC, etc. (57) Design alternatie: proide more powerful operations goal is to reduce number of instructions executed danger is a slower cycle time and/or a higher CPI Sometimes referred to as RISC s. CISC irtually all new instruction sets since 1982 hae been RISC VAX: minimize code size, make assembly language easy instructions from 1 to 54 bytes long! We will look at 80x86 Alternatie Architectures (58) 29

80x86 1978: The Intel 8086 is announced (16 bit architecture) 1980: The 8087 floating point coprocessor is added 1982: The 80286 increases address space to 24 bits, +instructions 1985: The 80386 extends to 32 bits, new addressing modes 1989-1995: The 80486, Pentium, Pentium Pro add a few instructions (mostly designed for higher performance) concurrency 1997: MMX (SIMD-INT) is added (PPMT and P-II) 1999: SSE (single prec. SIMD-FP and cacheability instructions) is added in P-III 2001: SSE2 (double prec. SIMD-FP) is added in P4 2004: Nocona introduced (compatible with AMD64 or once called x86-64) This history illustrates the impact of the golden handcuffs of compatibility adding new features as someone might add clothing to a packed bag an architecture that is difficult to explain and impossible to loe (59) Complexity: Instructions from 1 to 17 bytes long IA-32 Oeriew one operand must act as both a source and destination one operand can come from memory complex addressing modes e.g., base or scaled index with 8 or 32 bit displacement Saing grace: the most frequently used instructions are not too difficult to build compilers aoid the portions of the architecture that are slow what the 80x86 lacks in style is made up in quantity, making it beautiful from the right perspectie (60) 30

IA-32 Registers & Data Addressing Registers in the 32-bit subset that originated with 80386 Name Use 31 0 Accumulator registers EAX ECX GPR 0 GPR 1 Counter registers Stack registers Stack Base registers EDX EBX ESP EBP GPR 2 GPR 3 GPR 4 GPR 5 Data registers Base registers Source registers ESI GPR 6 Destination registers EDI CS GPR 7 Code segment pointer SS Stack segment pointer (top of stack) DS Data segment pointer 0 ES Data segment pointer 1 FS Data segment pointer 2 GS Data segment pointer 3 EIP Instruction pointer (PC) EFLAGS Condition codes (61) Basic x86 Addressing Modes Two operands per instruction n Source/dest operand Second source operand Register Register Register Immediate Register Memory Memory Register Memory Immediate Memory addressing modes n n Address in register Address = R base + displacement n Address = R base + 2 scale R index (scale = 0, 1, 2, or 3) n Address = R base + 2 scale R index + displacement (62) 31

IA-32 Register Restrictions Registers are not general purpose note the restrictions below (63) IA-32 Typical Instructions Four major types of integer instructions: Data moement including moe, push, pop Arithmetic and logical (destination register or memory) Control flow (use of condition codes / flags ) String instructions, including string moe and string compare (64) 32

IA-32 instruction Formats Typical formats: (notice the different lengths) a. JE EIP + displacement 4 4 8 JE b. CALL c. MOV EBX, [EDI + 45] 6 1 1 8 8 MOV d. PUSH ESI PUSH CALL Condition d w Reg Displacement 8 32 5 3 r/m Postbyte Displacement Offset e. ADD EAX, #6765 4 3 1 32 Variable length encoding Postfix bytes specify addressing mode Prefix bytes modify operation o Operand length, repetition, locking, ADD Reg w Immediate f. TEST EDX, #42 7 1 8 32 TEST w Postbyte Immediate (65) Instruction set design Tradeoffs between compiler complexity and hardware complexity Orthogonal (RISC) ISAs s. complex ISAs (more on this later in the class) Design Principles: simplicity faors regularity smaller is faster good design demands compromise make the common case fast Instruction set architecture a ery important abstraction indeed! Summary (66) 33

Study Guide What is i) an orthogonal instruction set, ii) load/store architecture, and iii) instruction set architecture? Translate small high leel language (e.g., C, Matlab) code blocks into MIPS assembly Allocate ariables to registers Layout data in memory Sequence data into/out of registers as necessary Write assembly instructions/program Write and execute the proceeding for A few simple if-then-else cases (say from C) for loops and while loops (67) Study Guide (cont.) Utilize data directies to layout data in memory Check anticipated layout in SPIM Layout a 2D matrix and a 3D matrix Layout a linked list Manually assemble instructions and check with SPIM Gien a program, encode branch and jump instructions Use SPIM to erify your answers remember SPIM branches are relatie to the PC not PC+4 Use SPIM to assemble some small programs Manually disassemble the code (68) 34

Study Guide (cont.) Name two adantages of a CISC ISA oer a RISC ISA Name two disadantages of a CISC ISA oer a RISC ISA What is the difference between big endian an little endian storage? Store a sequence of words in memory using the.byte directies. What are the alues at word boundaries assuming little endian or big endian format? (69) From a MIPS program Study Guide (cont.) Determine the number of bytes required for the text segment. You need to consider which instructions are pseudo instructions s. natie instructions Determine the alues of labels Is the code relocatable? Which instructions preent the code from being relocatable? What is the purpose of the symbol table? (70) 35

Glossary Big endian Binary compatibility Byte aligned memory access CISC Data directies Destination operand General purpose registers I-format Immediate operand Instruction encoding Instruction format Instruction set architecture J-format Little Endian Machine code (or language) Memory map (71) Glossary (cont.) Natie instructions Orthogonal ISA PC-relatie addressing Pseudo instructions R-format RISC Sign extension Source operand Symbol table Unsigned s. signed instructions Word aligned memory access (72) 36