CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson.

Slides:



Advertisements
Similar presentations
Henk Corporaal TUEindhoven 2011
Advertisements

Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Instruction Set-Intro
1 Lecture 3: Instruction Set Architecture ISA types, register usage, memory addressing, endian and alignment, quantitative evaluation.
1 ECE369 ECE369 Chapter 2. 2 ECE369 Instruction Set Architecture A very important abstraction –interface between hardware and low-level software –standardizes.
CS3350B Computer Architecture Winter 2015 Lecture 4
Systems Architecture Lecture 5: MIPS Instruction Set
Chapter 2 Instructions: Language of the Computer
Chapter 2.
Normal C Memory Management °A program’s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via.
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.
CS 61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
ECE 4436ECE 5367 ISA I. ECE 4436ECE 5367 CPU = Seconds= Instructions x Cycles x Seconds Time Program Program Instruction Cycle CPU = Seconds= Instructions.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CSE378 MIPS ISA1 MIPS History MIPS is a computer family –R2000/R3000 (32-bit); R4000/4400 (64-bit); R8000; R10000 (64-bit) etc. MIPS originated as a Stanford.
CDA 3101 Fall 2012 Introduction to Computer Organization Instruction Set Architecture MIPS Instruction Format 04 Sept 2013.
Instruction Set Architecture
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
1 (Based on text: David A. Patterson & John L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 3 rd Ed., Morgan Kaufmann,
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
1 Computer Architecture COSC 3430 Lecture 3: Instructions.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Execution of an instruction
Chapter 2 Instructions: Language of the Computer Part I.
CWRU EECS 3221 Language of the Machine EECS 322 Computer Architecture Instructor: Francis G. Wolff Case Western Reserve University.
Computer Architecture CSE 3322 Lecture 2 NO CLASS MON Sept 1 Course WEB SITE crystal.uta.edu/~jpatters.
Computer Architecture (CS 207 D) Instruction Set Architecture ISA.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /08/2013 Lecture 10: MIPS Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL STATE.
 1998 Morgan Kaufmann Publishers MIPS arithmetic All instructions have 3 operands Operand order is fixed (destination first) Example: C code: A = B +
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Assembly language: arithmetic and load/store instructions Ellen Spertus MCS 111 September 17, 2002.
MS108 Computer System I Lecture 3 ISA Prof. Xiaoyao Liang 2015/3/13 1.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 7, 8 Instruction Set Architecture.
1 Lecture 6: Assembly Programs Today’s topics:  Large constants  The compilation process  A full example  Intro to the MARS simulator.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Instruction Set Architecture Chapter 3 – P & H. Introduction Instruction set architecture interface between programmer and CPU Good ISA makes program.
CHAPTER 2 Instruction Set Architecture 3/21/
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Different CPUs.
Computer Architecture & Operations I
Computer Architecture & Operations I
MIPS Assembly.
Lecture 3: MIPS Instruction Set
CS2100 Computer Organisation
A Closer Look at Instruction Set Architectures
Instruction Set Architecture
Morgan Kaufmann Publishers
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
The University of Adelaide, School of Computer Science
Computer Architecture (CS 207 D) Instruction Set Architecture ISA
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Systems Architecture Lecture 5: MIPS Instruction Set
The University of Adelaide, School of Computer Science
Chapter 2 Instructions: Language of the Computer
Computer Instructions
Computer Architecture
September 17 Test 1 pre(re)view Fang-Yi will demonstrate Spim
Lecture 3: MIPS Instruction Set
The University of Adelaide, School of Computer Science
COMS 361 Computer Organization
Instructions in Machine Language
COMS 361 Computer Organization
Systems Architecture I (CS ) Lecture 5: MIPS Instruction Set*
Presentation transcript:

CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, © 2014, MK]

Translation and Startup Many compilers produce object modules directly

3 Copyright © 2014 Elsevier Inc. All rights reserved. FIGURE A.1.1 The process that produces an executable file. An assembler translates a file of assembly language into an object file, which is linked with other files and libraries into an executable file.

4 Copyright © 2014 Elsevier Inc. All rights reserved. FIGURE A.1.2 MIPS machine language code for a routine to compute and print the sum of the squares of integers between 0 and 100.

5 Copyright © 2014 Elsevier Inc. All rights reserved. FIGURE A.1.3 The same routine as in Figure A.1.2 written in assembly language. However, the code for the routine does not label registers or memory locations or include comments.

6 Copyright © 2014 Elsevier Inc. All rights reserved. FIGURE A.1.4 The same routine as in Figure A.1.2 written in assembly language with labels, but no comments. The commands that start with periods are assembler directives (see pages A-47–49)..text indicates that succeeding lines contain instructions..data indicates that they contain data..align n indicates that the items on the succeeding lines should be aligned on a 2 n byte boundary. Hence,.align 2 means the next item should be on a word boundary..globl main declares that main is a global symbol that should be visible to code stored in other files. Finally,.asciiz stores a null-terminated string in memory.

7 Copyright © 2014 Elsevier Inc. All rights reserved. FIGURE A.1.5 The routine in Figure A.1.2 written in the C programming language.

8 Copyright © 2014 Elsevier Inc. All rights reserved. FIGURE A.1.6 Assembly language either is written by a programmer or is the output of a compiler. Where do we see assembly language? First role: Output language of compilers

9 Copyright © 2014 Elsevier Inc. All rights reserved. Where do we see assembly language? Second role (dominant): Written by programmers when speed or size is critical Exploit hardware features that have no analogues in high-level languages.

When to use Assembly Language? Embedded computer ▫Needs to respond rapidly and predictably to events in the outside world ▫Tight control over which instructions execute ▫Reduces size of a program to fit in fewer memory chips ▫Reduces the cost of the embedded computer

A hybrid approach Most of a program is written in a high-level language and time-critical sections are written in assembly language ▫Programs typically spend most of their time executing a small fraction of the program’s source code. Builds on the strength of both languages

Compilers vs. Programmers Compilers are better at producing uniformly high- quality machine code across an entire program. Programmers understand algorithm and improve small sections of the program. ▫Consider several procedures simultaneously ▫Retain commonly used values in registers

Drawbacks of Assembly Language Machine-specific ▫Needs to be rewritten to run on another computer architecture Longer than the equivalent programs written in high-level language ▫Ratio (x) of assembly to HLL can be larger  Programmers write same amount of code per day  X times more productive in HLL Difficult to read, contain more bugs ▫Lack of structure (if-then, loops) ▫gotos

Instruction Set The repertoire of instructions of a computer ▫data types, instructions, registers, memory architecture, exception handling, etc. ▫commands implemented by a processor hardware/software interface A program (in say, C) is compiled into an executable that is composed of machine instructions – this executable must also run on future machines – for example, each Intel processor reads in the same x86 instructions, but each processor handles instructions differently

15 Design Principles Important design principles when defining the instruction set architecture (ISA):  keep the hardware simple – the chip must only implement basic primitives and run fast  keep the instructions regular – simplifies the decoding/scheduling of instructions

Instruction Set Different computers have different instruction sets, but with many aspects in common ▫MIPS (MIPS Technologies) ▫ARMv7 (similar to MIPS) ▫Intel x86 ▫ARMv8 (64 bits)

The MIPS Instruction Set Used as the example throughout the book Stanford MIPS commercialized by MIPS Technologies ( Large share of embedded core market ▫Applications in consumer electronics, network/storage equipment, cameras, printers, … Typical of many modern ISAs ▫See MIPS Reference Data tear-out card, and Appendixes B

Principles of Machine Design Stored-program concept 1.Instructions are represented as numbers and, as such, are indistinguishable from data 2.Programs are stored in alterable memory (that can be read or written to) just like data  Programs can be shipped as files of binary numbers – binary compatibility

19 A Basic MIPS Instruction C code: a = b + c ; Assembly code: (human-friendly machine instructions) add a, b, c # a is the sum of b and c Machine code: (hardware-friendly machine instructions)

20 A Basic MIPS Instruction add a, b, c # a is the sum of b and c Three operands Two sources and one destination All arithmetic operations have this form Design Principle 1: Simplicity favours regularity Regularity makes implementation simpler Simplicity enables higher performance at lower cost Translate the following C code into assembly code: a = b + c + d + e;

21 Example C code a = b + c + d + e; translates into the following assembly code: add a, b, c add a, b, c add a, a, d or add f, d, e add a, a, e add a, a, f Instructions are simple: fixed number of operands (unlike C) A single line of C code is converted into multiple lines of assembly code Some sequences are better than others… the second sequence needs one more (temporary) variable f

22 Subtract Example C code f = (g + h) – (i + j); Assembly code translation with only add and sub instructions:

23 Subtract Example C code f = (g + h) – (i + j); translates into the following assembly code: add t0, g, h # temp t0 = g + h add t1, i, j # temp t1 = i + j sub f, t0, t1 # f = t0 - t1 Each line can contain at most one instruction. Comments always terminate at the end of a line.

24 Operands In C, each “variable” is a location in memory In hardware, each memory access is expensive – if variable a is accessed repeatedly, it helps to bring the variable into an on-chip scratchpad and operate on the scratchpad (registers) To simplify the instructions, we require that each instruction (add, sub) only operate on registers Note: the number of operands (variables) in a C program is very large; the number of operands in assembly is fixed… there can be only so many scratchpad registers

25 Registers The MIPS ISA has 32 registers Why not more? Design Principle 2: Smaller is faster Large number of registers may increase the clock cycle time because electronic signals travel farther. Use for frequently accessed data Numbered 0 to 31

26 Registers Each register is 32-bit wide (64-bit architectures have 64-bit wide registers) A 32-bit entity (4 bytes) is referred to as a word Assembler names To make the code more readable, registers are represented as two-character names following a dollar sign $s0-$s7 (saved variables, such as C/Java variables) $t0-$t9 (temporary variables)…

Register numbers ▫$t0 – $t7 are reg’s 8 – 15 ▫$s0 – $s7 are reg’s 16 – 23 ▫$t8 – $t9 are reg’s 24 – 25 ▫add $t0, $s1, $s2 can be written as ▫add $8, $17, $18

Register Operand Example C++ code: f = (g + h) - (i + j); f, …, j in $s0, …, $s4 Compiled MIPS code: add $t0, $s1, $s2 add $t1, $s3, $s4 sub $s0, $t0, $t1

Memory Operands How can a computer represent and access large structures, for example, arrays? ▫Processor keeps only a small amount of data in registers ▫Data structures are kept in memory Data transfer instruction ▫A command that moves data between memory and registers. ▫Load values from memory into registers ▫Store result from register to memory

30 Memory Operands Values must be fetched from memory before (add and sub) instructions can operate on them Load word lw $t0, memory-address Store word sw $t0, memory-address How is memory-address determined? Register Memory Register Memory

31 Memory Address The compiler organizes data in memory… it knows the location of every variable (saved in a table)… it can fill in the appropriate mem-address for load-store instructions Memory is a large, single-dimensional array, with the address acting as the index to that array. int a, b, c, d[10] Memory … Base address Memory is byte addressed Each address identifies an 8-bit byte Alignment restriction: words must start at addresses that are multiple of 4.

32 Memory Instruction Format The format of a load instruction: destination register source address lw $t0, 8($s3) any register a constant that is added to the register in brackets The sum of the constant portion of the instruction and the contents of the second register forms the memory address.

Memory Operand Example 1 C/C++ code: g = h + A[8]; ▫g in $s1, h in $s2, base address of A in $s3 Compiled MIPS code: ▫Index 8 requires offset of 32  4 bytes per word lw $t0, 32($s3) # load word add $s1, $s2, $t0 offset base register

Memory Operand Example 2 C code: A[12] = h + A[8]; ▫h in $s2, base address of A in $s3 Compiled MIPS code: ▫Index 8 requires offset of 32 lw $t0, 32($s3) # load word add $t0, $s2, $t0 sw $t0, 48($s3) # store word

Big-Endian vs. Little-Endian Big Endian: leftmost byte is word address IBM 360/370, Motorola 68k, MIPS, Sparc, HP PA Little Endian:rightmost byte is word address Intel 80x86, DEC Vax, DEC Alpha msblsb little endian byte big endian byte 0

Registers vs. Memory Registers are faster to access than memory ▫keep the most frequently used variables in registers ▫spilling registers Operating on memory data requires loads and stores ▫More instructions to be executed Compiler must use registers for variables as much as possible ▫Only spill to memory for less frequently used variables ▫Register optimization is important!

37 Example Convert to assembly: C code: d[3] = d[2] + a; a in $s1, base address of d in $s4 Assembly: lw $t0, 8($s4) # d[2] is brought into $t0 add $t0, $t0, $s1 # the sum is in $t0 sw $t0, 12($s4) # $t0 is stored into d[3] Assembly version of the code continues to expand!

38 Immediate Operands An instruction may require a constant as input An immediate instruction uses a constant number as one of the inputs (instead of a register operand) addi (add immediate) addi $s3, $s3, 4 No subtract immediate instruction Just use a negative constant addi $s2, $s1, -1 Design Principle 3: Make the common case fast Small constants are common Immediate operand avoids a load instruction

39 Immediate Operands addi $s1, $s0, 0 # this is the address of variable a addi $s2, $s0, 4 # this is the address of variable b addi $s3, $s0, 8 # this is the address of variable c addi $s4, $s0, 12 # this is the address of variable d[0] Rewrite the following: lw $t0, 8($s0)

The Constant Zero MIPS register 0 ($zero or $0) is the constant 0 ▫Cannot be overwritten Useful for common operations ▫E.g., move between registers add $t2, $s1, $zero addi $s0, $zero, 1000 # the program has base address # 1000 and this is saved in $s0 # $zero is a register that always # equals zero