1 of 34

Chapter 2

Representing Instructions in the Computers, MIPS Logical Operations and Instructions for Making Decisions

2 of 34

Representing numbers in a word

  • In our last lecture we looked at converting numbers to other bases. We are now ready to explain the difference between the way humans instruct computers and the way computers see instructions.
  • We number the bits 0, 1, 2, 3, . . . from right to left in a word. The drawing below shows the numbering of bits within a MIPS word and the placement of the number 10112

  • The least significant bit is the rightmost bit in a MIPS word.
  • The most significant bit is the left most bit in a MIPS word.

3 of 34

Representing instructions in the computer

  • Instructions are kept in the computer as a series of high and low electronic signals and may be represented as numbers.
  • Each piece of an instruction can be considered as an individual number, and placing these numbers side by side forms the instruction.
  • Since registers are referred to in instructions, there must be a convention to map register names into numbers
  • In MIPS assembly language, registers $s0 to $s7 map onto registers 16 to 23, and registers $t0 to $t7 map onto registers 8 to 15. Hence, $s0 means register 16, $s1 means register 17, $s2 means register 18, …, $t0 means register 8, $t1 means register 9, and so on.

4 of 34

Instruction Format

  • Instruction format is a form of representation of an instruction composed of fields of binary numbers.
  • Layout of the instruction 
  • MIPS instruction takes exactly 32 bits—the same size as a data word
  • Machine language: Binary representation used for communication within a computer system.

5 of 34

Translating a MIPS Assembly Instruction into a Machine Instruction

  • add $t0, $s1, $s2
  • The decimal representation:

  • Each of these segments of an instruction is called a field. The first and last fields (containing 0 and 32 in this case) in combination tell the MIPS computer that this instruction performs addition.

6 of 34

MIPS Fields

  • op: Basic operation of the instruction, traditionally called the opcode.
  • rs: The fi rst register source operand.
  • rt: The second register source operand.
  • rd: The register destination operand. It gets the result of the operation.
  • shamt: Shift amount. (Section 2.6 explains shift instructions and this term; it will not be used until then, and hence the field contains zero in this section.)
  • funct: Function. This field, often called the function code, selects the specific variant of the operation in the op field.

  • Opcode: The field that denotes the operation and format of an instruction.
  • A problem occurs when an instruction needs longer fields than those shown above. For example, the load word instruction must specify two registers and a constant. 

7 of 34

Instruction Format

  • There are 3 main instruction formats in MIPS. The fields in each type are laid out in such a way that the same fields are always in the same place for each type.
    1. R-type (for register) or R-format.
    2. I-type (for immediate) or I-format and is used by the immediate and data transfer instructions
    3. J-Type
  •  In this lesson we only discuss the R-type and the I-type
  • There are used for different kinds of instruction formats for different kinds of instructions. 

8 of 34

R-Type Instructions

  • They are identified by the opcode 0 and are differentiated by the funct values
  • These instructions can be used for arithmetic operations, jumps and system calls

9 of 34

I-Type instruction

  • These instruction has an opcode of a number greater than 3

10 of 34

Instruction formats

11 of 34

Sample Instructions

12 of 34

 R-type and I-type instructions.

  • What type of instruction is add? R-type
  • What type of instruction is addi (add immediate)? I-type
  • What type of instruction is sw (store word)? I-type

13 of 34

Example: Translating MIPS assembly language into machine language

  • If $t1 has the base of the array A and $s2 corresponds to h, the assignment statement�A[300] = h + A[300];

is compiled into

lw $t0, 1200($t1) # Temporary reg $t0 gets A[300]

add $t0, $s2, $t0 # Temporary reg $t0 gets h + A[300]

sw $t0, 1200($t1) # Stores h + A[300] back into A[300]

What is the MIPS machine language code for these three instructions?

14 of 34

Solution

  • We know that the lw instruction is identified by 35 in the first field (op).
  • The base register 9 ($t1) is specified in the second field (rs), and the destination register 8 ($t0) is specified in the third field (rt). The offset to select A[300] (1200 = 300 × 4) is found in the final field (address).
  • The add instruction that follows is specified with 0 in the first field (op) and 32 in the last field (funct). The three register operands (18, 8, and 8) are found in the second, third, and fourth fields and correspond to $s2, $t0, and $t0.
  • The sw instruction is identified with 43 in the first field. The rest of this final instruction is identical to the lw instruction.

15 of 34

Solution

  • Let's first represent the machine language instructions using decimal numbers

16 of 34

Solution

17 of 34

Example

  • Translate addi $t7, $t4, 5 to the corresponding MIPS machine language code. The fields of an I-format instruction are provided below:
  • Solution:

18 of 34

Solution

  • The addi instruction is specified with a 8ten in the op field.
  • The rs field stores the first source register, $t4. $t0-$t7 are register numbers 8-15, meaning $t4 is register number 12.
  • The meaning of the rt field has changed for the addi instruction: the rt field specifies the destination register. $t0-$t7 are register numbers 8-15, meaning $t7 is register number 15. 
  • 5ten is the constant value

19 of 34

MIPS machine language code

8

12

15

5

Machine code

20 of 34

MIPS architecture 

21 of 34

Test Your knowledge

  • Opcode 35 indicates a _____ instruction: load word
  • Opcode _____ indicates a store word instruction: 43
  • Opcode 0 and a funct field of 34 indicates a(n) _____ instruction: substract
  • addi's opcode is _____: 8
  • The stored-program concept means: The program are stored in memory along data

22 of 34

���MIPS instructions.���

  • Which MIPS instruction does the following represent?

  • Solution: sub $t2, $t0, $t1

23 of 34

The stored-program concept

  • This means programs are stored in memory together with data
  • Instructions are represented as numbers.
  • Programs are stored in memory to be read or written, just like data.

24 of 34

Logical operations

  •  C and Java logical operators and their corresponding MIPS instructions

25 of 34

Shifts

  •  A shift moves all the bits in a word to the left or right, filling the emptied bits with 0s.
  •  The two MIPS shift instructions are called shift left logical (sll) and shift right logical (srl)
  • sll $t2, $s0, 4 # reg $t2 = reg $s0 << 4 bits

26 of 34

shamt field in the R-format

  • shamt is used in shift instructions
  • shamt stands for shift amount
  • sll $t2, $s0, 4 # reg $t2 = reg $s0 << 4 bits

  • The encoding of sll is 0 in both the op and funct fields, rd contains 10 (register $t2), rt contains 16 (register $s0), and shamt contains 4. The rs field is unused and thus is set to 0

27 of 34

AND

  • AND: A logical bit- by-bit operation with two operands that calculates a 1 only if there is a 1 in both operands.

28 of 34

OR

  • A logical bit-by-bit operation with two operands that calculates a 1 if there is a 1 in either operand.
  • Using register $t1 and $t2 from the previous slide,

or $t0, $t1, $t2 # reg $t0 = reg $t1 | reg $t2

The value in register $t0:

0000 0000 0000 0000 0011 1101 1100 0000two

29 of 34

NOT

  • NOT takes one operand and places a 1 in the result if one operand bit is a 0, and vice versa.

30 of 34

Instructions for Making Decisions

  • Decision making is commonly represented in programming languages using the if statement, sometimes combined with go to statements and labels. 
  • MIPS assembly language includes two decision-making instructions, similar to an if statement with a go to.

31 of 34

branch if equal (beq)

  • Syntax: beq register1, register2, L1
  • Semantics: go to the statement labeled L1 if the value in register1 equals the value in register2
  • Example: if (i == j) f = g + h
  • The above C code is

32 of 34

branch if not equal (bne)

  • Syntax: bne register1, register2, L1
  • Semantics: go to the statement labeled L1 if the value in register1 does not equal the value in register2

33 of 34

Conditional branch

  • Conditional branch: An instruction that requires the comparison of two values and that allows for a subsequent transfer of control to a new address in the program based on the outcome of the comparison.
  • bne and beq are called conditional branches

34 of 34

�Compiling if-then-else into Conditional Branches�

  • In the following code segment, f, g, h, i, and j are variables. If the five variables f through j correspond to the five registers $s0 through $s4, what is the compiled MIPS code for this C if statement?

if (i == j) f = g + h; else f = g – h;

Solution:

bne $s3,$s4,Else # go to Else if i ≠ j

add $s0,$s1,$s2 # f = g + h (skipped if i ≠ j)

j Exit # go to Exit

Else:sub $s0,$s1,$s2 # f = g – h (skipped if i = j)

Exit: