CS 61C: Great Ideas in Computer Architecture. Lecture 5: Intro to Assembly Language, MIPS Intro. Instructor: Sagar Karandikar

Similar documents
CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro. Machine Interpreta4on

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and MIPS Instruction Set Architecture

CS 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and RISC-V Instruction Set Architecture

CS 110 Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro

CS61C : Machine Structures

CS61C : Machine Structures

UCB CS61C : Machine Structures

CS61C Machine Structures. Lecture 10 - MIPS Branch Instructions II. 2/8/2006 John Wawrzynek. (

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

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

UCB CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to Machine Language

Lecture #6 Intro MIPS; Load & Store

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS61C Machine Structures. Lecture 8 - Introduction to the MIPS Processor and Assembly Language. 9/14/2007 John Wawrzynek

9/9/12. New- School Machine Structures (It s a bit more complicated!) CS 61C: Great Ideas in Computer Architecture IntroducMon to Machine Language

CS 61C: Great Ideas in Computer Architecture. Lecture 6: More MIPS, MIPS Func.ons. Instructor: Sagar Karandikar

I ve been getting this a lot lately So, what are you teaching this term? Computer Organization. Do you mean, like keeping your computer in place?

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

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

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

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

CS61C : Machine Structures

Reduced Instruction Set Computer (RISC)

CS 61c: Great Ideas in Computer Architecture

Reduced Instruction Set Computer (RISC)

UCB CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Outline. Homework. Assembly Language. Instructions. Instruction Set Architectures. Csci 136 Computer Architecture II MIPS Instruction Set Architecture

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

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

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

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

EE108B Lecture 2 MIPS Assembly Language I. Christos Kozyrakis Stanford University

UCB CS61C : Machine Structures

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CS61C Machine Structures. Lecture 12 - MIPS Procedures II & Logical Ops. 2/13/2006 John Wawrzynek. www-inst.eecs.berkeley.

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

Chapter 2. Instructions:

CS 61c: Great Ideas in Computer Architecture

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

Chapter 2: Instructions:

CS61C C/Assembler Operators and Operands Lecture 2 January 22, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)

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

CMPE324 Computer Architecture Lecture 2

CS61C : Machine Structures

Computer Architecture

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

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

CS3350B Computer Architecture MIPS Instruction Representation

Review of instruction set architectures

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

CENG3420 Lecture 03 Review

ECE 154A Introduction to. Fall 2012

Instructions: Language of the Computer

Lecture 6 Decision + Shift + I/O

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

CS61C Constants and Making Decisions in C/Assembly Language. Lecture 3. January 27, 1999 Dave Patterson (http.cs.berkeley.

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

Levels of Programming. Registers

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

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

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

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

CS61C : Machine Structures

Computer Architecture

מבנה מחשבים Amar Lior Based on lectures notes from Arie Schlesinger

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

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

CS 61C: Great Ideas in Computer Architecture. More MIPS, MIPS Functions

Chapter 2A Instructions: Language of the Computer

CS61C : Machine Structures

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

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

EE108B Lecture 3. MIPS Assembly Language II

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

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

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

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

Programming the processor

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

Assembly Programming

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

MIPS Assembly Programming

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

Instructors: Randy H. Katz David A. PaHerson hhp://inst.eecs.berkeley.edu/~cs61c/fa10. Fall Lecture #1. Agenda

Instructors: Randy H. Katz David A. PaHerson hhp://inst.eecs.berkeley.edu/~cs61c/fa10. Fall Lecture #6. Agenda

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

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

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Transcription:

CS 61C: Great Ideas in Computer Architecture Lecture 5: Intro to Assembly Language, MIPS Intro Instructor: Sagar Karandikar sagark@eecs.berkeley.edu hbp://inst.eecs.berkeley.edu/~cs61c 1

Machine Interpreta4on Levels of RepresentaHon/ InterpretaHon High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture DescripCon (e.g., block diagrams) Architecture Implementa4on temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) Anything can be represented as a number, i.e., data or instruchons 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 Logic Circuit DescripCon (Circuit SchemaCc Diagrams) 2

Assembly Language Basic job of a CPU: execute lots of instruc8ons. InstrucHons are the primihve operahons that the CPU may execute. Different CPUs implement different sets of instruchons. The set of instruchons a parhcular CPU implements is an Instruc8on Set Architecture (ISA). Examples: ARM, Intel x86, MIPS, RISC- V, IBM/ Motorola PowerPC (old Mac), Intel IA64,... 3

InstrucHon Set Architectures Early trend was to add more and more instruchons to new CPUs to do elaborate operahons VAX architecture had an instruchon to mulhply polynomials! RISC philosophy (Cocke IBM, PaBerson, Hennessy, 1980s) Reduced InstrucHon Set CompuHng Keep the instruchon set small and simple, makes it easier to build fast hardware. Let sodware do complicated operahons by composing simpler ones. 4

MIPS Architecture MIPS semiconductor company that built one of the first commercial RISC architectures We will study the MIPS architecture in some detail in this class (upper- div arch classes like 150, 152 use a similar ISA, RISC- V) Why MIPS instead of Intel x86? MIPS is simple, elegant. Don t want to get bogged down in griby details. MIPS widely used in embedded apps, x86 lible used in embedded, and more embedded computers than PCs 5

Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers Limited number of special locahons built directly into the hardware OperaHons can only be performed on these! Benefit: Since registers are directly in hardware, they are very fast (faster than 1 ns - light travels 30cm in 1 ns!!! ) 6

Number of MIPS Registers Drawback: Since registers are in hardware, there are a predetermined number of them SoluHon: MIPS code must be very carefully put together to efficiently use registers 32 registers in MIPS Why 32? Smaller is faster, but too small is bad. Goldilocks problem. Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS 7

Names of MIPS Registers Registers are numbered from 0 to 31 Each register can be referred to by number or name Number references: $0, $1, $2, $30, $31 For now: $16 - $23è $s0 - $s7 (correspond to C variables) $8 - $15 è $t0 - $t7 (correspond to temporary variables) Later will explain other 16 register names In general, use names to make your code more readable 8

C, Java variables vs. registers In C (and most High Level Languages) variables declared first and given a type Example: int fahr, celsius; char a, b, c, d, e; Each variable can ONLY represent a value of the type it was declared as (cannot mix and match int and char variables). In Assembly Language, registers have no type; operahon determines how register contents are treated 9

AddiHon and SubtracHon of Integers AddiHon in Assembly Example: add $s0,$s1,$s2 (in MIPS) Equivalent to: a = b + c (in C) where C variables MIPS registers are: a $s0, b $s1, c $s2 SubtracHon in Assembly Example: sub $s3,$s4,$s5 (in MIPS) Equivalent to: d = e - f (in C) where C variables MIPS registers are: d $s3, e $s4, f $s5 10

AddiHon and SubtracHon of Integers Example 1 How to do the following C statement? a = b + c + d - e; Break into mulhple instruchons add $t0, $s1, $s2 # temp = b + c add $t0, $t0, $s3 # temp = temp + d sub $s0, $t0, $s4 # a = temp - e A single line of C may break up into several lines of MIPS. NoHce the use of temporary registers don t want to modify the variable registers $s Everything ader the hash mark on each line is ignored (comments) 11

Immediates Immediates are numerical constants. They appear oden in code, so there are special instruchons for them. Add Immediate: addi $s0,$s1,-10 (in MIPS) f = g - 10 (in C) where MIPS registers $s0,$s1 are associated with C variables f, g Syntax similar to add instruchon, except that last argument is a number instead of a register. add $s0,$s1,$zero (in MIPS) f = g (in C) 12

Overflow in Arithmetic Reminder: Overflow occurs when there is a mistake in arithmetic due to the limited precision in computers. Example (4-bit unsigned numbers): 15 1111 + 3 + 0011 18 10010 But we don t have room for 5-bit solution, so the solution would be 0010, which is +2, and wrong. 13

Overflow handling in MIPS Some languages detect overflow (Ada), some don t (most C implementahons) MIPS soluhon is 2 kinds of arithmehc instruchons: These cause overflow to be detected add (add) add immediate (addi) subtract (sub) These do not cause overflow detechon add unsigned (addu) add immediate unsigned (addiu) subtract unsigned (subu) Compiler selects appropriate arithmehc MIPS C compilers produce addu, addiu, subu 14

Data Transfer: Load from and Store to memory Processor Control Enable? Read/Write Memory Input Datapath PC Registers Address Write Data = Store to memory Program Bytes ArithmeHc & Logic Unit (ALU) Read Data = Load from memory Data Output Processor- Memory Interface I/O- Memory Interfaces 15

Memory Addresses are in Bytes Lots of data is smaller than 32 bits, but rarely smaller than 8 bits works fine if everything is a mulhple of 8 bits 8 bit chunk is called a byte (1 word = 4 bytes) Memory addresses are really in bytes, not words Word addresses are 4 bytes apart Addr of lowest byte in word is addr of word 12 8 4 0 133 9 2 5 1 1 0 Word address is same as address of ledmost byte (i.e. Big- endian) 14 10 6 2 15 11 7 3 16

Transfer from Memory to Register C code int A[100]; g = h + A[3]; Using Load Word (lw) in MIPS: lw $t0,12($s3) # Temp reg $t0 gets A[3] add $s1,$s2,$t0 # g = h + A[3] Note: $s3 base register (pointer) 12 offset in bytes Offset must be a constant known at assembly Hme 17

Transfer from Register to Memory C code int A[100]; A[10] = h + A[3]; Using Store Word (sw) in MIPS: lw $t0,12($s3) # Temp reg $t0 gets A[3] add $t0,$s2,$t0 # Temp reg $t0 gets h + A[3] sw $t0, 40($s3) # A[10] = h + A[3] Note: $s3 base register (pointer) 12,40 offsets in bytes $s3+12 and $s3+40 must be mulhples of 4 18

Loading and Storing bytes In addihon to word data transfers (lw, sw), MIPS has byte data transfers: load byte: lb store byte: sb Same format as lw, sw E.g., lb $s0, 3($s1) contents of memory locahon with address = sum of 3 + contents of register $s1 is copied to the low byte posihon of register $s0. xxxx xxxx xxxx xxxx xxxx xxxx xzzz zzzz $s0: is copied to sign-extend byte loaded This bit 19

Speed of Registers vs. Memory Given that Registers: 32 words (128 Bytes) Memory: Billions of bytes (2 GB to 8 GB on laptop) and the RISC principle is Smaller is faster How much faster are registers than memory?? About 100-500 Hmes faster! in terms of latency of one access 20

How many hours h on Homework 0? A: 0 h < 5 B: 5 h < 10 C: 10 h < 15 D: 15 h < 20 E: 20 h 21

Clickers/Peer Instruction We want to translate *x = *y +1 into MIPS (x, y pointers to ints, stored in: $s0 $s1) A: addi $s0,$s1,1 B: lw $s0,1($s1) sw $s1,0($s0) C: lw $t0,0($s1) addi $t0,$t0,1 sw $t0,0($s0) D: sw $t0,0($s1) addi $t0,$t0, 1 lw $t0,0($s0) E: lw $s0,1($t0) sw $s1,0($t0) 22

Break 23

MIPS Logical InstrucHons Useful to operate on fields of bits within a word e.g., characters within a word (8 bits) OperaHons to pack /unpack bits into words Called logical opera8ons Logical operations C operators Java operators MIPS instructions Bit-by-bit AND & & and Bit-by-bit OR or Bit-by-bit NOT ~ ~ not Shift left << << sll Shift right >> >>> srl 24

Logic Shifting Shid Led: sll $s1,$s2,2 #s1=s2<<2 Store in $s1 the value from $s2 shided 2 bits to the led (they fall off end), inserhng 0 s on right; << in C. Before: 0000 0002 hex 0000 0000 0000 0000 0000 0000 0000 0010 two Ader: 0000 0008 hex 0000 0000 0000 0000 0000 0000 0000 1000 two What arithmehc effect does shid led have? Shid Right: srl is opposite shid; >> 25

ArithmeHc Shiding Shid right arithmehc moves n bits to the right (insert high order sign bit into empty bits) For example, if register $s0 contained 1111 1111 1111 1111 1111 1111 1110 0111 two = - 25 ten If executed sra $s0, $s0, 4, result is: 1111 1111 1111 1111 1111 1111 1111 1110 two = - 2 ten Unfortunately, this is NOT same as dividing by 2 n Fails for odd negahve numbers C arithmehc semanhcs is that division should round towards 0 26

Computer Decision Making Based on computahon, do something different In programming languages: if- statement MIPS: if- statement instruchon is beq register1,register2,l1 means: go to statement labeled L1 if (value in register1) == (value in register2).otherwise, go to next statement beq stands for branch if equal Other instruchon: bne for branch if not equal 27

Types of Branches Branch change of control flow CondiConal Branch change control flow depending on outcome of comparison branch if equal (beq) or branch if not equal (bne) UncondiConal Branch always branch a MIPS instruchon for this: jump (j) 28

Example if Statement Assuming translahons below, compile if block f $s0 g $s1 h $s2 i $s3 j $s4 if (i == j) bne $s3,$s4,exit f = g + h; add $s0,$s1,$s2 Exit: May need to negate branch condihon 29

Example if- else Statement Assuming translahons below, compile f $s0 g $s1 h $s2 i $s3 j $s4 if (i == j) bne $s3,$s4,else f = g + h; add $s0,$s1,$s2 else j Exit f = g h; Else: sub $s0,$s1,$s2 Exit: 30

Administrivia Hopefully everyone completed HW0 HW1 out Proj 1 out Make sure you test your code on hive machines, that s where we ll grade them First Guerrilla Session this Thursday (07/02) from 5-7pm in the Woz OpHonal (not part of EPA) Covers Number Rep and MIPS 31

Administrivia Midterm one week from Thursday In this room, at this Hme One 8.5 x11 handwriben cheatsheet We ll provide a MIPS green sheet No electronics Covers up to and including this Thursday s lecture (07/02) TA- led review session on Monday 07/06 from 5-8pm in HP Auditorium Feedback form at the end of lab 2 tell us how lecture, disc, and lab are going 32

CS61C In the News MIPS steers spacecrad to Pluto 4 MIPS R3000 32bit CPUs Command and Data handling Guidance and Control Launched 2006, first pics in July 2015 http://www.electronicsweekly.com/news/military-aerospaceelectronics/mips-steers-spacecraft-pluto-2015-01/ 33

CS61C in the News RISC- V Workshop 34

CS61C in the News RISC- V Workshop Workshop happening right now at InternaHonal House A modest goal: To become the industry standard ISA (replacing x86, ARM, MIPS, etc.) CS150, CS152, CS250, CS252 use RISC- V 35

CS61C in the News pt. 2 36

Inequalities in MIPS UnHl now, we ve only tested equalihes (== and!= in C). General programs need to test < and > as well. Introduce MIPS Inequality InstrucHon: Set on Less Than Syntax: slt reg1,reg2,reg3 Meaning: if (reg2 < reg3) reg1 = 1; else reg1 = 0; set means change to 1, reset means change to 0. 37

Inequalities in MIPS Cont. How do we use this? Compile by hand: if (g < h) goto Less; #g:$s0, h:$s1 Answer: compiled MIPS code slt $t0,$s0,$s1 # $t0 = 1 if g<h bne $t0,$zero,less # if $t0!=0 goto Less Register $zero always contains the value 0, so bne and beq oden use it for comparison ader an slt instruchon sltu treats registers as unsigned 38

Immediates in Inequalities slti an immediate version of slt to test against constants Loop:... slti $t0,$s0,1 # $t0 = 1 if # $s0<1 beq $t0,$zero,loop # goto Loop # if $t0==0 # (if ($s0>=1)) 39

Loops in C/Assembly Simple loop in C; A[] is an array of ints do { g = g + A[i]; i = i + j; } while (i!= h); Use this mapping: g, h, i, j, &A[0] $s1, $s2, $s3, $s4, $s5 Loop: sll $t1,$s3,2 # $t1= 4*i addu $t1,$t1,$s5 # $t1=addr A+4i lw $t1,0($t1) # $t1=a[i] add $s1,$s1,$t1 # g=g+a[i] addu $s3,$s3,$s4 # i=i+j bne $s3,$s2,loop # goto Loop # if i!=h 40

Which of the following are true for the addiu instruchon? 1: The instruchon performs a different operahon at the hardware level than add (excluding overflow reporhng) 2: The instruchon tells the hardware not to report an overflow 3: The instruchon sign extends the immediate A: F, F, F B: T, T, T C: F, T, F D: F, T, T E: T, F, F 41

And In Conclusion Computer words and vocabulary are called instruchons and instruchon set respechvely MIPS is example RISC instruchon set in this class Rigid format: 1 operahon, 2 source operands, 1 deshnahon add,sub,mul,div,and,or,sll,srl,sra lw,sw,lb,sb to move data to/from registers from/to memory beq, bne, j, slt, slti for decision/flow control Simple mappings from arithmehc expressions, array access, if- then- else in C to MIPS instruchons 42