Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CMPT 334 Computer Organization Chapter 2 Instructions: Language of the Computer [Adapted from Computer Organization and Design 5 th Edition, Patterson."— Presentation transcript:

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

2 Translation and Startup Many compilers produce object modules directly

3 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 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 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 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 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 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 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.

10 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

11 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

12 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

13 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

14 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 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

16 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)

17 The MIPS Instruction Set Used as the example throughout the book Stanford MIPS commercialized by MIPS Technologies (www.mips.com)www.mips.com 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

18 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 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) 00000010001100100100000000100000

20 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 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 22 Subtract Example C code f = (g + h) – (i + j); Assembly code translation with only add and sub instructions:

23 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 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 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 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)…

27 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

28 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

29 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 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 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 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.

33 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

34 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

35 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 3 2 1 0 little endian byte 0 0 1 2 3 big endian byte 0

36 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 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 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 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)

40 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


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

Similar presentations


Ads by Google