1 / 43

Lecture 6 Sept 16 Chapter 2 continue MIPS

Lecture 6 Sept 16 Chapter 2 continue MIPS translating c into MIPS – examples MIPS simulator (MARS and SPIM). Logical Operations. Instructions for bitwise manipulation. §2.6 Logical Operations. Useful for extracting and inserting groups of bits in a word.

Download Presentation

Lecture 6 Sept 16 Chapter 2 continue MIPS

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. Lecture 6 Sept 16 • Chapter 2 • continue MIPS • translating c into MIPS – examples • MIPS simulator (MARS and SPIM)

  2. Logical Operations • Instructions for bitwise manipulation §2.6 Logical Operations • Useful for extracting and inserting groups of bits in a word

  3. op rs rt rd shamt funct 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits Shift Operations • shamt: how many positions to shift • Shift left logical • Shift left and fill with 0 bits • sll by i bits multiplies by 2i • Shift right logical (arithmetic) • Shift right and fill with 0 bits (sign bit) • srl, sra by i bits divides by 2i

  4. AND Operations • Useful to mask bits in a word • Select some bits, clear others to 0 and $t0, $t1, $t2 $t2 0000 0000 0000 0000 0000 1101 1100 0000 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0000 1100 0000 0000

  5. OR, XOR Operations • Useful to include bits in a word • Set some bits to 1, leave others unchanged or $t0, $t1, $t2 $t2 0000 0000 0000 0000 0000 1101 1100 0000 $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 0000 0000 0000 0000 0011 1101 1100 0000 • MIPS also has a bitwise exclusive-or instruction • xor $t0, $t1, $t2

  6. NOT Operations • Useful to invert bits in a word • change 0 to 1, and 1 to 0 • MIPS has 3-operand NOR instruction • a NOR b == NOT ( a OR b ) nor $t0, $t1, $zero Register 0: always read as zero $t1 0000 0000 0000 0000 0011 1100 0000 0000 $t0 1111 1111 1111 1111 1100 0011 1111 1111

  7. Initializing a Register Show how each of these bit patterns can be loaded into $s0: 0010 0001 0001 0000 0000 0000 0011 1101 1111 1111 1111 1111 1111 1111 1111 1111 We can use a MIPS instruction load upper immediate. This loads the given 16 bit number as the upper 16 bits of a register, rest filled with 0. lui R, 0x ab98 # copy ab980000 to R.

  8. Initializing a Register Show how each of these bit patterns can be loaded into $s0: 0010 0001 0001 0000 0000 0000 0011 1101 1111 1111 1111 1111 1111 1111 1111 1111 Solution The first bit pattern has the hex representation: 0x2110003d lui $s0,0x2110 # put the upper half in $s0 ori $s0,$s0,0x003d # put the lower half in $s0 Same can be done, with immediate values changed to 0xffff for the second bit pattern. But, the following is simpler and faster: nor $s0,$zero,$zero

  9. Loading data via memory • It is possible to load a value into register by writing it into memory and reading it from there usinglw. • This is done in two steps in MIPS: • Load the memory address using la. • Then use lw to read the content.

  10. Conditional Operations • Branch to a labeled instruction if a condition is true • Otherwise, continue sequentially beq rs, rt, L1 • if (rs == rt) branch to instruction labeled L1; bne rs, rt, L1 • if (rs != rt) branch to instruction labeled L1; j L1 • unconditional jump to instruction labeled L1

  11. Exercise: Write a sequence of MIPS 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:

  12. compiling if statements • C code: if (i==j) f = g+h;else f = g-h; • f, g, … in $s0, $s1, … • Compiled MIPS code: bne $s3, $s4, Else add $s0, $s1, $s2 j ExitElse: sub $s0, $s1, $s2Exit: … Assembler calculates addresses

  13. More Conditional Operations Set result to 1 if a condition is true • Otherwise, set to 0 slt rd, rs, rt • if (rs < rt) rd = 1; else rd = 0; slti rt, rs, constant • if (rs < constant) rt = 1; else rt = 0; Use in combination with beq, bne slt $t0, $s1, $s2 # if ($s1 < $s2) bne $t0, $zero, L # branch to L

  14. Branch Instruction Design Why not blt, bge, etc? Hardware for <, ≥, … slower than =, ≠ Combining with branch involves more work per instruction, requiring a slower clock beq and bne are the common case This is a good design compromise

  15. op rs rt constant or address 6 bits 5 bits 5 bits 16 bits Branch Addressing • Branch instructions specify • Opcode, two registers, target address • Most branch targets are near branch • Forward or backward • PC-relative addressing • Target address = PC + offset × 4 • PC already incremented by 4 by this time

  16. Signed vs. Unsigned • Signed comparison: slt, slti • Unsigned comparison: sltu, sltui • Example $s0 = 1111 1111 1111 1111 1111 1111 1111 1111 $s1 = 0000 0000 0000 0000 0000 0000 0000 0001 • slt $t0, $s0, $s1 # signed • –1 < +1  $t0 = 1 • sltu $t0, $s0, $s1 # unsigned • +4,294,967,295 > +1  $t0 = 0

  17. Jump Instruction Unconditional jump instruction j verify # go to mem loc named “verify”

  18. op address 26 bits 6 bits Jump Addressing • Jump (j and jal) targets could be anywhere in text segment • Encode full address in instruction • (Pseudo)Direct jump addressing • Target address = PC31…28 : (address × 4)

  19. Examples for Conditional Branching If the branch target is too far to be reachable with a 16-bit offset (rare occurrence), the assembler replaces the branch instructionbeq $s0,$s1,L1with: bne $s1,$s2,L2 # skip jump if (s1)(s2) j L1 # goto L1 if (s1)=(s2) L2: ... Forming if-then constructs; e.g.,if (i == j) x = x + y bne $s1,$s2,endif # branch on ij add $t1,$t1,$t2 # execute the “then” part endif: ... If the condition were (i < j), we would change the first line to: slt $t0,$s1,$s2 # set $t0 to 1 if i<j beq $t0,$0,endif # branch if ($t0)=0; # i.e., i not< j or ij

  20. Compiling if-then-else Statements Show a sequence of MIPS instructions corresponding to: if (i<=j) x = x+1; z = 1; else y = y–1; z = 2*z Solution Similar to the “if-then” statement, but we need instructions for the “else” part and a way of skipping the “else” part after the “then” part. slt $t0,$s2,$s1 # j<i? (inverse condition) bne $t0,$zero,else # if j<i goto else part addi $t1,$t1,1 # begin then part: x = x+1 addi $t3,$zero,1 # z = 1 j endif # skip the else part else: addi $t2,$t2,-1 # begin else part: y = y–1 add $t3,$t3,$t3 # z = z+z endif:...

  21. compiling loop statements • c code: while (save[i] == k) i += 1; • i in $s3, k in $s5, base address of save in $s6 • compiled MIPS code: Loop: sll $t1, $s3, 2 add $t1, $t1, $s6 lw $t0, 0($t1) bne $t0, $s5, Exit addi $s3, $s3, 1 j LoopExit: …

  22. MIPS Instructions Covered So Far op 15 0 0 0 8 10 0 0 0 0 12 13 14 35 43 2 0 1 4 5 fn 32 34 42 36 37 38 39 8 Copy Arithmetic Logic Memory access Control transfer

  23. Finding the Maximum in an array Array A is stored in memory beginning at the address given in $s1. Array length is given in $s2. Find the largest integer in the list and copy it into $t0. Solution Scan the list, holding the largest element identified thus far in $t0.

  24. Finding the Maximum in an array lw $t0,0($s1) # initialize maximum to A[0] 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 sll $t2,$t2,2 # 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: # continuation of the program

  25. SPIM – A simulator for MIPS Assembly language • Home page of SPIM: • http://www.cs.wisc.edu/~larus/spim.html • A useful document: • http://www.cs.wisc.edu/HP_AppA.pdf

  26. SPIM simulator windows

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

  28. SPIM simulator – how to run? We will implement the code written earlier (finding the max element in an array) using the SPIM simulator. Code is shown below:

  29. Creating the code and the text segments in SPIM • .text • .globl __start • __start: • la $s1, array # initialize array • 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 array into registers $s1 and $t6.

  30. We use the code for finding the maximum 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:

  31. 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 = "

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

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

  34. Output read from simulators SPIM

  35. Output read from simulators MARS

  36. Procedure Calling Steps required • Place parameters (arguments) in registers • Transfer control to procedure • Acquire storage for procedure • Perform procedure’s operations • Place result in register for caller • Return to place of call §2.8 Supporting Procedures in Computer Hardware

  37. Register Usage • $a0 – $a3: arguments (reg’s 4 – 7) • $v0, $v1: result values (reg’s 2 and 3) • $t0 – $t9: temporaries • Can be overwritten by callee • $s0 – $s7: saved • Must be saved/restored by callee • $gp: global pointer for static data (reg 28) • $sp: stack pointer (reg 29) • $fp: frame pointer (reg 30) • $ra: return address (reg 31)

  38. Procedure Call Instructions • Procedure call: jump and link jal ProcedureLabel • Address of following instruction put in $ra • Jumps to target address • Procedure return: jump register jr $ra • Copies $ra to program counter • Can also be used for computed jumps • e.g., for case/switch statements

  39. Leaf Procedure Example • c code: int leaf_example (int g, h, i, j){ int f; f = (g + h) - (i + j); return f;} • Arguments g, …, j in $a0, …, $a3 • f in $s0 (hence, need to save $s0 on stack) • Result in $v0

  40. Leaf Procedure Example • MIPS code: leaf_example: addi $sp, $sp, -4 sw $s0, 0($sp) add $t0, $a0, $a1 add $t1, $a2, $a3 sub $s0, $t0, $t1 add $v0, $s0, $zero lw $s0, 0($sp) addi $sp, $sp, 4 jr $ra Save $s0 on stack Procedure body Result Restore $s0 Return

  41. Non-Leaf Procedures • Procedures that call other procedures are known as non-leaf procedures. • For nested call, caller needs to save on the stack: • Its return address • Any arguments and temporaries needed after the call • Restore from the stack after the call

  42. Non-Leaf Procedure Example • C code: int fact (int n){ if (n < 1) return f; else return n * fact(n-1);} • Argument n in $a0 • Result in $v0

  43. Non-Leaf Procedure Example • MIPS code: fact: addi $sp, $sp, -8 # adjust stack for 2 items sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save argument slti $t0, $a0, 1 # test for n < 1 beq $t0, $zero, L1 addi $v0, $zero, 1 # if so, result is 1 addi $sp, $sp, 8 # pop 2 items from stack jr $ra # and returnL1: addi $a0, $a0, -1 # else decrement n jal fact # recursive call lw $a0, 0($sp) # restore original n lw $ra, 4($sp) # and return address addi $sp, $sp, 8 # pop 2 items from stack mul $v0, $a0, $v0 # multiply to get result jr $ra # and return

More Related