Chapter 2 Instructions: Language of the Computer - PowerPoint PPT Presentation

1 / 105
About This Presentation
Title:

Chapter 2 Instructions: Language of the Computer

Description:

Encodes machine instructions using symbols and numbers instead of ... and and andi can mask out part of a word. or and ori can combine parts of words together ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 106
Provided by: kevinsc5
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 Instructions: Language of the Computer


1
Chapter 2Instructions Language of the Computer
Computer Organization
  • Kevin Schaffer
  • Department of Computer Science
  • Hiram College

2
Assembly Language
  • Encodes machine instructions using symbols and
    numbers instead of 0's and 1's
  • Every processor architecture has its own assembly
    language
  • Syntax is usually similar
  • Instruction set is main difference
  • We will use MIPS assembly language

3
Why Learn Assembly?
  • Operating systems and device drivers
  • Compilers
  • Time-critical code
  • Using special processor features
  • Understanding what's going on behind the scenes

4
Basic Arithmetic
  • Example add a, b, c
  • add is the operation
  • a, b, and c are the operands
  • a is a destination operand
  • b and c are source operands
  • Adds b and c and stores the result in a
  • Subtract instruction (sub) is similar

5
More Operands
  • If we need more operands then we have to use
    multiple instructions
  • Example a b c d
  • add a, b, c
  • add a, a, d
  • Note that an operand can be both a source and a
    destination

6
Complex Expressions
  • Consider f (g h) (i j)
  • Evaluate expressions in parentheses first, then
    subtract
  • Use temporary variables to store intermediate
    results in complex expressions
  • In assembly language
  • add t0, g, h
  • add t1, i, j
  • sub f, t0, t1

7
Registers
  • Operands must be stored in registers
  • Registers are like memory cells, but they are
    much faster and there are very few of them
  • MIPS has 31 general-purpose registers
  • In assembly language, a register is designated
    with a dollar sign ()
  • For now we will use registers s0...s7 for
    holding variables and registers t0...t9 as
    temporaries

8
Using Registers
  • Once again, consider f (g h) (i j)
  • Assign each variable to a register
  • f is s0, g is s1, h is s2, i is s3, and j is
    s4
  • Replace variable names with register names
  • add t0, s1, s2
  • add t1, s3, s4
  • sub s0, t0, t1

9
Memory
  • There aren't enough registers to hold all the
    variables used by most programs
  • Complex variables like structures and arrays
    cannot be stored in registers either
  • In these cases we must use memory
  • However, arithmetic instructions require their
    operands to be in registers
  • Data transfer instructions move data between
    memory and registers

10
Load
  • The lw (load word) instruction transfers a full
    32-bit word from memory to a register
  • Syntax lw dest, offset(base)
  • The offset (a constant) is added to the value of
    the base register to generate the address
  • The data word located at that address is placed
    in the destination register

11
Accessing an Array Element
  • To access an element of an array you need three
    things
  • Base address
  • Index
  • Width of the base type
  • Formula address (index width)
  • How can we use the lw instruction for this?
  • Place the base address in a register
  • Compute index width and use that as the offset

12
Array Example 1
  • Assume A is an integer array whose base address
    is in s0 x is in s2
  • In C
  • x A3
  • In assembly language
  • lw s2, 12(s0)

13
Store
  • The sw (store word) instruction stores a full
    32-bit word from a register to memory
  • It works just like lw except that the destination
    register is replaced with a source register
  • Syntax sw src, offset(base)
  • The value in the source register is stored in
    memory at the computed address

14
Array Example 2
  • Assume A is an integer array whose base address
    is in s0 x is in s2
  • In C
  • A4 x
  • In assembly language
  • sw s2, 16(s0)

15
Array Example 3
  • Assume A and B are integer arrays whose base
    addresses are in s0 and s1 x is in s2
  • In C
  • A5 B8
  • In assembly language
  • lw s2, 32(s1)
  • sw s2, 20(s0)

16
More Loads and Stores
  • Bytes (8-bit)
  • Load byte (lb)
  • Store byte (sb)
  • Halfwords (16-bit)
  • Load halfword (lh)
  • Store halfword (sh)

17
Immediate Operands
  • Many times the operands to an arithmetic
    operation are small constants
  • It is cumbersome and inefficient to have to load
    these constants from memory
  • Some arithmetic operations permit a 16-bit
    immediate (constant) operand to be encoded
    directly in the instruction
  • Example addi (add immediate)
  • The immediate operand must be the second source

18
Immediate Example
  • Assume a is s0, b is s1
  • In C
  • a
  • b b 5
  • In assembly language
  • addi s0, s0, 1
  • addi s1, s1, 5

19
Zero Register
  • The constant value zero is used frequently
    throughout most programs
  • MIPS has a special register (zero or 0) which
    is hardwired to zero
  • Attempts to change the value of zero are ignored
  • Commonly used to synthesize instructions that
    MIPS does not support directly

20
Bit Operations
  • Arithmetic operations treat the contents of a
    register as a binary number
  • Bit operations work on the individual bits
    without interpreting them in any way
  • We can use bit operations when the content of a
    register is not actually a binary number (bit
    fields)
  • By exploiting the properties of binary numbers we
    can perform some arithmetic operations faster

21
Logical Operations
  • Logical operations combine the bits from two
    words using a Boolean operator (AND, OR, etc.)
  • and and andi can mask out part of a word
  • or and ori can combine parts of words together
  • There is no "not" instruction in MIPS since that
    would violate the two source, one destination
    format use nor instead

22
Logical Example
  • In C
  • a b c
  • d e f
  • g h
  • In assembly language
  • and s0, s1, s2
  • or s3, s4, s5
  • nor s6, s7, s7

23
Moving Values
  • Move the value of one register to another
  • add dest, zero, src
  • or dest, zero, src
  • Move an immediate value directly into a register
  • addi dest, zero, immed
  • ori dest, zero, immed

24
Large Constants
  • addi or ori can load a constant into a register
    so long as it 16 bits or smaller
  • Load upper immediate (lui) can be used to first
    fill the upper 16 bits in order to make a full
    32-bit constant
  • Example
  • lui s0, 15
  • ori s0, 16960

25
Shift Operations
  • Shift instructions move the bits in a word to the
    left or to the right
  • There are three shift instructions
  • Shift left logical (sll)
  • Shift right logical (srl)
  • Syntax sll dest, src, shamt
  • shamt is the shift amount (number of bits) and
    must be a constant between 0 and 31

26
Shifts as Multiplies
  • In C
  • a b 2
  • c d / 8
  • In assembly language
  • sll s0, s1, 1
  • srl s2, s3, 3

27
Multiplication
  • Integer multiplication differs from other
    arithmetic operations since its results may not
    fit within a single register
  • The MIPS multiply instruction (mult) places its
    result into a pair of special-purpose registers
    called hi and lo
  • The mfhi (move from hi) and mflo (move from lo)
    instructions transfer values from hi and lo into
    general-purpose registers
  • Most high-level languages ignore the high word

28
Multiplication Example
  • Assume a is s0, b is s1, and product is s2
  • In C
  • product a b
  • In assembly language
  • mult s0, s1
  • mflo s2

29
Division
  • Integer division produces two results a quotient
    and a remainder
  • The MIPS divide instruction (div) places its
    results in the hi (remainder) and lo (quotient)
    registers
  • High-level languages typically use seperate
    operators for these operations (/ and )

30
Division Example
  • Assume a is s0, b is s1, q is s2, r is s3
  • In C
  • q a / b
  • r a b
  • In assembly language
  • div s0, s1
  • mflo s2
  • mfhi s3

31
Variable Index into Array
  • Previous method for accessing array elements will
    only work if index is constant
  • If index is a variable we must compute the
    address in code
  • Formula address (index width)
  • Since width is usually a power of two, we can use
    a left shift to perform the multiplication

32
Array Example 4
  • Assume A is an integer array whose base address
    is in s0 i is in s1, x is in s2
  • In C
  • x Ai
  • In assembly language
  • sll t0, s1, 2
  • add t1, t0, s0
  • lw s2, 0(t1)

33
Branches and Jumps
  • Normally the processor executes instructions
    sequentially
  • Branch and jump instructions allow us to skip
    over instructions or to go back and repeat them
  • Branches can be conditional or unconditional
  • A conditional branch whose condition is true is
    said to be taken otherwise it is untaken (not
    taken)
  • An untaken branch falls through to the next
    sequential instruction

34
Branch Instructions
  • Branch if equal (beq) compares two registers and
    branches if they are equal
  • Syntax beq src1, src2, label
  • Branch if not equal (bne) is similar but branches
    when the registers are not equal
  • Syntax bne src1, src2, label
  • Jump (j) is an unconditional branch
  • Syntax j label

35
Labels
  • Branch instructions specify where to go using a
    label as an operand
  • The label must be defined somewhere in the
    program by attaching it to the front of another
    instruction
  • Example
  • label add s0, s1, s2

36
If Statement
  • Use a conditional branch to skip past the then
    block when the condition is false
  • When the condition is true the branch won't be
    taken (it will fall through) and the then block
    will be executed
  • Note that the branch should be taken when the
    condition is false so the branch instruction will
    be the opposite of the condition

37
If Example (C)
  • if (a b)
  • z 0
  • z

38
If Example (Asm)
  • bne s0, s1, end
  • or s2, zero, zero
  • end addi s2, s2, 1

39
If-Else Statement
  • Use a conditional branch to skip to the else
    block when the condition is false
  • When the condition is true the branch won't be
    taken and the then block will be executed
  • Use an unconditional branch at the end of the
    then block to skip over the else block

40
If-Else Example (C)
  • if (a b)
  • z 1
  • else
  • z A0
  • z

41
If-Else Example (Asm)
  • bne s0, s1, else
  • ori s2, zero, 1
  • j end
  • else lw s2, 0(s7)
  • end addi s2, s2, 1

42
Loops
  • While
  • Evaluate condition
  • If condition is false go to the end
  • Execute loop body
  • Go back to the beginning
  • Do-While
  • Execute loop body
  • Evaluate condition
  • If condition is true go back to the beginning

43
While Example (C)
  • while (a ! b)
  • a
  • z a

44
While Example (Asm)
  • loop beq s0, s1, end
  • addi s0, s0, 1
  • j loop
  • end or s2, s0, zero

45
Do-While Example (C)
  • do
  • a
  • while (a ! b)
  • z b

46
Do-While Example (Asm)
  • loop addi s0, s0, 1
  • bne s0, s1, loop
  • or s2, s1, zero

47
Comparisons
  • Set on less than (slt) compares its source
    registers and sets its destination register to 1
    if src1 lt src2 and to 0 otherwise
  • Syntax slt dest, src1, src2
  • Set on less than immediate (slti) perform the
    same comparison, but its second source operation
    is an immediate value
  • Syntax slti dest, src1, immed
  • Combined with beq and bne these instructions can
    implement all possible relational operators

48
Relational Branches 1
  • Branch if less than
  • slt t0, src1, src2
  • bne zero, t0, label
  • Branch if greater than or equal
  • slt t0, src1, src2
  • beq zero, t0, label

49
Relational Branches 2
  • Branch if greater than
  • slt t0, src2, src1
  • bne zero, t0, label
  • Branch if less than or equal
  • slt t0, src2, src1
  • beq zero, t0, label

50
Relational Example (C)
  • while (a lt b)
  • a
  • z

51
Relational Example (Asm)
  • loop slt t0, s0, s1
  • beq zero, t0, end
  • addi s0, s0, 1
  • j loop
  • end addi s2, s2, 1

52
Case/Switch Statement
  • Can be implemented like a chain of if-then-else
    statements
  • Using a jump address table is faster
  • Must be able to jump to an address loaded from
    memory
  • Jump register (jr) gives us that ability
  • Syntax jr src

53
Functions
  • A function (or procedure) is a sequence of
    instructions that can be used to perform a task
  • When a function is called control transfers to
    the function once the function completes its
    task it returns
  • Functions may accept parameters (arguments)
    and/or return a value
  • A function can also define local variables for
    its use which exist only until the function
    returns
  • Functions can call other functions

54
Call and Return
  • Call and return are nothing more than
    unconditional jumps
  • The calling function (the caller) jumps to the
    beginning of the function
  • The called function (the callee) jumps back to
    the point from which it was called (return
    address)
  • Since a function can be called from any number of
    places the return address changes with each call

55
Jump and Link
  • The jump and link (jal) instruction performs an
    unconditional jump just like j, but first saves
    the address of the next instruction in the return
    address register (ra)
  • jal label
  • A function returns by jumping to the address
    stored in that register
  • jr ra

56
Function Example 1
  • jal func
  • .
  • .
  • func add t0, t1, t2
  • sub t3, t4, t5
  • jr ra

57
Calling Conventions
  • We need a way to pass parameters to a function
    and get a return value from it
  • A calling convention is a set of rules that
    define how parameters and return values are
    passed to/from functions as well as which
    registers a function can use
  • Allows us to call functions compiled by different
    compilers and even languages

58
Parameters/Return Value
  • Most MIPS software uses the same calling
    convention
  • The first four parameters are placed in registers
    (a0-a3) additional parameters are placed on the
    stack
  • Return values go in registers v0-v1

59
Function Example 2
  • or a0, s0, zero
  • jal func
  • or s0, v0, zero
  • .
  • .
  • func add v0, a0, a0
  • jr ra

60
Register Use
  • Callee-saved registers are owned by caller if a
    function uses a callee-saved register it must
    save the old value and restore that value before
    it returns
  • Caller-saved registers are owned by the callee
    if a caller is using a caller-saved register, it
    must save the old value before calling the
    function and restore it after the function
    returns
  • In MIPS, s0-s7 are callee-saved and t0-t9 are
    caller-saved

61
The Stack
  • Data that a function needs to save in memory is
    pushed onto the stack
  • Before returning, the function pops the data off
    of the stack
  • In MIPS, the stack pointer (sp) contains the
    address of the last word pushed onto the stack
  • The stack grows downward from higher addresses to
    lower ones

62
Accessing the Stack
  • Push a word onto the stack
  • addi sp, sp, -4
  • sw src, 0(sp)
  • Pop a word off of the stack
  • lw dest, 0(sp)
  • addi sp, sp, 4
  • Typically we batch multiple pushes and pops
    together

63
Accessing the Stack (cont'd)
  • Push three words onto the stack
  • addi sp, sp, -12
  • sw t1, 8(sp)
  • sw t0, 4(sp)
  • sw s0, 0(sp)
  • Pop three words off of the stack
  • lw s0, 0(sp)
  • lw t0, 4(sp)
  • lw t1, 8(sp)
  • addi sp, sp, 12

64
Stack Illustration
65
Data on the Stack
  • Data a function puts on the stack makes up its
    stack frame or activation record
  • If there are more than four parameters, the extra
    parameters must be pushed onto the stack
  • Non-leaf functions must save the value of the
    return address register (ra)

66
Data on the Stack (cont'd)
  • Any registers that must be saved are stored on
    the stack
  • Also local variables that won't fit into
    registers (either because there are no registers
    left or the data is too big)
  • Commonly a frame pointer (fp) is used in
    addition to the stack pointer, since the stack
    pointer could change during the function

67
Stack Frame
68
MIPS Registers
69
Recursion Example (C)
  • int fact(int n)
  • if (n lt 1)
  • return 1
  • else
  • return fact(n - 1) n

70
Recursion Example (Asm)
  • fact addi sp, sp, -8
  • sw ra, 4(sp)
  • sw a0, 0(sp)
  • slti t0, a0, 1
  • beq t0, zero, else
  • addi v0, zero, 1
  • addi sp, sp, 8
  • jr ra

71
Recursion Example (Asm)
  • else addi a0, a0, 1
  • jal fact
  • lw a0, 0(sp)
  • lw ra, 4(sp)
  • addi sp, sp, 8
  • mult v0, a0
  • mflo v0
  • jr ra

72
Program Memory
73
Static Data Area
  • Global variables and static fields
  • Fixed size
  • Accessed through the global pointer (gp)

74
Dynamic Data Area
  • Allocated dynamically with new or malloc
  • Also known as the heap
  • Managed by the programmer
  • In C/C, programmer must deallocate this memory
    explicitly with delete or free
  • In Java, the garbage collector automatically
    deallocates this memory

75
Stack
  • Local variables and function-related information
  • Managed automatically by the compiler in
    high-level languages
  • Accessed through the frame pointer (fp) or stack
    pointer (sp)

76
Instruction Formats
  • R-type
  • Arithmetic, logic, and shift instructions
  • All register operands
  • I-type
  • Arithmetic and logic with immediates
  • Conditional branch instructions
  • Load and store instructions
  • J-type
  • Jump and jump-and-link

77
R-Type
  • op operation code or opcode
  • rs first source register
  • rt second source register
  • rd destination register
  • shamt shift amount
  • funct function code

78
R-Type Example
  • In assembly add s0, s1, s2

79
I-Type
  • op opcode
  • rs first source register
  • rt second source register or destination
    register
  • immed immediate operand or branch displacement

80
I-Type Example
  • In assembly addi s0, s1, 5

81
J-Type
  • op opcode
  • addr branch target address

82
How Branches Work
  • The program counter (PC) is a register that
    contains the address of the instruction that the
    processor is currently executing
  • Most of time the processor increments the value
    of PC by 4 after executing an instruction
  • Branch instructions allow the PC to be modified
    in other ways

83
Computing Branch Targets
  • For J-type instructions (j and jal) the lower
    bits of the target address are taken directly
    from the instruction
  • This is called pseudodirect addressing
  • For I-type instructions (beq and bne) the
    immediate field is added to the PC
  • This is called PC-relative addressing
  • jr simply copies the register's value into the PC

84
Branching Far Away
  • An immediate value is limited to 16 bits
  • The target of a conditional branch must be within
    32,767 instructions of the branch instruction
    itself
  • To branch farther, use two instructions
  • bne s0, s1, L2
  • j L1
  • L2 ...

85
Translating a C Program
86
Assembler
  • Extends the machine's instruction set through
    pseudoinstructions
  • Accepts number in multiple bases
  • Maintains a symbol table mapping labels into
    addresses
  • Generates an object file for the linker

87
Pseudoinstructions
  • Makes assembly language easier to read/write
  • Example
  • move t0, t1
  • instead of
  • add t0, zero, t1
  • Assembler may use register at as a temporary
    when expanding pseudoinstructions

88
Object File
  • Object file header
  • Text segment
  • Static data segment
  • Relocation information
  • Symbol table
  • Debugging information

89
Object File Example
90
Linker
  • Combines multiple object files together into a
    single executable file
  • Uses relocation information and symbol table to
    resolve interobject dependencies
  • Patches the machine code with the final addresses

91
Dynamic Linking
  • Dynamically linked libraries (DLLs)
  • Delays the linking process until the program runs
  • Allows for a single set of shared libraries,
    stored in a centralized location
  • Function is linked the first time it is called
  • Subsequent calls do not incur any additional
    overhead

92
Dynamic Linking Example
93
Translating a Java Program
94
Java Technologies
  • Bytecode
  • Portable machine language for Java programs
  • Java virtual machine (JVM)
  • Interprets Java bytecode
  • Just-in-time (JIT) compiler
  • Translates frequently used portions of bytecode
    into native machine language as the program runs

95
How Compilers Optimize
96
High-Level Optimizations
  • Function inlining
  • Replaces a call to a function with the function
    body
  • Loop unrolling
  • Replicates the body of a loop multiple times in
    order to reduce the number of iterations

97
Local Optimizations
  • Common subexpression elimination
  • Remove code that computes the same value twice
  • Constant propagation
  • Replace a variable that is assigned a constant
    with the constant itself
  • Stack height reduction
  • Rearrange expression tree to minimize resources
    needed for evaluation

98
Global Optimizations
  • Global common subexpression elimination
  • Copy propagation
  • If A is assigned the value of B, replace A with B
  • Code motion
  • Remove code from a loop that computes the same
    value each iteration
  • Induction variable elimination
  • Simplify array addressing calculations within
    loops

99
Low-Level Optimizations
  • Strength reduction
  • Replace multiply with left shift, etc.
  • Pipeline scheduling
  • Reorder instructions to improve pipeline
    performance
  • Branch offset optimization
  • Choose shortest branch displacement that reaches
    the target

100
SPIM
  • MIPS assembly language simulator
  • Available athttp//pages.cs.wisc.edu/larus/spim.h
    tml

101
MIPS Assembler
  • Assembler directives tell the assembler how to
    translate a program, but don't generate code
  • MIPS assembler directives begin with a dot (.)
  • Comments begin with a number sign () and
    continue to the end of the line
  • Supports pseudoinstructions in addition to the
    actual MIPS instruction set

102
Assembler Directives
  • Set output to data segment or text segment
  • .data
  • .text
  • Declare that a label is global
  • .globl symbol
  • Store data values into the output
  • .asciiz, .byte, .half, .word, etc.

103
Pseudoinstructions
  • Load address into register
  • la dest, offset(base)
  • Load immediate value into register
  • li dest, immed
  • Move register to register
  • move dest, src

104
System Calls
  • MIPS program request operating system services
    through the syscall instruction
  • Each system service is identified by a number
  • Place the number in the v0 register before
    executing syscall
  • Arguments go in a0a3 and return values are
    placed back in v0, just like a function call

105
System Calls in SPIM
  • Print Integer
  • Put 1 in v0
  • Put integer value to output in a0
  • Print String
  • Put 4 in v0
  • Put address of string to output in a0
  • Read Integer
  • Put 5 in v0
  • Result will be placed in v0
Write a Comment
User Comments (0)
About PowerShow.com