1 of 45

Chapter 2

Instructions: Language of the Computer

2 of 45

Machine Language and Assembly Language

  • To command a computer’s hardware, you must speak its language.
  • The words of a computer’s language are called instructions, and its vocabulary is called an instruction set.
  • Computer instructions can be represented as sequences of bits. This representation is called machine language
  • A slightly higher-level representation (and one that is much easier for humans to use) is called assembly language
  • The chosen instruction set comes from MIPS Technologies.

3 of 45

Instruction Set

  • The repertoire of instructions of a computer
  • Different computers have different instruction sets
    • But with many aspects in common
  • Early computers had very simple instruction sets
    • Simplified implementation
  • Many modern computers also have simple instruction sets

4 of 45

 Three Other Popular Instruction Sets

  • ARMv7 is similar to MIPS.
  • Intel x86, which powers both the PC and the cloud of the PostPC Era
  • ARMv8, which extends the address size of the ARMv7 from 32 bits to 64 bits

5 of 45

Stored-program Computer Concept

  • Stored-program computer is the idea that instructions and data of many types can be stored in memory as numbers

6 of 45

 �Operations Of The Computer Hardware

  • Every computer must be able to perform arithmetic
  • Example of MIPS assembly language notation:

add a, b, c

The above means add b and c and put the result in a

  • In MIPS, data must be in registers to perform arithmetic

7 of 45

Three Underlying Principles Of Hardware Design:

  1. Simplicity favors regularity.
  2. Smaller is faster

8 of 45

The MIPS Instruction Set

  • Used as the example throughout the course
  • Stanford MIPS commercialized by MIPS Technologies (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 and E

Chapter 2 — Instructions: Language of the Computer — 8

9 of 45

MIPS Assembly Language

  • MIPS stands for Microprocessor without Interlocked Pipeline Stages
  • MIPS assembly language is a 3-address assembly language. Operands are either immediates or in registers.
  • There are 32 registers that we commonly use. Each is 32 bits wide. The registers are identified by an integer, numbered 0 - 31.
  • To reference a register as an operand, use the syntax�$x, where x is the number of the register you want.�examples: $12, $15

10 of 45

The MIPS Assembly Language Notation

  • add a, b, c
  • The above MIPS assembly language notation instructs a computer to add the two variables b and c and to put their sum in a.
  • Type a MIPS add instruction that computes:�z = x + y
  • Solution: add z, x, y or add z y, x
  • The natural number of operands for an operation like addition is three: the two numbers being added together and a place to put the sum.

11 of 45

MIPS Notation

  • This notation is rigid in that each MIPS arithmetic instruction performs only one operation and must always have exactly three variables.

12 of 45

Arithmetic Operations

  • Add and subtract, three operands
    • Two sources and one destination

add a, b, c # a gets b + c

  • All arithmetic operations have this form
  • Design Principle 1: Simplicity favours regularity
    • Regularity makes implementation simpler
    • Simplicity enables higher performance at lower cost

Chapter 2 — Instructions: Language of the Computer — 12

13 of 45

Example of Compiling Two C Assignment Statements into MIPS.

  • C Code
    • a = b + c;
    • d = a - e;
  • MIPS
    • add a, b, c # register a contains b + c
    • sub d, a, e # register d contains a – e
  • Sharp symbol (#) on each line is a comment

14 of 45

Basic Instruction Execution

  • Given: b = 2, c = 5, d = 1.
  • add a, b, c. What is the final value of a?

  • add t, d, c
  • add a, t, c # What is the final value of a

  • sub t, c, b
  • add a, t, d # What is the final value of a

15 of 45

 ���Example 1 of compiling a complex C assignment into MIPS.���

  • C Code
    • f = (g + h) - (i + j);
  • MIPS
    • add t0, g, h # temp t0 contains g + h
    • add t1, i, j # temp t1 contains i + j
    • sub f, t0, t1 # f gets t0 - t1, which is (g + h) - (i + j)

16 of 45

Examples

  • add $s1, $s2, $s3
  • addi $s1, $s3, 50

17 of 45

MIPS Continued

  • The natural number of operands for an operation like addition is three: the two numbers being added together and a place to put the sum.
  • Requiring every instruction to have exactly three operands, no more and no less, conforms to the philosophy of keeping the hardware simple: hardware for a variable number of operands is more complicated than hardware for a fixed number. This situation illustrates the first of three underlying principles of hardware design:

18 of 45

Register Operands

  • Arithmetic instructions use register operands
  • MIPS has a 32 × 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
    • c.f. main memory: millions of locations

19 of 45

MIPS operands

20 of 45

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

Chapter 2 — Instructions: Language of the Computer — 20

21 of 45

MIPS assembly language for Arithmetic

22 of 45

Registers vs. Memory

  • Registers are faster to access than memory
  • 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!

Chapter 2 — Instructions: Language of the Computer — 22

23 of 45

MIPS assembly language Data Transfer

24 of 45

Instruction Types

  1. Data movement instructions:
    • Data movement instructions are the most frequently used instructions. Data is moved from memory into registers, from registers to registers, and from registers to memory, and many machines provide different instructions depending on the source and destination.
  2. Arithmetic instructions:
    • Instructions that use integers and floating-point numbers.
    • Many instruction sets provide different arithmetic instructions for various data sizes
    • Arithmetic operations occur only on registers in MIPS instructions

25 of 45

Instruction Types Continued

  1. Boolean logic instructions:
    • Instructions perform Boolean operations, much in the same way that arithmetic operations work.
    • Instructions for performing AND, NOT, and often OR and XOR operations.
  2. Bit manipulation (shift and rotate)
    • Bit manipulation instructions are used for setting and resetting individual bits (or sometimes groups of bits) within a given data word. These include both arithmetic and logical shift instructions and rotate instructions, both to the left and to the right. Logical shift instructions simply shift bits to either the left or the right by a specified amount, shifting in zeros from the opposite end.

26 of 45

Instruction Types Continued

  1. I/O instructions
    • The basic schemes for handling I/O are programmed I/O, interrupt-driven I/O, and DMA devices
  2. Transfer of control instructions
    • Control instructions include branches, skips, and procedure calls. Branching can be unconditional or conditional. Skip instructions are basically branch instructions with implied addresses. Because no operand is required, skip instructions often use bits of the address field to specify different situations

27 of 45

Instruction Types Continued

  1. Special purpose instructions
    • Special purpose instructions include those used for string processing, high-level language support, protection, flag control, and cache management. Most architectures provide instructions for string processing, including string manipulation and searching.

28 of 45

MIPS Assembly Language Logical

29 of 45

Assembly Language

30 of 45

�Operands of the Computer Hardware�

  • The operands of arithmetic instructions are restricted; they must be from a limited number of special locations built directly in hardware called registers
  • The size of a register in the MIPS architecture is 32 bits; groups of 32 bits occur so frequently that they are given the name word in the MIPS architecture
  • In the MIPS architecture, each register is 32 bits wide.

31 of 45

Compiling a C Assignment Using Registers.

  • Consider f = (g + h) - (i + j);
  • The variables f, g, h, i, and j are assigned to the registers $s0, $s1, $s2, $s3, and $s4, respectively. What is the compiled MIPS code?
  • Solution:
    • add $t0, $s1, $s2 # register $t0 contains g + h
    • add $t1, $s3, $s4 # register $t1 contains i + j
    • sub $s0, $t0, $t1 # f gets $t0 - $t1, which is (g + h) - (i + j)

32 of 45

�Memory operands�

  • Programming languages have simple variables that contain single data elements. They also have more complex data structures—arrays and structures.  How can a computer represent and access such large structures?
  • The processor can keep only a small amount of data in registers, but computer memory contains billions of data elements. Hence, data structures (arrays and structures) are kept in memory.
  • Arithmetic operations occur only on registers in MIPS instructions; thus, MIPS must include instructions that transfer data between memory and registers. This is data transfer instructions

33 of 45

  • To access a word in memory, the instruction must supply the memory address. Memory is just a large, single-dimensional array, with the address acting as the index to that array, starting at 0.
  • The data transfer instruction that copies data from memory to a register is traditionally called load.
  • The format of the load instruction is the name of the operation followed by the register to be loaded, then a constant and register used to access memory.
  • The actual MIPS name for this instruction is lw, standing for load word.
  • The format of the load instruction is the name of the operation followed by the register to be loaded, then a constant and register used to access memory

34 of 45

Memory Operands

  • Main memory used for composite data
    • Arrays, structures, dynamic data
  • To apply arithmetic operations
    • Load values from memory into registers
    • Store result from register to memory
  • Memory is byte addressed
    • 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
    • c.f. Little Endian: least-significant byte at least address

35 of 45

Memory Operand Example 1

  • 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

36 of 45

��Load Word Instruction��

  • g = h + A[8];

37 of 45

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

38 of 45

Load word instruction.

  • Assume $s3 has 5000, and words addressed 5000..5002 have the data shown:�5000: 99�5001: 77�5002: 323

  • What address will be computed by:�lw $t0, 2($s3)
  • 2($s3) computes the address as 2 + (the value in $s3), so 2 + 5000, or 5002.

39 of 45

Load word instruction.

  • Assume $s3 has 5000, and words addressed 5000..5002 have the data shown:�5000: 99�5001: 77�5002: 323

  • What value will be put in $t0 by:�lw $t0, 0($s3)
  • 0($s3) will compute the address 0 + 5000, or 5000. The value in the word at address 5000 is 99. Thus, 99 will be put in $t0.

40 of 45

Load word instruction.

  • Assume $s3 has 5000, and words addressed 5000..5002 have the data shown:�5000: 99�5001: 77�5002: 323

  • What value will be put in $t1 by:�lw $t1, 2($s3)
  • 2($s3) will compute the address 2 + 5000, or 5002. The value in the word at address 5002 is 323. Thus, 323 will be put in $t1.

41 of 45

Store word

  • Load word and store word are the instructions that copy words between memory and registers in the MIPS architecture.
  • Store copies data from a register to memory
  • The actual MIPS name is sw, standing for store word.

42 of 45

���Example of compiling using load and store.���

43 of 45

  • If $s3 has 900, what address does this instruction compute?�sw $t0, 20($s3)
  • 20($s3) computes 20 + (the value in $s3), so 20 + 900, or 920.

44 of 45

Constant or immediate operands

  • Constant data specified in an instruction
  • addi $s3, $s3, 4 # $s3 = $s3 + 4
  • Many times a program will use a constant in an operation
  • add immediate or addi.
  • 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

45 of 45

Readings

  • Chapter 2.1 to 2.3