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

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

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

Lecture #6 Intro MIPS; Load & Store

UCB CS61C : Machine Structures

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

CS61C : Machine Structures

ECE232: Hardware Organization and Design

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

ECE232: Hardware Organization and Design

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

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 61C: Great Ideas in Computer Architecture Introduction to Assembly Language and MIPS Instruction Set Architecture

CMPE324 Computer Architecture Lecture 2

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

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC)

ECE 154A Introduction to. Fall 2012

CS3350B Computer Architecture

CS430 - Computer Architecture William J. Taffe Fall 2002 using slides from. CS61C - Machine Structures Dave Patterson Fall 2000

Chapter 1. Computer Abstractions and Technology

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

Chapter 2 Instruction: Language of the Computer

CS61C : Machine Structures

Chapter 2A Instructions: Language of the Computer

CS61C Machine Structures. Lecture 1 Introduction. 8/27/2006 John Wawrzynek (Warzneck)

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

ECE260: Fundamentals of Computer Engineering

Overview COMP Microprocessors and Embedded Systems. Lecture 11: Memory Access - I. August, 2003

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

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

EC 413 Computer Organization

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

ECE 15B COMPUTER ORGANIZATION

CISC 360. Computer Architecture. Seth Morecraft Course Web Site:

EECS Computer Organization Fall Based on slides by the author and prof. Mary Jane Irwin of PSU.

Instructor: Francis G. Wolff Case Western Reserve University This presentation uses powerpoint animation: please viewshow

CS61C Machine Structures. Lecture 1 Introduction. 8/25/2003 Brian Harvey. John Wawrzynek (Warznek) www-inst.eecs.berkeley.

Levels of Programming. Registers

Telematics group University of Göttingen, Germany

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

CSEE 3827: Fundamentals of Computer Systems

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

Computer Architecture. Lecture 2 : Instructions

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

EEC 581 Computer Architecture Lecture 1 Review MIPS

Instructions: Language of the Computer

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

ECE369. Chapter 2 ECE369

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

ECE331: Hardware Organization and Design

EECE 321: Computer Organization

Topic Notes: MIPS Instruction Set Architecture

Computer Organization MIPS ISA

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

Chapter 2. Instructions:

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

Five classic components

Computer Architecture

Control MIPS unconditional branch instruction (i.e., jump)

CS64 Computer Organization

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

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

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

Review: What is a Computer?

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

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

Computer Architecture

Lecture 6 Decision + Shift + I/O

CPE300: Digital System Architecture and Design. Fall 2011 MW 17:30-18:45 CBC C316

syscall takes a service ID (number) in $v0 Read integer $v0=5, after syscall, $v0 holds the integer read from keyboard

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

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

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

ELEC 5200/6200. Computer Architecture & Design. Victor P. Nelson Broun 326

CS3350B Computer Architecture MIPS Introduction

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

CSE Introduction to Computer Architecture. Jeff Brown

MIPS Memory Access Instructions

What is Computer Architecture? CSE Introduction to Computer Architecture. Why do I care? Which is faster? Allan Snavely

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

ECE 486/586. Computer Architecture. Lecture # 6

CS3350B Computer Architecture MIPS Instruction Representation

CENG3420 Lecture 03 Review

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

Computer Model. What s in a computer? Processor Memory I/O devices (keyboard, mouse, LCD, video camera, speaker, disk, CD drive, )

Computer Architecture

Memory. CS 447, Spring /25/16. Memory. Processor (CPU) transfer

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

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

UCB CS61C : Machine Structures

Operations, Operands, and Instructions

CPE300: Digital System Architecture and Design

Review 1/2 Big idea in CS&E; compilation to translate from one level of abstraction to lower level

Computer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific. MIPS Assembly

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

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

UCB CS61C : Machine Structures

Transcription:

מבנה מחשבים 2006 Amar Lior Based on lectures notes from Arie Schlesinger (aries@cs.columbia.edu) Adapted from Computer Organization&Design, H/S interface, Patterson Hennessy@UCB,1999 1

Administration Course site www.cs.huji.ac.il/~comarc Please register to the course via the web The first 4 tirgulim will be based on appendix A of the book Computer Organization and Design By Hennessy and Patterson This appendix is available via the course web page We will learn both MIPS and x86 assembler Exercises Are obligatory Are 30% of grade Should be done alone (unless we say otherwise) 2

Administration The newsgroup is local.course.comarc.stud There is no TA newsgroups I will be available 45 minutes right after each tirgul at Ross 110 SIPM simulator spim-7.1/xspim-7.1 All exercises are to be submitted electronically Theoretical exercises should be submitted as text files or pdf 3

What are Machine Architecture? Software Hardware Application (Browser) Compiler Assembler Processor Memory Operating System (Linux) Datapath & Control I/O system Instruction Set Architecture Digital Design Circuit Design transistors Coordination of many levels of abstraction 4

Levels of Representation High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g.,mips) Assembler Machine Language Program (MIPS) temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $to, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) 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 Machine Interpretation 5 Control Signal Specification ALUOP[0:3] <= InstReg[9:11] & MASK

Anatomy: 5 components of any Computer Computer Processor (active) Control ( brain ) Datapath Memory (passive) (where programs, data live when running) Devices Input Output Keyboard, Mouse Disk (where programs, data live when not running) Display, Printer 6

` Continued rapid improvement in Computing 2X in speed every 1.5 years; processor speed, memory size, disk capacity; Moore s Law enables it (2X transistors/chip/1.5 yrs) Will this continue?? 7

Hello world (in spim) ttyp1>>!cat cat hello.asm.data hello_str:.asciiz "hello world\n".text.globl main main: la $a0, hello_str li $v0, 4 syscall li $v0, 10 syscall os-21: spim-7.1 -f hello.asm SPIM Version 7.1 of January 2, 2005 Copyright 1990-2004 by James R. Larus (larus@cs.wisc.edu). All Rights Reserved. See the file README for a full copyright notice. Loaded: /usr/local/etc/spim/exceptions.s hello world >> 8

C Operators/Operands Arithmetic: operators: +, -, *, /, % (mod) Assignment: Variable = expression Operands: Variables: lower, upper, fahr, celsius Constants: e.g., 0, 1000, -17, 15 In C (and most High Level Languages), 1st - variables are declared and given a type, then they may be used. Example: int fahr, celsius; int a, b, c, d, e; 9

C Operators/Operands Examples Assignment statements : a = b + c; a = b + c + d - e; celsius = (fahr - 32)* (5/ 9); 10

Assembly Operators (MIPS) Syntax of Assembly Operator 1) name of the operation 2) the operand which gets the result 3) 1st operand for operation 4) 2nd operand for operation Ex. add b to c and put the result in a : Assembly Language Instruction: add a, b, c Equivalent assignment statement in C: a = b + c; 11

Assembly Operators/Instructions MIPS Assembly Syntax is rigid: 1 operation, 3 variables Why? Keep Hardware simple via regularity How to do the following C statement? a = b + c + d - e; Break into multiple instructions add a, b, c # a = sum of b & c add a, a, d # a = sum of b,c,d sub a, a, e # a = b+c+d-e To right of sharp (#) is a comment terminated by end of the line 12

Assembly Operators / Instructions Note: Unlike C (and most other HLL), each line of assembly contains at most 1 instruction 13

Compilation 14 How to turn notation that programmers prefer, into a notation which the computer understands? Program to translate C statements into Assembly Language instructions; called a compiler Example: compile by hand this C code: a = b + c; d = a - e; Easy: add a, b, c sub d, a, e Big Idea: compiler translates notation from 1 level of abstraction to lower level

Compilation 2 Example: compile by hand this C code: f = (g + h) - (i + j); First sum of g and h. Where to put the result? add f,g,h # f contains g+h Now sum of i and j. Where to put the result? Cannot use i! Compiler creates temporary variable to hold sum add t1,i,j # t1 contains i + j Finally produce difference sub f,f,t1 # f = ( g + h )-( i + j ) 15

Compilation 2 Summary C statement (5 operands, 3 operators): f = (g + h) - (i + j); Becomes 3 assembly instructions (6 unique operands, 3 operators): add f,g,h add t1,i,j sub f,f,t1 # f contains g+h # t1 contains i+j # f=(g+h)-(i+j) In general, each line of C produces many assembly instructions 16 One reason why people program in C vs. Assembly; fewer lines of code Other reasons?

Assembly Operands Unlike HLL, assembly instructions cannot use variables Why not? Keep Hardware Simple Instruction operands are registers: limited number of special locations; 32 registers in MIPS Why 32? Smaller is faster (and also cheaper) Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS 17

Assembly Operands: Registers Naming of 32 registers: instead of r0, r1,, r31, use $s0 $s7 for registers corresponding to C variables $t0 $t7 for registers corresponding to temporary variables Will explain mapping convention later of $s0, $s1,, $t0, $t1,, to r0, r1, Note: whereas C declares its operands, Assembly operands (registers) are fixed and not declared 18

Compilation using Registers Compile by hand using register names: f = (g + h) - (i + j); f maps onto $s0 g maps onto $s1 h maps onto $s2 i maps onto $s3 j maps onto $s4 19

Compilation using Registers Compile by hand using registers: C instruction: f = (g + h) - (i + j); f: $s0, g: $s1, h: $s2, i: $s3, j: $s4 MIPS Instructions: add $s0,$s1,$s2 # $s0 = g+h add $t1,$s3,$s4 # $t1 = i+j sub $s0,$s0,$t1 # f=(g+h)-(i+j) 20

Assembly Operands: Memory C variables map onto registers; what about data structures like arrays? 1 of 5 components of a computer: Memory, contains such data structures But arithmetic instructions only operate on registers? Data transfer instructions transfer data between registers and memory Think of memory as a large single dimensioned array, starting at 0 21

Data Transfer Instruction: Memory to Reg Load: moves data from Memory to Register Syntax: 1) Name of the Operation 2) Name of Register to be Loaded with Data 3) Constant and Register # (to be added) to create the Data Address in Memory MIPS name, lw for load word: Example: lw $t0, 8($s3) Offset Base Register 22

Compilation when Operand is in Memory Compile by hand using registers: g = h + A[8]; g:$s1, h:$s2, starting address (base address) of array A, in register: $s3 Since A[8] is in memory, 1st: transfer A[8] from memory to (temporary) register: lw $t0,8($s3) # $t0 gets A[8] Adds 8 to contents of $s3 to select A[8], puts value into $t0 Next add it to h, and place result in g add $s1,$s2,$t0 # $s1 = h+a[8] 23

Addressing: Byte vs. word Every word in memory has an address, similar to an index in an array Early computers numbered words like C numbers elements of an array: Memory[0], Memory[1], Memory[2], Computers needed to access 8-bit bytes as well as words (4 bytes/word) Today machines address memory as bytes, hence word addresses differ by 4 Memory[0], Memory[4], Memory[8], Called the address of a word 24

Notes about Memory Pitfall: Forgetting that sequential word addresses in machines with byte addressing do not differ by 1. Many an assembly language programmer has toiled over errors made by assuming that the address of the next word can be found by incrementing the address in a register by 1 instead of by the word size in bytes. Another advantage of compilers: they use proper byte address for words 25

Compilation with Memory Revisited What offset in lw to select A[8] in C? 4x8=32 to select A[8]: byte v. word Compile by hand using registers: g = h + A[8]; g: $s1, h: $s2, $s3: base address of A 1st transfer from memory to register: lw $t0,32($s3) # $t0 gets A[8] Add 32 to $s3 to select A[8], put into $t0 Next add it to h and place in g add $s1,$s2,$t0 # $s1 = h+a[8] 26

More Notes about Memory How are Bytes numbered in a Word? little endian byte 0 3 2 1 0 msb lsb 0 1 2 3 big endian byte 0 Gulliver s Travels: Which end of egg to open?. Little Endian address of least significant byte: Intel 80x86, DEC Alpha Big Endian address of most significant byte HP PA, IBM/Motorola PowerPC, SGI, Sparc 27

More Notes about Memory: Alignment MIPS requires that all words start at addresses that are multiples of 4 Aligned Not Aligned 0 1 2 3 Called Alignment: objects must fall on address that is multiple of their size. (Later we ll see how alignment helps performance) 28

Data transfer: Register to Memory Store: complementary instruction to load MIPS: sw for Store Word, same syntax as lw Example: A[12] = h; base address of A: $s3, h: $s2 MIPS assembly instructions: sw $s2,48($s3) # h into A[12] 29

Compile with variable index What if array index not a constant? g = h + A[i]; g: $s1, h: $s2, i: $s4, $s3: base address of A To load A[i] into a register, first turn i into a byte address; multiply by 4 How multiply using adds? Creating 4i i + i = 2i, 2i + 2i = 4i add $t1,$s4,$s4 # $t1 = 2*i add $t1,$t1,$t1 # $t1 = 4*i 30

Compile with variable index, con t Next add i to base of A: add $t1,$t1,$s3 # $t1=address of # A[i] (4*i+$s3) Now load A[i] into a temporary register: lw $t0,0($t1) # Temp $t0 = A[i] Finally add to h and put sum in g: add $s1,$s2,$t0 # g = h + A[i] 31

Compile with variable index: Summary C statement: g = h + A[i]; Compiled MIPS assembly instructions: add $t1,$s4,$s4 # $t1 = 2*i add $t1,$t1,$t1 # $t1 = 4*i add $t1,$t1,$s3 lw $t0,0($t1) add $s1,$s2,$t0 # $t1=addr A[i] # $t0 = A[i] # g = h + A[i] 32

Role of Registers vs. Memory What if more variables than registers? Compiler tries to keep most frequently used variable in registers Writing less common to memory: spilling Why not keep all variables in memory? Smaller is faster: registers are faster than memory Registers more versatile: MIPS arithmetic instruction can read 2, operate on them, and write 1 per instruction MIPS data transfer only read or write 1 operand per instruction, and no operation 33

Class exercise 34 Compile this C code into MIPS: B[i] = h + A[i]; h: $s2, i: $s4, $s3:base address of A $s5:base address of B add $t1,$s4,$s4 # $t1 = 2*i add $t1,$t1,$t1 # $t1 = 4*i add $t2,$t1,$s3 # $t2=addr A[i] lw $t0,0($t2) # $t0 = A[i] add $t0,$s2,$t0 # $t0 = h+a[i] add $t3,$t1,$s5 # $t3=addr B[i] sw $t0,0($t3) # B[i]= h+a[i]

Conclusion... Big idea in CS; compilation to translate from one level of abstraction to lower level Generally single HLL statement produces many assembly instructions Also hides address calculations (byte vs. word addressing) Design of an Assembly Language like MIPS shaped by 1) Desire to keep hardware simple: e.g., each operation has 3 operands 2) Smaller is faster: e.g., MIPS has 32 registers 35

Conclusion... MIPS assembly language thus far: Instructions: add, sub, lw, sw At most one assembly instruction per line Comments start with # to end of line Operands: registers $s0, $s1,... ; $t0, $t1,... Operands: memory Memory[0], Memory[4], Memory[8],,..., Memory[4294967292] Next: difference between computers and calculators: Making decisions 36