CS61C : Machine Structures

Similar documents
CS61C : Machine Structures

CS61C : Machine Structures

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

CS61C L10 MIPS Instruction Representation II, Floating Point I (6)

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

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

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

CS 61c: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

CS61C : Machine Structures

Lecture 9: Disassembly

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

CS 61C: Great Ideas in Computer Architecture. MIPS Instruc,on Representa,on II. Dan Garcia

UCB CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

CS61C : Machine Structures

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

UC Berkeley CS61C : Machine Structures

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

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

CS61C : Machine Structures

CS61C Machine Structures. Lecture 13 - MIPS Instruction Representation I. 9/26/2007 John Wawrzynek. www-inst.eecs.berkeley.

UCB CS61C : Machine Structures

CS61C : Machine Structures

Review. Disassembly is simple and starts by decoding opcode field. Lecture #13 Compiling, Assembly, Linking, Loader I

Review Reserve exponents, significands: Exponent Significand Object nonzero Denorm anything +/- fl. pt. # /- 255 nonzero NaN

Where Are We Now? Linker (1/3) Linker (2/3) Four Types of Addresses we ll discuss. Linker (3/3)

CS61C : Machine Structures

CS61C : Machine Structures

UC Berkeley CS61C : Machine Structures

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

Review. N-bit adder-subtractor done using N 1- bit adders with XOR gates on input. Lecture #19 Designing a Single-Cycle CPU

UCB CS61C : Machine Structures

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

CS 110 Computer Architecture MIPS Instruction Formats

Assembler. #13 Running a Program II

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

Review 1/2 MIPS assembly language instructions mapped to numbers in 3 formats. CS61C Negative Numbers and Logical Operations R I J.

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

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

CS 61C: Great Ideas in Computer Architecture RISC-V Instruction Formats

9/14/17. Levels of Representation/Interpretation. Big Idea: Stored-Program Computer

CS61C : Machine Structures

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2. Instruction Set Architecture (ISA)

Clever Signed Adder/Subtractor. Five Components of a Computer. The CPU. Stages of the Datapath (1/5) Stages of the Datapath : Overview

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

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

UC Berkeley CS61C : Machine Structures

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

Unsigned Binary Integers

Unsigned Binary Integers

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

CS 110 Computer Architecture MIPS Instruction Formats

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

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

bits 5..0 the sub-function of opcode 0, 32 for the add instruction

Review. Lecture #7 MIPS Decisions So Far...

UCB CS61C : Machine Structures

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

CS3350B Computer Architecture MIPS Instruction Representation

CS61C : Machine Structures

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

Review C program: foo.c Compiler Assembly program: foo.s Assembler Object(mach lang module): foo.o

MIPS (SPIM) Assembler Syntax

ECE 486/586. Computer Architecture. Lecture # 7

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

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

CS3350B Computer Architecture MIPS Introduction

CS61C : Machine Structures

Lecture 4: MIPS Instruction Set

CS31001 COMPUTER ORGANIZATION AND ARCHITECTURE. Debdeep Mukhopadhyay, CSE, IIT Kharagpur. Instructions and Addressing

COMP MIPS instructions 2 Feb. 8, f = g + h i;

Computer Architecture

CS3350B Computer Architecture

Lecture #6 Intro MIPS; Load & Store

CS61C : Machine Structures

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

Grading: 3 pts each part. If answer is correct but uses more instructions, 1 pt off. Wrong answer 3pts off.

Course Administration

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

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

CS222: MIPS Instruction Set

Chapter 2: Instructions:

Computer Organization MIPS ISA

Topic Notes: MIPS Instruction Set Architecture

The MIPS Instruction Set Architecture

Instructions: Language of the Computer

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 18 CPU Design: The Single-Cycle I ! Nasty new windows vulnerability!

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Inequalities in MIPS (2/4) Inequalities in MIPS (1/4) Inequalities in MIPS (4/4) Inequalities in MIPS (3/4) UCB CS61C : Machine Structures

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

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

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

Outline. Lecture 40 Hardware Parallel Computing Thanks to John Lazarro for his CS152 slides inst.eecs.berkeley.

Transcription:

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 14 Introduction to MIPS Instruction Representation II Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia Are you P2P sharing fans? Two news items: (1) The US Supreme court has decided to hear the landmark P2P case MGM vs Grokster, & (2) Napster was cracked, days after release! www.cnn.com/2005/law/02/16/hilden.fileswap/ www.stuff.co.nz/stuff/0,2106,3189925a28,00.html CS61C L14 Introduction to MIPS: Instruction Representation II (1)

I-Format Problems (0/3) Problem 0: Unsigned # sign-extended? addiu, sltiu, sign-extends immediates to 32 bits. Thus, # is a signed integer. Rationale addiu so that can add w/out overflow - See K&R pp. 230, 305 sltiu suffers so that we can have ez HW - Does this mean we ll get wrong answers? - Nope, it means assembler has to handle any unsigned immediate 2 15 n < 2 16 (I.e., with a 1 in the 15th bit and 0s in the upper 2 bytes) as it does for numbers that are too large. CS61C L14 Introduction to MIPS: Instruction Representation II (2)

I-Format Problems (1/3) Problem 1: Chances are that addi, lw, sw and slti will use immediates small enough to fit in the immediate field. but what if it s too big? We need a way to deal with a 32-bit immediate in any I-format instruction. CS61C L14 Introduction to MIPS: Instruction Representation II (3)

I-Format Problems (2/3) Solution to Problem 1: Handle it in software + new instruction Don t change the current instructions: instead, add a new instruction to help out New instruction: lui register, immediate stands for Load Upper Immediate takes 16-bit immediate and puts these bits in the upper half (high order half) of the specified register sets lower half to 0s CS61C L14 Introduction to MIPS: Instruction Representation II (4)

I-Format Problems (3/3) Solution to Problem 1 (continued): So how does lui help us? Example: addi becomes: lui ori add $t0,$t0, 0xABABCDCD $at, 0xABAB $at, $at, 0xCDCD $t0,$t0,$at Now each I-format instruction has only a 16- bit immediate. Wouldn t it be nice if the assembler would this for us automatically? (later) CS61C L14 Introduction to MIPS: Instruction Representation II (5)

Branches: PC-Relative Addressing (1/5) Use I-Format opcode rs rt immediate opcode specifies beq v. bne rs and rt specify registers to compare What can immediate specify? Immediate is only 16 bits PC (Program Counter) has byte address of current instruction being executed; 32-bit pointer to memory So immediate cannot specify entire address to branch to. CS61C L14 Introduction to MIPS: Instruction Representation II (6)

Branches: PC-Relative Addressing (2/5) How do we usually use branches? Answer: if-else, while, for Loops are generally small: typically up to 50 instructions Function calls and unconditional jumps are done using jump instructions (j and jal), not the branches. Conclusion: may want to branch to anywhere in memory, but a branch often changes PC by a small amount CS61C L14 Introduction to MIPS: Instruction Representation II (7)

Branches: PC-Relative Addressing (3/5) Solution to branches in a 32-bit instruction: PC-Relative Addressing Let the 16-bit immediate field be a signed two s complement integer to be added to the PC if we take the branch. Now we can branch ± 2 15 bytes from the PC, which should be enough to cover almost any loop. Any ideas to further optimize this? CS61C L14 Introduction to MIPS: Instruction Representation II (8)

Branches: PC-Relative Addressing (4/5) Note: Instructions are words, so they re word aligned (byte address is always a multiple of 4, which means it ends with 00 in binary). So the number of bytes to add to the PC will always be a multiple of 4. So specify the immediate in words. Now, we can branch ± 2 15 words from the PC (or ± 2 17 bytes), so we can handle loops 4 times as large. CS61C L14 Introduction to MIPS: Instruction Representation II (9)

Branches: PC-Relative Addressing (5/5) Branch Calculation: If we don t take the branch: PC = PC + 4 PC+4 = byte address of next instruction If we do take the branch: Observations PC = (PC + 4) + (immediate * 4) - Immediate field specifies the number of words to jump, which is simply the number of instructions to jump. - Immediate field can be positive or negative. - Due to hardware, add immediate to (PC+4), not to PC; will be clearer why later in course CS61C L14 Introduction to MIPS: Instruction Representation II (10)

Branch Example (1/3) MIPS Code: Loop: beq $9,$0,End add $8,$8,$10 addi $9,$9,-1 j Loop End: beq branch is I-Format: opcode = 4 (look up in table) rs = 9 (first operand) rt = 0 (second operand) immediate =??? CS61C L14 Introduction to MIPS: Instruction Representation II (11)

Branch Example (2/3) MIPS Code: Loop: beq $9,$0,End addi $8,$8,$10 addi $9,$9,-1 j Loop End: Immediate Field: Number of instructions to add to (or subtract from) the PC, starting at the instruction following the branch. In beq case, immediate = 3 CS61C L14 Introduction to MIPS: Instruction Representation II (12)

Branch Example (3/3) MIPS Code: Loop: beq $9,$0,End addi $8,$8,$10 addi $9,$9,-1 j Loop End: decimal representation: 4 9 0 3 binary representation: 000100 01001 00000 0000000000000011 CS61C L14 Introduction to MIPS: Instruction Representation II (13)

Questions on PC-addressing Does the value in branch field change if we move the code? What do we do if destination is > 2 15 instructions away from branch? Since it s limited to ± 2 15 instructions, doesn t this generate lots of extra MIPS instructions? Why do we need all these addressing modes? Why not just one? CS61C L14 Introduction to MIPS: Instruction Representation II (14)

Administrivia Dan s OH cancelled this next week He ll be out of town Homework 2 graded They ll be frozen next week CS61C L14 Introduction to MIPS: Instruction Representation II (15)

Upcoming Calendar Week # #6 Next week #7 Following week #8 Mon President s Day Holiday MIPS Inst Format III Wed TA Floating Pt I (No Dan OH) Running Program I Thu Lab Floating Pt (No Dan OH) Running Program Fri TA Floating Pt II Running Program II Midterm week Sun 2pm Review 10 Evans SDS I Midterm 7pm- 10pm 1 Le Conte SDS II SDS SDS III CS61C L14 Introduction to MIPS: Instruction Representation II (16)

J-Format Instructions (1/5) For branches, we assumed that we won t want to branch too far, so we can specify change in PC. For general jumps (j and jal), we may jump to anywhere in memory. Ideally, we could specify a 32-bit memory address to jump to. Unfortunately, we can t fit both a 6-bit opcode and a 32-bit address into a single 32-bit word, so we compromise. CS61C L14 Introduction to MIPS: Instruction Representation II (17)

J-Format Instructions (2/5) Define fields of the following number of bits each: 6 bits 26 bits As usual, each field has a name: opcode target address Key Concepts Keep opcode field identical to R-format and I-format for consistency. Combine all other fields to make room for large target address. CS61C L14 Introduction to MIPS: Instruction Representation II (18)

J-Format Instructions (3/5) For now, we can specify 26 bits of the 32-bit bit address. Optimization: Note that, just like with branches, jumps will only jump to word aligned addresses, so last two bits are always 00 (in binary). So let s just take this for granted and not even specify them. CS61C L14 Introduction to MIPS: Instruction Representation II (19)

J-Format Instructions (4/5) Now specify 28 bits of a 32-bit address Where do we get the other 4 bits? By definition, take the 4 highest order bits from the PC. Technically, this means that we cannot jump to anywhere in memory, but it s adequate 99.9999 % of the time, since programs aren t that long - only if straddle a 256 MB boundary If we absolutely need to specify a 32-bit address, we can always put it in a register and use the jr instruction. CS61C L14 Introduction to MIPS: Instruction Representation II (20)

J-Format Instructions (5/5) Summary: New PC = { PC[31..28], target address, 00 } Understand where each part came from! Note: {,, } means concatenation { 4 bits, 26 bits, 2 bits } = 32 bit address { 1010, 11111111111111111111111111, 00 } = 10101111111111111111111111111100 Note: Book uses, Verilog uses {,, } We will learn Verilog later in this class CS61C L14 Introduction to MIPS: Instruction Representation II (21)

Peer Instruction Question (for A,B) When combining two C files into one executable, recall we can compile them independently & then merge them together. A. Jump insts don t require any changes. B. Branch insts don t require any changes. C. You now have all the tools to be able to decompile a stream of 1s and 0s into C! CS61C L14 Introduction to MIPS: Instruction Representation II (22) ABC 1: FFF 2: FFT 3: FTF 4: FTT 5: TFF 6: TFT 7: TTF 8: TTT

R I J In conclusion MIPS Machine Language Instruction: 32 bits representing a single instruction opcode rs rt rd shamt funct opcode rs rt immediate opcode target address Branches use PC-relative addressing, Jumps use absolute addressing. Disassembly is simple and starts by decoding opcode field. (more in a week) CS61C L14 Introduction to MIPS: Instruction Representation II (23)