1 / 33

Feb 18, 2009 Lecture 4-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 subroutines and functions. Problem 5.1.

cdickerson
Download Presentation

Feb 18, 2009 Lecture 4-2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • subroutines and functions

  2. 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. • List two positive effects of these changes. Justify your answers. • List two negative effects of these changes. Justify your answers.

  3. Problem 5.1 Solution: • 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. • Shorter field for immediate operands (15 rather than 16). Also limits the future extensions of the ALU operations. Decoding logic may become more complex.

  4. 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. • NOT

  5. 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. • NOT • nor $t0, $t0, $zero

  6. 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

  7. 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, $t1 nor $t0, $t0, $zero

  8. 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:

  9. 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 = 1 done:

  10. 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.

  11. 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

  12. 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!

  13. 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

  14. 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.

  15. SPIM – A simulator for MIPS Assembly language

  16. Screen shot of the various windows

  17. 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.

  18. 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:

  19. 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.

  20. The rest of the code is taken from Example 5.5 of the text. 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:

  21. Input to the program – Data Segment • .data • array: .word 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13 • count: .word 15 • endl: .asciiz "\n" • ans2: .asciiz "\nmax = "

  22. Output the result Input array: 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13

  23. 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

  24. Output read from Console

  25. 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

  26. 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

  27. 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.

  28. The program Works fine for n = 12 and computes n! correctly. But for n = 13, we get …

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

  30. 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 lo mfhi $t1 # move the high 32 bits into $t1 mflo $t2 # move the low 32 bits into $t2

  31. 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 …

  32. Computation of factorial for larger n’s 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

  33. Now we get the following result: Can you explain the result? How do we fix this?

More Related