Cs 61C L5 Machine Lang.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Machine Language Lecture 5 February 3, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

Goal: Write Programs in Assembly
Lecture 5: MIPS Instruction Set
©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 5 MIPS ISA & Assembly Language Programming.
ELEN 468 Advanced Logic Design
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
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.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 08 MIPS Instruction Representation I Lecturer SOE Dan Garcia
Assembly Language II CPSC 321 Andreas Klappenecker.
1 Warning! Unlike the previous lessons, for today's lesson you will have to listen, think and even understand (for the exam, of course). Individuals with.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 MIPS Instruction Representation I Shanghai Jiaotong University and Lanxiang.
Cs 61C L6 Instruction, Proc II.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Functions, Part II Lecture 6 February 5, 1999 Dave Patterson.
CS61C L13 MIPS Instruction Representation I (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
ENEE350 Spring07 1 Ankur Srivastava University of Maryland, College Park Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005.”
Instruction Representation II (1) Fall 2007 Lecture 10: Instruction Representation II.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
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.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
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.
Computer Architecture - The Instruction Set The Course’s Goals  To be interesting and fun. An interested student learns more.  To answer questions that.
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)
©UCB CPSC 161 Lecture 5 Prof. L.N. Bhuyan
1 Today  Finish-up procedures/stack  strlen example  Machine language, the binary representation for instructions. —We’ll see how it is designed for.
ISA-2 CSCE430/830 MIPS: Case Study of Instruction Set Architecture CSCE430/830 Computer Architecture Instructor: Hong Jiang Courtesy of Prof. Yifeng Zhu.
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Topic 10: Instruction Representation CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Lecture 4: MIPS Instruction Set
IFT 201: Unit 1 Lecture 1.3: Processor Architecture-3
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
9/29: Lecture Topics Conditional branch instructions
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
CENG 311 Instruction Representation
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
CDA 3101 Spring 2016 Introduction to Computer Organization
Lecture 16: 10/29/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
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
Lecture 4: MIPS Instruction Set
ELEN 468 Advanced Logic Design
Lecturer PSOE Dan Garcia
IT 251 Computer Organization and Architecture
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
CS170 Computer Organization and Architecture I
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
COMS 361 Computer Organization
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
COMS 361 Computer Organization
Instructions in Machine Language
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 13 – Introduction to MIPS Instruction Representation I Lecturer PSOE.
Presentation transcript:

cs 61C L5 Machine Lang.1 Patterson Spring 99 ©UCB CS61C Instruction Representation and Machine Language Lecture 5 February 3, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html

cs 61C L5 Machine Lang.2 Patterson Spring 99 ©UCB Review 1/2 °Functions, procedures one of main ways to give a program structure, reuse code °jr required instruction; add jal to make common case fast °Registers make programs fast, but make procedure/function call/return tricky °MIPS SW convention divides registers into those calling procedure save/restore and those called procedure save/restore Assigns registers to arguments, return address, return value, stack pointer

cs 61C L5 Machine Lang.3 Patterson Spring 99 ©UCB Review 2/2 °MIPS assembly language instructions Arithmetic: add,sub,addi, subi Data transfer: lw, sw Relative test: slt, slti Conditional branches: beq, bne Unconditional branches: j, jr, jal °Operands Registers (word = 32 bits): $zero ; $v0, $v1, $a0 - $a3, $s0 - $s7, $t0 - $t9, $gp, $sp, $fp, $ra Memory (8-bit byte address, 4 bytes/word): Memory[0], Memory[4], Memory[8],,..., Memory[ ]

cs 61C L5 Machine Lang.4 Patterson Spring 99 ©UCB Overview °Instruction Fields, Formats (5 minutes) °Machine Language Examples (5 minutes) °Big Idea: Stored program concept, instructions are just data °Assembler extending Machine Language °Administrivia,“What’s this stuff good for” °Addressing in branches, other Addressing Modes °Decoding Instructions from numbers °Conclusion (1 minute)

cs 61C L5 Machine Lang.5 Patterson Spring 99 ©UCB Instructions as Numbers °Hardware stores information as series of high and low electrical signals °Binary numbers convenient representation °Must convert Assembly Language Instructions into binary numbers °Also means turn register names into numbers

cs 61C L5 Machine Lang.6 Patterson Spring 99 ©UCB Register Names as Numbers (page A-23) °Register Names Register No. $zero$0 $at (reserved for assembler)$1 (Return) Value registers ($v0,$v1)$2 - $3 Argument registers ($a0-$a3)$4 - $7 Temporary registers ($t0-$t7)$8 - $15 Saved registers ($s0-$s7) $16 - $22 Temporary registers ($t8-$t9)$23 - $24 $k0,$k1 (reserved for OS kernel)$26, $27 Global Pointer ($gp)$28 Stack Pointer ($sp)$29 Frame Pointer ($fp), or $t10$30 Return Address ($ra) $31

cs 61C L5 Machine Lang.7 Patterson Spring 99 ©UCB °C code: i = j + k; /* i-k:$s0-$s2 */ °Assembly: add $s0,$s1,$s2 #s0=s1+s2 °Decimal representation: Segments called fields 1st and last tell MIPS computer to add 2nd is 1st source operand (17 = $s1) 3rd is 2nd source operand (18 = $s2) 4th is destination operand (16 = $s0) 5th unused, so set to 0 Order differs: desti- nation 1st v.last! (common error) Instruction as Number Example (decimal)

cs 61C L5 Machine Lang.8 Patterson Spring 99 ©UCB Numbers: Review °Number Base B => B symbols per digit: Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base 2 (Binary):0, 1 °Number representation: d 4 d 3 d 2 d 1 d 0 d 4 x B 4 + d 3 x B 3 + d 2 x B 2 + d 1 x B 1 + d 0 x B ten = 1x x x x x10 0 = 1x x x100 +1x10 +0x1 = = ten two = 1x x x x x2 0 = 1x16 + 0x8 + 0x4 + 1x2+ 0x1 = 16 ten + 0 ten + 0 ten + 2 ten + 0 ten = 18 ten

cs 61C L5 Machine Lang.9 Patterson Spring 99 ©UCB Numbers in Decimal vs. Binary

cs 61C L5 Machine Lang.10 Patterson Spring 99 ©UCB Instruction as Number Example (binary) °C code: i = j + k; /* i-k:$s0-$s2 */ °Assembly: add $s0,$s1,$s2 #s0=s1+s2 °Decimal representation: bits5 bits 6 bits °Binary representation: Called Machine Language Instruction Layout called Instruction Format All MIPS instructions 32 bits (word): simple!

cs 61C L5 Machine Lang.11 Patterson Spring 99 ©UCB Big Idea: Stored-Program Concept °Computers built on 2 key principles: 1) Instructions are represented as numbers 2) Programs can be stored in memory to be read or written just like numbers °Machine for accounting can help write a book; just load memory with program & data, & start executing at given address °Simplifies SW/HW of computer systems: Memory technology for data also used for programs Compilers can translate HLL (data) into machine code (instructions)

cs 61C L5 Machine Lang.12 Patterson Spring 99 ©UCB Big Consequence #1: Everything addressed °Since all instructions and data are stored in memory as numbers, everything has a memory address: instructions, data words branches use memory address of instruction °C pointers are just memory addresses: they can point to anything in memory Unconstrained use of addresses can lead to nasty bugs; up to you in C; limits in Java °One register keeps address of instruction being executed: “Program Counter” (PC) Better name is Instruction Address Register, but PC is traditional name

cs 61C L5 Machine Lang.13 Patterson Spring 99 ©UCB Big Consequence #2: Binary Compatibility °Programs are distributed in binary form Programs bound to instruction set architecture Different version for Macintosh and IBM PC °New machines want to run old programs (“binaries”) as well as programs compiled to new instructions °Leads to 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 II); could still run program from 1981 PC today

cs 61C L5 Machine Lang.14 Patterson Spring 99 ©UCB Instruction Format Field Names Fields have names: op: basic operation of instruction, “opcode” rs: 1st register source operand rt: 2nd register source operand rd: register destination operand, gets the result shamt: shift amount (user later, so 0 for now) funct: function; selects the specific variant of the operation in the op field; sometimes called the function code oprsrtrdfunctshamt 6 bits5 bits 6 bits

cs 61C L5 Machine Lang.15 Patterson Spring 99 ©UCB Instruction Formats °What if want longer fields? e.g, lw 5 bits  address of 2 5 or 32  too small But want all instructions same length! °Principle: Good design demands good compromises Add 2nd format with larger address oprsrtrdfunctshamt 6 bits5 bits 6 bits oprsrtaddress 6 bits5 bits 16 bits 1st R (register) format; 2nd I (immediate) format

cs 61C L5 Machine Lang.16 Patterson Spring 99 ©UCB Notes about Register and Imm. Formats °To make it easier for hardware (HW), 1st 3 fields same in R-format and I-format °Alas, rt field meaning changed R-format: rt is 2nd source operand I-format: rt can be register destination operand °How HW know which format is which? Distinct values in 1st field (op) tell whether last 16 bits are 3 fields (R-format) or 1 field (I-format) 6 bits5 bits 6 bits oprsrtrdfunctshamt oprsrtaddress 6 bits5 bits 16 bits R: I:

cs 61C L5 Machine Lang.17 Patterson Spring 99 ©UCB Instructions, Formats, “opcodes” InstructionFormatopfunct add Register032 sub Register 034 slt Register 042 jr Register 08 lw Immediate35 sw Immediate 43 addi Immediate 8 beq Immediate 4 bne Immediate 5 slti Immediate 10 Register Format if op field = 0

cs 61C L5 Machine Lang.18 Patterson Spring 99 ©UCB Immediate Instruction in Machine Code °C code: i = j + 4; /* i,j:$s0,$s1 */ °Assembly: addi $s0,$s1,4 #s0=s1+s2 °Format: °Decimal representation: °Binary representation: bits5 bits 16 bits oprsrtaddress 6 bits5 bits 16 bits bits5 bits 16 bits

cs 61C L5 Machine Lang.19 Patterson Spring 99 ©UCB Administrivia °Readings: 3.4,3.8 °2nd homework: Due Wed 2/10 7PM Exercises 3.7, 3.8, 3.10 °2nd project: MIPS Disassembler Due Wed. 2/17 7PM www-inst.EECS/~cs61c/handouts/proj2.pdf °Why is everything due Wednesday evenings? TA’s try to set office hours to be available when useful; having single due date helps Its not Monday to try to avoid weekend

cs 61C L5 Machine Lang.20 Patterson Spring 99 ©UCB Administrivia °Read the newsgroup! Especially before posting questions! Saves you time, saves TA time “Asked and answered” on newsgroup °Midterm/Final Conflicts (3/17, 5/12) 1)If received letter from Mark regarding the final or midterm rescheduling, and not responded, you are implicitly saying you can make the *originally scheduled* time 2) That if you did NOT fill out the survey and can't make it to the originally scheduled times, immediately

cs 61C L5 Machine Lang.21 Patterson Spring 99 ©UCB “What’s This Stuff Good For?” Electro Shark Therapy: a chip in the diver's backpack activates two electrodes--one on the diver's back, and one attached to his fin--to generate a 92-volt force field. The pulsing charge repels sharks to a distance of 21 feet because of their hyper- sensitivity to naturally occurring electrical fields (which help them navigate). The long-term goal is to replace the chain-link shark nets along South Africa's popular Natal Coast beaches, which currently entangle and kill more than 1,000 sharks a year, with a large-scale electronic force field that will repel sharks without causing harm. One Digital Day, Again,what do apps like these mean for reliability requirements of our technology?

cs 61C L5 Machine Lang.22 Patterson Spring 99 ©UCB °Must keep instructions same size, but immediate field only 16 bits °Add instruction to load upper 16 bits, then later instruction gives lower 16 bits load upper immediate ( lui ) sets the upper 16 bits of a constant in a register Machine language version of lui $s0, 15 Contents of $s0 after executing lui $s0, 15 What if constant bigger than 16 bits?

cs 61C L5 Machine Lang.23 Patterson Spring 99 ©UCB Big Constant Example °C: i = 80000; /* i:s1 */ °MIPS Asm: ten = two lui $at, 1 addi $s1,$at,14464# °MIPS Machine Language $s1 ($17):

cs 61C L5 Machine Lang.24 Patterson Spring 99 ©UCB Assembler Extends Instruction Set! °Assembly Language extends native Machine Language e.g., no subi machine instruction Assemble subi $x,$y,n into addi $x,$y,-n °Sometimes 1 Assembly language instruction turns into 2 Machine language instructions lw $x,80000($zero) => lui $at, 1 lw $x,14464($at) Register $at reserved for assembler to use as whenever it needs a register (compiler doesn’t generate instructions using register $at )

cs 61C L5 Machine Lang.25 Patterson Spring 99 ©UCB Branch Addressing: Jumps, J format °j # go to location °Add third format “J-format”; example: bits26 bits Since no operands, can have big address Allows large programs Also used by jal ( op = 3 ) °Op field = 2 or Op field = 3 => J-format °Op field = 0 => R-format °Otherwise, I-format

cs 61C L5 Machine Lang.26 Patterson Spring 99 ©UCB Branch Addressing: PC-relative °Conditional Branches need opcode, 2 register fields, one address: I-format oprsrtaddress 6 bits5 bits 16 bits address just 16 bits (2 16 ), program too small! °Option: always add address to register PC = Register + Branch address Change register contents => bigger programs °Which register? How use conditional branch? if-else, loops Near current instruction => use PC as reg! PC-relative addressing (PC+4) +/ words

cs 61C L5 Machine Lang.27 Patterson Spring 99 ©UCB MIPS Addressing Modes: operand shaded immediate(arith., slt) register (arithmetic, slt) base address (data transfers) PC-relative (branch) direct (jump) register oprsrtaddress word Memory + oprsrtimmediate oprsrtrd funct register PC oprsrtaddress + word Memory PC opaddress : word Memory

cs 61C L5 Machine Lang.28 Patterson Spring 99 ©UCB Branch Addressing: PC-relative Example Loop:slt$t1,$zero,$a1# t1=9,a1=5 beq$t1,$zero,Exit# no=>Exit add$t0,$t0,$a0# t0=8,a0=4 subi$a1,$a1,1# a1=5 j Loop# goto Loop Exit:add $v0,$t0,$zero# v0=2,t0=8 See opcodes on slide 17, registers slide Address = ( ) + 3*4

cs 61C L5 Machine Lang.29 Patterson Spring 99 ©UCB Decoding Machine Instructions °Sometimes forced to reverse engineer machine language to create original assembly language e.g., looking at a core dump °Step 1: look at op field to determine instruction format: R, I, J °Step 2: break binary into fields by format °Step 3: convert to decimal number / field °Step 4: reverse into assembly using register names, instruction names, labels

cs 61C L5 Machine Lang.30 Patterson Spring 99 ©UCB Decoding example: if there is time °Binary  Decimal  Assembly  C? Try to do in section if not in class °Start at program at address 4,194,304 (2 22 ) °What are instruction formats of these 7 instructions?

cs 61C L5 Machine Lang.31 Patterson Spring 99 ©UCB Decoding example: if there is time °Binary  Decimal  Assembly  C? R R I R I R I R R I R I R I

cs 61C L5 Machine Lang.32 Patterson Spring 99 ©UCB Decoding example: if there is time °Binary  Decimal  Assembly  C? R R I R I R I add$2, $0, $ slt$9, $0, $ beq$9, $0, add$2, $2, $ addi$5, $5, slt$9, $0, $ bne$9, $0, -4

cs 61C L5 Machine Lang.33 Patterson Spring 99 ©UCB Decoding example: if there is time °Binary  Decimal  Assembly  C? add$2, $0, $ slt$9, $0, $ beq$9, $0, add$2, $2, $ addi$5, $5, slt$9, $0, $ bne$9, $0, -4 add$v0,$zero,$zero slt$t1,$zero,$a1 beq$t1,$zero, Exit Loop:add$v0,$v0,$a0 addi$a1,$a1, -1 slt$t1,$zero,$a1 bne$t1,$zero,Loop Exit:

cs 61C L5 Machine Lang.34 Patterson Spring 99 ©UCB Decoding example: if there is time °Binary  Decimal  Assembly  C? add$v0,$zero,$zero slt$t1,$zero,$a1 beq$t1,$zero, Exit Loop:add$v0,$v0,$a0 addi$a1,$a1, -1 slt$t1,$zero,$a1 bne$t1,$zero,Loop Exit: ° Mapping product:v0, mcand:a1, mlier:a1; product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier - 1; } C MIPSMIPS

cs 61C L5 Machine Lang.35 Patterson Spring 99 ©UCB “And in Conclusion …” 1/1 °MIPS assembly language instructions mapped to numbers in 3 formats Op field determines format °Operands Registers: $0 to $31 mapped onto $zero ; $at; $v0, $v1, $a0.., $s0.., $t0.., $gp, $sp, $fp, $ra Memory : Memory[0], Memory[4], Memory[8],,..., Memory[ ] -Index is the address of the word 6 bits5 bits 6 bits oprsrtrdfunctshamt oprsrtimmediate opaddress R I J

cs 61C L5 Machine Lang.36 Patterson Spring 99 ©UCB “And in Conclusion …” 2/2 °Machine Language: what HW understands Assembly Language: can extend machine language to simplify for programmer Can encode instructions as numbers °Big Idea: Stored Program Concept From C to binary, and vice versa Everything has an address Pointer in C is HLL version of address Binary SW distribution  Binary compatibility  Instruction set “lock in” °Next : More on procedures, stacks