48
Instructions – Language of the Computer Alexander Nelson January 20, 2021 University of Arkansas - Department of Computer Science and Computer Engineering

Instructions Language of the COmputercsce.uark.edu/~ahnelson/CSCE2214/lectures/lecture4-instructions... · Representing Instructions { I-Format Immediate arithmetic and load/store

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • Instructions – Language of the Computer

    Alexander Nelson

    January 20, 2021

    University of Arkansas - Department of Computer Science and Computer Engineering

  • The really decisive considerations from the

    present point of view, in selecting a code, are

    of a more practical nature: simplicity of the

    equipment demanded by the code, and the

    clarity of its application to the actually

    important problems together with the speed

    of its handling those problems.

    -Arthur Burks, Herman Goldstine, von

    Neumann

    0

  • Instructions & Instruction Set

    Instructions – individual words sent to a computer

    Instruction Set – Vocabulary available to the computer

    Each computer could have own instruction set

    However

    Basic underlying principles inform their development

    Most instruction set architectures support similar vocabularies

    1

  • Instruction & Instruction Set

    Goal of instruction set – enable complex behavior through simple

    equipment

    We will primarily study the MIPS instruction set

    MIPS – Instruction set from MIPS technologies since 1980s

    Large share of embedded core market

    Other ISAs we will look at

    • ARMv7 – Similar to MIPS, >9B chips• Intel x86 – powers many PCs• ARMv8 – Extends v7 to 64-bit

    2

  • Example Instruction

    Basic arithmetic operation:

    add a, b, c # a = b + c

    sub a, b, c # a = b - c

    Design Principle 1: Simplicity favors regularity

    Regularity makes implementation simpler

    Simplicity enables higher performance at lower cost

    3

  • Arithmetic Example

    C Code:

    f = (g+h) - (i+j);

    How would you do this in assembly?

    4

  • Arithmetic Example

    C Code:

    f = (g+h) - (i+j);

    How would you do this in assembly?

    add t0, g, h #temp t0 = g + h

    add t1, i, j #temp t1 = i + j

    sub f, t0, t1 #f = t0 - t1

    5

  • MIPS Instruction Set

    6

  • MIPS Instruction Set

    7

  • Register Operands

    Arithmetic instructions use register operands

    MIPS has a 32 x 32-bit register file

    • Use for frequently accessed data• Numbered 0 to 31• 32-bit data called a “word”

    Assembler Names:

    • $t0, $t1, ..., $t9 for temporary values• $s0, $s1, ..., $s7 for saved variables

    Design Principle 2: Smaller is faster

    8

  • Arithmetic Example

    C Code:

    f = (g+h) - (i+j);

    //Assume f, g, h, i, j in $s0-$s4 respectively

    How would you do this in assembly?

    add $t0, $s1, $s2 #temp $t0 = g + h

    add $t1, $s3, $s4 #temp $t1 = i + j

    sub $s0, $t0, $t1 #f = $t0 - $t1

    9

  • Memory Operands

    Main memory used for composite data

    e.g. Arrays, data structures, dynamic data

    To apply arithmetic operations:

    • Load values from memory into registers• Compute arithmetic in registers• Store result from register into memory

    10

  • Memory Operands

    Memory is byte addressed

    i.e. Each address identifies an 8-bit byte

    Why?

    11

  • Memory Operands

    Memory is byte addressed

    i.e. Each address identifies an 8-bit byte

    Words are aligned in memory

    Address must be a multiple of 4

    Why?

    12

  • Memory Operands

    Memory is byte addressed

    i.e. Each address identifies an 8-bit byte

    Words are aligned in memory

    Address must be a multiple of 4

    MIPS is Big Endian

    • Most-significant byte at least address of a word• Little-Endian – Least-significant byte at least address

    13

  • Memory Operand Example 1

    C Code:

    g = h + A[8]; // Assume g in $s1, h in $s2

    // Base address of A in $s3

    Then Compiled MIPS (Index 8 requires offset of 32)

    lw $t0, 32($s3) #32 is offset, $s3 base register

    add $s1, $s2, $t0 #g = h + A[8]

    14

  • Memory Operand Example 2

    C Code:

    A[12] = h + A[8]; // Assume h in $s2

    // Base address of A in $s3

    Then Compiled MIPS (Index 8 requires offset of 32)

    Index 12 requires offset of ?

    lw $t0, 32($s3) # 32 is offset, $s3 base register

    add $t0, $s2, $t0 # g = h + A[8]

    sw $t0, X($s3) # What is X?

    15

  • Memory Operand Example 2

    C Code:

    A[12] = h + A[8]; // Assume h in $s2

    // Base address of A in $s3

    Then Compiled MIPS (Index 8 requires offset of 32)

    Index 12 requires offset of 4× 12 = 48

    lw $t0, 32($s3) # 32 is offset, $s3 base register

    add $t0, $s2, $t0 # g = h + A[8]

    sw $t0, 48($s3) # A[12] = h + A[8];

    16

  • Registers vs. Memory

    Registers – Faster access than memory

    Operating on memory 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!

    17

  • Immediate Operands

    What about constant data?

    e.g. a = b + 5

    Constant data specified in an instruction:

    addi $s3, $s4, 5

    No subtract immediate instruction

    • Use a negative constant• e.g. addi $s3, $s4, −5

    Design Principle 3: Make the common case fast!

    Small constants are common (e.g. i++, i+=2)

    Immediate operand avoids a load instruction

    18

  • Constant Zero

    In MIPS, register 0 is a constant zero

    Can be addressed as $zero

    Cannot be overwritten

    Useful for common operations

    e.g. Copy registers

    add $t2, $s1, $zero

    19

  • Signed/Unsigned

    Recall from Digital Design:

    For binary 10102:

    • Unsigned = 1010• Sign-Magnitude = −210• 1’s Complement = −510• 2’s Complement = −610

    Signed values in MIPS are 2’s complement values

    20

  • Signed Negation

    For 2’s complement, to negate:

    Complement and add 1

    Example: Negate 210

    2 = 00000010

    Complement = 11111101

    Add1 = 11111110

    -2 = 11111110

    21

  • Sign Extension

    Sign extension – representing same number using more bits

    2810 = 000111002 – Extend to 16 bits

    −1810 = 111011102 – Extend to 16 bits

    22

  • Sign Extension

    Sign extension – representing same number using more bits

    2810 = 000111002 – 00000000000111002

    −1810 = 111011102 – 11111111111011102

    23

  • Representing Instructions

  • Representing Instructions

    Instructions are encoded in binary

    • Called machine code

    MIPS Instructions

    • Encoded as 32-bit instruction words• Small number of formats encoding operation code (opcode),

    register numbers

    • Regularity!

    Register Numbers

    • $t0-$t7 are registers 8-15• $t8-$t9 are registers 24-25• $s0-$s7 are registers 16-23

    24

  • Representing Instructions – R-Format

    Instruction Fields:

    • op – Operation• rs – First source register number• rt – Second source register number• rd – Destination register number• shamt – Shift amount (00000 for now)• funct – function code (extends opcode)

    25

  • R-Format Example

    Example: add $t0, $s1, $s2

    special $s1 $s2 $t0 0 add

    0 17 18 8 0 32

    00000 1001 10010 01000 00000 100000

    The machine code for this instruction would be

    0000010011001001000000001000002

    26

  • Representing Instructions – I-Format

    Immediate arithmetic and load/store instructions

    • rt – Destination or source register number• Constant: −215 to +215 − 1• Address: Offset added to base address in rs

    Design Principle 4 – Good design demands good compromises

    • Different formats complicate decoding, but allow 32-bitinstructions uniformly

    • Keep formats as similar as possible

    27

  • Stored Program Computers

    Instructions represented in binary

    • Just like dataInstructions and data stored in memory

    Programs can operate on programs

    • e.g. compilers, linkers, etc.Binary compatibility allows compiled

    programs to work on different

    computers

    • Standardized ISAs

    Big Picture

    28

  • Logical Operations

    Instructions for bitwise manipulation

    Useful for extracting and inserting groups of bits in a word

    29

  • Shift Operations

    shamt – how many positions to shift

    sll – shift left logical

    • Shift left and fill with 0 bits• sll by i bits multiplies by 2i

    srl – shift right logical

    • Shift right and fill with 0 bits• srl by i bits divides by 2i (unsigned only)

    30

  • AND Operations

    Useful to mask bits in a word

    • Select some bits, clear others to 0

    e.g. and $t0, $t1, $t2

    31

  • OR Operations

    Useful to include bits in a word

    • Set some bits to 1, leave others unchanged

    e.g. or $t0, $t1, $t2

    32

  • NOT Operations

    Useful to invert bits in a word

    • Change 0 to 1, 1 to 0

    MIPS does not have a NOT operation

    Has a 3-operand NOR instruction!

    • a NOR b == NOT (a OR b)

    e.g. nor $t0, $t1, $zero

    33

  • Conditional Operations

    Branch to a labeled instruction if a condition is true

    • Otherwise, continue sequentially

    beq – branch if equal

    • beq rs, rt, L1 – branch to L1 if rs==rt

    bne – branch not equal

    • bne rs, rt, L1 – branch to L1 if rs!=rt

    j – Unconditional jump

    • j L1 – jump to instruction labeled L1

    34

  • Compiling if statements

    Example (code to right)

    Assume f, g, h, i, j in $s0 - $s4

    Compiled MIPS Code:

    bne $s3, $s4, Else

    add $s0, $s1, \$s2

    j Exit

    Else: sub $s0, $s1, $s2

    Exit: (continue with program)

    if (i==j)

    f = g + h;

    else

    f = g-h;

    35

  • Compiling Loop Statements

    Example:

    while(save[i] == k)

    i += 1;

    Assume i in $s3, k in $s5, address of save in $s6

    How would we do this in assembly?

    36

  • Compiling Loop Statements

    Example:

    while(save[i] == k)

    i += 1;

    Assume i in $s3, k in $s5, address of save in $s6

    Loop: sll $t1, $s3, 2

    add $t1, $t1, $s6

    lw $t0, 0($t1)

    bne $t0, $s5, Exit

    addi $s3, $s3, 1

    j Loop

    Exit: (continue with program)

    37

  • Basic Blocks

    Basic Block – sequence of instructions with:

    • No embedded branches (except at end)• No branch targets (except at beginning)

    Compiler identifies basic blocks

    for optimization

    Advanced processor can

    accelerate execution of basic

    blocks

    38

  • More Conditional Operations

    slt – Set less than

    slti – Set if less than immediate

    These two operations set the destination register to 1 if evaluates

    to true, otherwise , set to 0

    slt $t0, $s3, $s4 # $if $s3 < $s4, $t0 = 1

    # else $t0 = 0

    slti $t0 , $s2, 10 # $if $s2 < 10, $t0 = 1

    # else $t0 = 0

    Commonly used with beq, bne

    slt $t0, $s1, $s2 # if($s1 < $s2)

    bne $t0, $zero, L # branch to L39

  • Branch Instruction Design

    Why not blt, bge, etc?

    40

  • Branch Instruction Design

    Why not blt, bge, etc?

    Hardware for

  • Branch Instruction Design

    Why not blt, bge, etc?

    Hardware for

  • Signed vs. Unsigned

    Consider the following:

    11111111111111012 = −210 – 2’s complement11111111111111012 = 6553310 – unsigned

    00000000000000102 = 210

    Assume $s1 = 11111111111111012

    Assume $s2 = 00000000000000102

    What is the value of $t0 after slt $t0, $s1, $s2?

    43

  • Signed vs. Unsigned

    There is no good solution with a single instruction

    Compromise:

    Signed comparison – slt, slti

    Unsigned comparison – sltu, sltui

    Assume $s1 = 11111111111111012

    Assume $s2 = 00000000000000102

    slt $t0, $s1, $s2 # Set $t0 1 if $s1 < $s2 (signed)

    sltu $t1, $s1, $s2 # Set $t1 1 if $s1 < $s2 (unsigned)

    What is the value of $t0, $t1?

    44

  • Switch/Case Statement

    Many languages allow for switch/case statements

    Compiler may unroll them to several if/else statements

    Some may be better coded as a table of jump addresses (called a

    jump table)

    Code indexes into the table, gets the proper memory location

    jr – jump register, allows for jump to a memory address in a

    register rather than a specific location

    We will touch on this later

    45

    Representing Instructions