Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of...

33
Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) • MIPS encoding of instructions • Spim simulator • more examples of MIPS programming • subroutines and functions
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of...

Page 1: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Feb 18, 2009 Lecture 4-2

• instruction set architecture (Part II of [Parhami])

• MIPS encoding of instructions

• Spim simulator

• more examples of MIPS programming

• subroutines and functions

Page 2: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.1.

In the MIPS instruction format (Figure 5.4), we can reduce the opcode field from 6 to 5 bits and the function field from 6 to 4 bits. Given the number of MIPS instructions and different functions needed, these changes will not limit the design. The 3 bits thus gained can be used to extend rs, rt and rd fields from 5 bits to 6 bit each.

a) List two positive effects of these changes. Justify your answers.

b) List two negative effects of these changes. Justify your answers.

Page 3: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.1

Solution:(a)Can use 64 registers, and this will help to

reduce the memory traffic. Larger field for jump address (one extra bit ) doubles the span of the jump.

(b)Shorter field for immediate operands (15 rather than 16). Also limits the future extensions of the ALU operations. Decoding logic may become more complex.

Page 4: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.2

A number of bitwise logic operations might be useful for certain applications. Show how the following logic operations can be synthesized by means of MiniMIPS instructions covered in this chapter. Try to use as few instructions as possible.

(a)NOT

Page 5: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.2

A number of bitwise logic operations might be useful for certain applications. Show how the following logic operations can be synthesized by means of MiniMIPS instructions covered in this chapter. Try to use as few instructions as possible.

(a)NOT

nor $t0, $t0, $zero

Page 6: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.2

A number of bitwise logic operations might be useful for certain applications. Show how the following logic operations can be synthesized by means of MiniMIPS instructions covered in this chapter. Try to use as few instructions as possible.

(b) NAND

Page 7: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.2

A number of bitwise logic operations might be useful for certain applications. Show how the following logic operations can be synthesized by means of MiniMIPS instructions covered in this chapter. Try to use as few instructions as possible.

(b) NAND

and $t0, $t0, $t1nor $t0, $t0, $zero

Page 8: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.3

Write a sequence of MiniMIPS instructions that adds two integers stored in $s1 and $s2, stores the sum (mod 2^32) in register $s3, and sets the register $t0 to 1 (0) if there is an overflow (if there is no overflow).

Claim: The sum A + B (where A, B and the result C are 32 bit two’s complement integers) causes overflow if and only if (a) both are of same sign and (b) both A and B are positive (negative) and C <= A (C > A).

Proof:

Page 9: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Solution for Problem 5.3:

$t0 = 0 $s3 = $s1 + $s2 if ($s1 > 0) goto test1 if ($s2 > 0) goto done if ($s3 > $s1) then $t0 = 1 goto done test1: if (s2 <= 0) goto done if ($s3 < $s1) then $t0 = 1done:

Page 10: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.7

Write a sequence of MiniMIPS instructions to swap the contents of $s0 and $s1 without disturbing the content of any other register.

Page 11: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.7

Write a sequence of MiniMIPS instructions to swap the contents of $s0 and $s1 without disturbing the content of any other register.

Solution:

xor $s0, $s0, $s1

xor $s1, $s0, $s1

xor $s0, $s0, $s1

Page 12: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.13

Modify the solution to Example 5.5 so that it (a) finds the smallest integer in the list (b) the list element whose significant byte is the largest.

(a) A single change will do this!

Page 13: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.13

Modify the solution to Example 5.5 so that it (a) finds the smallest integer in the list (b) the list element whose significant byte is the largest.

Solution to (a)

Change

slt $t4, $t0, $t3 to

to

slt $t4, $t3, $t0

Page 14: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Problem 5.13

Modify the solution to Example 5.5 so that it (a) finds the smallest integer in the list (b) the list element whose significant byte is the largest.

Solution to (b)

After loading a word from memory into register $t0, check if it is positive. If so, continue with comparison.

Else, change the value from y to – y.

sub $t0, $zero, $t0 is one way to do this.

Page 15: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

SPIM – A simulator for MIPS Assembly language

Page 16: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Screen shot of the various windows

Page 17: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Tutorials on SPIM

There are many good tutorials on SPIM. Here are two introductory ones:

http://www.cs.umd.edu/class/fall2001/cmsc411/projects/spim/

http://users.ece.gatech.edu/~sudha/2030/temp/spim/spim-tutorial.html

Please read (watch) them once and refer to them when you need help.

Page 18: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Example of SPIM program

We will implement the code written in Example 5.5 (finding the max element in an array).

Code is shown below:

Page 19: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Creating the code and the text segments in SPIM

.text.globl __start

__start: la $s1, array # initialize the starting address of array to t5 lw $t0, ($s1) la $t6, count lw $s2, ($t6)

• la is a pseudo-instruction (load address).

• We use this to input the starting address and the size of the arrays into registers $s1 and $t6.

Page 20: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

addi $t1,$zero, 0 # initialize index i to 0

loop: add $t1,$t1,1 # increment index i by 1

beq $t1,$s2,done # if all elements examined, quit

add $t2,$t1,$t1 # compute 2i in $t2

add $t2,$t2,$t2 # compute 4i in $t2

add $t2,$t2,$s1 # form address of A[i] in $t2

lw $t3,0($t2) # load value of A[i] into $t3

slt $t4,$t0,$t3 # maximum < A[i]?

beq $t4,$zero,loop # if not, repeat with no change

addi $t0,$t3,0 # if so, A[i] is the new maximum

j loop # change completed; now repeat

done:

The rest of the code is taken from Example 5.5 of the text.

Page 21: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Input to the program – Data Segment

.data

array: .word 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13count: .word 15endl: .asciiz "\n"ans2: .asciiz "\nmax = "

Page 22: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Output the result

Input array: 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13

Page 23: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

System calls for output

la $a0,ans2 li $v0,4 syscall # print "\nmax = "

move $a0,$t0 li $v0,1 syscall # print max

la $a0,endl # system call to print li $v0,4 # out a newline syscall

li $v0,10 syscall # end

Page 24: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Output read from Console

Page 25: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Example 2: Compute n! for a given input n.

First, write the code to implement factorial computation. We need an instruction for multiplication:

mul $s1, $s2, $s3

Page 26: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Example 2: Compute n! for a given input n.

First, write the code to implement factorial computation. We need an instruction for multiplication:

mul $s1, $s2, $s3

The following code computes n! where n is in register $t0

li $t1, 0 # initialize loop counter li $t2, 1 # initialize product to 1

loop: addi $t1, $t1, 1 # increment loop counter by 1 mul $t2, $t2, $t1 # update value of t2. beq $t0, $t1, exit # if if counter = N, goto exit j loop # otherwise loop again

Page 27: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Input through the console

In this example, we will enter the input through the console.

.text

.globl __start__start: li $v0, 4 # mesg1 asking for a number la $a0, msg1 syscall li $v0,5 # system call that reads an integer syscall move $t0, $v0

The rest of the details are already known.

Page 28: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

The programWorks fine forn = 12 and computes n! correctly.

But for n = 13, we get …

Page 29: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Can you explain what went wrong? How do we fix this problem?

Page 30: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

The problem is caused by overflow

12! = 479001600 is less than 232 = 4294967296

But 13! = 6227020800 > 4294967296

We need an instruction that computes the product of 32 bits correctly.

Here is the instruction level support for it in MIPS: mult $s1, $s2 # result goes into registers Hi and lomfhi $t1 # move the high 32 bits into $t1mflo $t2 # move the low 32 bits into $t2

Page 31: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Overcoming the overflow problem

• Suppose X is a 64 bit integer X = P x 232 + Q

• Let Y be a 32 bit integer.

• Then, X Y can be obtained as follows:

First compute QY = (R x 232 + S)

X Y = (P Y + R) 232 + S

Check this with an example …

Page 32: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

li $t1, 0 # initialize loop counter

li $t2, 1 # initialize product to 1

li $t3, 0

li $t4, 0

loop: addi $t1, $t1, 1 # increment loop counter by 1

mult $t2, $t1

mfhi $t4

mflo $t2

mul $t3, $t3, $t1

add $t3, $t3, $t4

beq $t0, $t1, exit # if counter = N, goto exit label

j loop # otherwise loop again

Computation of factorial for larger n’s

Page 33: Feb 18, 2009 Lecture 4-2 instruction set architecture (Part II of [Parhami]) MIPS encoding of instructions Spim simulator more examples of MIPS programming.

Now we get the following result:

Can you explain the result?

How do we fix this?