CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

The University of Adelaide, School of Computer Science
CS 61C L13Introduction to MIPS: Instruction Representation I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Chapter 2 Instructions: Language of the Computer Part III.
CS2100 Computer Organisation MIPS Part III: Instruction Formats (AY2014/2015) Semester 2.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 08 MIPS Instruction Representation I Lecturer SOE Dan Garcia
CS61C L10 : MIPS Instruction Representation II (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 10 MIPS.
CS61C L14 Introduction to MIPS: Instruction Representation II (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 MIPS Instruction Representation I Shanghai Jiaotong University and Lanxiang.
CS 61C L09 Instruction Format (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #9: MIPS Instruction.
CS61C L14 MIPS Instruction Representation II (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L13 MIPS Instruction Representation I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
CS 61C L14Introduction to MIPS: Instruction Representation II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Chap Instruction Representation Jen-Chang Liu, Spring 2006 Adapted from
CS61C L13 Introduction to MIPS: Instruction Representation I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Instruction Representation II (1) Fall 2005 Lecture 10: Instruction Representation II.
CS61C L9 MIPS Logical & Shift Ops, and Instruction Representation I (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c.
Instruction Representation I (1) Fall 2005 Lecture 05: INSTRUCTION REPRESENTATION.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 MIPS Instruction Representation I The National Science Foundation (NSF)
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
CS61C L14 MIPS Instruction Representation II (1) Garcia, Fall 2006 © UCB Intel: 80 cores/chip in 5 yrs!  At their developer’s forum in SF on Tuesday,
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 14 MIPS Instruction Representation II IBM wants to use “self-assembling”
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
Topic 10: Instruction Representation CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and.
Character Data and 32-bit Constants (Lecture #20) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
Computer Organization CS224 Fall 2012 Lessons 9 and 10.
Instructor: Justin Hsia 7/03/2013Summer Lecture #71 CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats.
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
Chapter 2 CSF 2009 The MIPS Assembly Language: Introduction to Binary System.
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
ECE 15B Computer Organization Spring 2011 Dmitri Strukov Partially adapted from Computer Organization and Design, 4 th edition, Patterson and Hennessy,
CENG 311 Instruction Representation
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /22/2013 Lecture 12: Character Data Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
EET 4250 Instruction Representation & Formats Acknowledgements: Some slides and lecture notes for this course adapted from Prof. Mary Jane Penn.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
CS 61C: Great Ideas in Computer Architecture MIPS Functions, Instruction Formats 1 Instructors: Vladimir Stojanovic and Nicholas Weaver
Dan Garcia 1 CS 61C: Great Ideas in Computer Architecture MIPS Instruction Representation II.
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
Chapter 2 Instructions: Language of the Computer.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Instruction Representation. Levels of Representation (abstractions) High Level Language Program (e.g., C) Assembly Language Program (e.g.,MIPS) Machine.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 8, EE 334.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 08 MIPS Instruction Representation I Guest Lecturer Alan Christopher
MIPS Assembly.
Computer Architecture & Operations I
IT 251 Computer Organization and Architecture
Rocky K. C. Chang Version 0.1, 14 September 2017
Morgan Kaufmann Publishers
Lecturer PSOE Dan Garcia
IT 251 Computer Organization and Architecture
Lecturer PSOE Dan Garcia
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
“so many gadgets, so many aches” - NYT
Computer Architecture & Operations I
There is one handout today at the front and back of the room!
Instructions and Conditional Execution
CS61C : Machine Structures Lecture 3. 1
Computer Architecture
Lecturer SOE Dan Garcia
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 13 – Introduction to MIPS Instruction Representation I Lecturer PSOE.
Presentation transcript:

CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, ©2005 Some slides and/or pictures in the following are adapted from: slides ©2008 UCB 7

lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) High Level Language Program (e.g., C) Assembly Language Program (e.g.,MIPS) Machine Language Program (MIPS) Hardware Architecture Description (e.g., block diagrams) Compiler Assembler Machine Interpretation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; Logic Circuit Description (Circuit Schematic Diagrams) Architecture Implementation CSCI-365 Levels of Representation

int fib (int n) { if (n == 0) return 1; if (n == 1) return 1; return ( fib(n-1) + fib(n-2) ); } Example 3 Compile into MIPS

Computers built on 2 key principles: 1) Instructions are represented as bit patterns - can think of these as numbers 2) Therefore, entire programs can be stored in memory to be read or written just like data Simplifies SW/HW of computer systems: –Memory technology for data also used for programs –Programs can be written to manipulate other programs (or self?) Big Idea: Stored-Program Concept

Since all instructions and data are stored in memory, everything has a memory address: instructions, data words –both branches and jumps use these One register keeps address of instruction being executed: “Program Counter” (PC) –Basically a pointer to memory: Intel calls it Instruction Address Pointer, a better name Consequence #n: Everything Addressed

Programs are distributed in binary form –Programs bound to specific instruction set –Different version for Macintoshes and PCs New machines want to run old programs (“binaries”) as well as programs compiled to new instructions Leads to “backward compatible” instruction set evolving over time Selection of Intel 8086 in 1981 for 1st IBM PC is major reason latest PCs still use 80x86 instruction set (Pentium 4); could still run program from 1981 PC today Consequence #n+1: Binary Compatibility

Currently all data we work with is in words (32- bit blocks): –Each register is a word – lw and sw both access memory one word at a time So how do we represent instructions? –Remember: CPU only understands 1s and 0s, so “ add $t0,$0,$0 ” is meaningless –MIPS wants simplicity: since data is in words, make instructions be words too Instructions as Numbers

One word is 32 bits, so divide instruction word into “fields” Each field tells processor something about instruction We could define different fields for each instruction, but MIPS is based on simplicity, so define 3 basic types of instruction formats: –R-format –I-format –J-format Instructions as Numbers

I-format: used for instructions with immediates, lw and sw (since the offset counts as an immediate), and the branches ( beq and bne ) –(but not the shift instructions; later) J-format: used for j and jal R-format: used for all other instructions It will soon become clear why the instructions have been partitioned in this way Instruction Formats

R-format Instructions Define “fields” of the following number of bits each: = 32 For simplicity, each field has a name: Important: On these slides and in book, each field is viewed as a 5- or 6-bit unsigned integer, not as part of a 32-bit integer –Consequence: 5-bit fields can represent any number 0-31, while 6-bit fields can represent any number opcodersrtrdfunctshamt

What do these field integer values tell us? – opcode : partially specifies what instruction it is Note: This number is equal to 0 for all R-Format instructions – funct : combined with opcode, this number exactly specifies the instruction –Question: Why aren’t opcode and funct a single 12- bit field? Answer: We’ll answer this later R-format Instructions

– rs (Source Register): generally used to specify register containing first operand – rt (Target Register): generally used to specify register containing second operand (note that name is misleading) – rd (Destination Register): generally used to specify register which will receive result of computation R-format Instructions opcodersrtrdfunctshamt

Final field: – shamt : This field contains the amount a shift instruction will shift by. Shifting a 32-bit word by more than 31 is useless, so this field is only 5 bits (so it can represent the numbers 0-31) –This field is set to 0 in all but the shift instructions For a detailed description of field usage for each instruction, see green insert in textbook –(You can bring with you to all exams) R-format Instructions

MIPS Instruction: add $8,$9,$10 opcode = 0 (look up in table in book) funct = 32 (look up in table in book) rd = 8 (destination) rs = 9 (first operand) rt = 10 (second operand) shamt = 0 (not a shift) R-format Example

MIPS Instruction: add $8,$9,$10 Decimal number per field representation: Binary number per field representation: hex representation: 012A 4020 hex decimal representation: 19,546,144 ten Called a Machine Language Instruction hex

What about instructions with immediates? –5-bit field only represents numbers up to the value 31: immediates may be much larger than this –Ideally, MIPS would have only one instruction format (for simplicity): unfortunately, we need to compromise Define new instruction format that is partially consistent with R-format: –First notice that, if instruction has immediate, then it uses at most 2 registers I-format Instructions

Define “fields” of the following number of bits each: = 32 bits Again, each field has a name: Key Concept: Only one field is inconsistent with R-format. Most importantly, opcode is still in same location opcodersrtimmediate

What do these fields mean? – opcode : same as before except that, since there’s no funct field, opcode uniquely specifies an instruction in I-format –This also answers question of why R-format has two 6-bit fields to identify instruction instead of a single 12-bit field: in order to be consistent as possible with other formats while leaving as much space as possible for immediate field – rs : specifies a register operand (if there is one) – rt : specifies register which will receive result of computation (this is why it’s called the target register “rt”) or other operand for some instructions I-format Instructions opcodersrtimmediate

The Immediate Field: –16 bits  can be used to represent immediate up to 2 16 different values –This is large enough to handle the offset in a typical lw or sw, plus a vast majority of values that will be used in the slti instruction I-format Instructions

MIPS Instruction: addi $21,$22,-50 opcode = 8 (look up in table in book) rs = 22 (register containing operand) rt = 21 (target register) immediate = -50 (by default, this is decimal) I-format Example

MIPS Instruction: addi $21,$22,-50 Decimal/field representation: Binary/field representation: hexadecimal representation: 22D5 FFCE hex decimal representation: 584,449,998 ten

I-format Problem Problem: –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

I-format Problem Solution to Problem: –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

I-format Problem Solution to Problem (continued): –So how does lui help us? –Example: addi $t0, $t0, 0xABABCDCD becomes lui $at, 0xABAB ori $at, $at, 0xCDCD add $t0, $t0, $at –Now each I-format instruction has only a 16-bit immediate –Wouldn’t it be nice if the assembler would do this for us automatically? (later)

Branches: PC-Relative Addressing Use I-Format opcode specifies beq versus 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 opcodersrtimmediate

How do we typically use branches? –Answer: if-else, while, for –Loops are generally small: usually 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 Branches: PC-Relative Addressing

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? Branches: PC-Relative Addressing

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 Branches: PC-Relative Addressing

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: PC = (PC + 4) + ( immediate * 4) –Observations 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 Branches: PC-Relative Addressing

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 = ??? Branch Example opcodersrtimmediate

Loop:beq $9,$0,End add $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 Branch Example opcodersrtimmediate

Branch Example Loop:beq $9,$0,End add $8,$8,$10 addi $9,$9,-1 j Loop End: decimal representation: binary representation: opcodersrtimmediate

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?

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 J-format Instructions

Define “fields” of the following number of bits each: As usual, each field has a name: 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 6 bits26 bits opcodetarget address

Target Addressing Example Loop code from earlier example –Assume Loop at location Loop: sll $t1, $s3, add $t1, $t1, $s lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, j Loop Exit: … 80024

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:…

0xAE0BFFFC, 0x8D08FFC0 What assembly instruction does the above represent? Op=0, rs=1, rt=2, rd=3, shamt=0, funct=32 Op=0x2B, rs=0x10, rt=0x5, const=0x4 What type instruction do the above represent? What is the MIPS assembly instruction above? Problem 1

We explore 32-bit constants in MIPS: Write the MIPS code that creates the 32-bit constants listed above and stores that value to $t1. 2.If the current value of the PC is 0x , can you use a single jump instruction to get to the PC address as shown above. 3.If the current value of the PC is 0x , can you use a single branch instruction to get to the PC address shown above Problem 2

Character Data Byte-encoded character sets –ASCII: 128 characters 95 graphic, 33 control –Latin-1: 256 characters ASCII, +96 more graphic characters Unicode: 32-bit character set –Used in Java, C++ wide characters, … –Most of the world’s alphabets, plus symbols –UTF-8, UTF-16: variable-length encodings §2.9 Communicating with People

Byte/Halfword Operations Could use bitwise operations MIPS byte/halfword load/store –String processing is a common case lb rt, offset(rs) lh rt, offset(rs) –Sign extend to 32 bits in rt lbu rt, offset(rs) lhu rt, offset(rs) –Zero extend to 32 bits in rt sb rt, offset(rs) sh rt, offset(rs) –Store just rightmost byte/halfword

Assume that the register $t1 contains the address 0x and the register $t2 contains the address 0x lb $t0, 0($t1) sw $t0, 0($t2) lb $t0, 0($t1) sb $t0, 0($t2) Assume that the data in hex at address 0x is What value is stored at the address pointed to by register $t2? Assume the memory location pointed to $t2 initialized to 0xFFFF FFFF. Problem 3

String Copy Example C code (naïve): –Null-terminated string void strcpy (char x[], char y[]) { int i; i = 0; while ((x[i]=y[i])!='\0') i += 1; } –Addresses of x, y in $a0, $a1 –i in $s0

String Copy Example MIPS code: strcpy: addi $sp, $sp, -4 # adjust stack for 1 item sw $s0, 0($sp) # save $s0 add $s0, $zero, $zero # i = 0 L1: add $t1, $s0, $a1 # addr of y[i] in $t1 lbu $t2, 0($t1) # $t2 = y[i] add $t3, $s0, $a0 # addr of x[i] in $t3 sb $t2, 0($t3) # x[i] = y[i] beq $t2, $zero, L2 # exit loop if y[i] == 0 addi $s0, $s0, 1 # i = i + 1 j L1 # next iteration of loop L2: lw $s0, 0($sp) # restore saved $s0 addi $sp, $sp, 4 # pop 1 item from stack jr $ra # and return