lecture 6 sept 16 chapter 2 continue mips
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Lecture 6 Sept 16
• Chapter 2
• continue MIPS
• translating c into MIPS – examples
• MIPS simulator (MARS and SPIM)
Logical Operations
Instructions for bitwise manipulation
Operation C Java MIPS
Shift left << << sll
Shift right >> >>>, >>
Srl, sra
Bitwise AND & & and, andi
Bitwise OR | | or, ori
Bitwise NOT ~ ~ nor Useful for extracting and inserting groups
of bits in a word
§2.6
Logica
l Op
era
tions
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
op rs rt rd shamt funct
6 bits 6 bits5 bits 5 bits 5 bits 5 bits
AND Operations
Useful to mask bits in a word Select some bits, clear others to 0
and $t0, $t1, $t2
0000 0000 0000 0000 0000 1101 1100 0000
0000 0000 0000 0000 0011 1100 0000 0000
$t2
$t1
0000 0000 0000 0000 0000 1100 0000 0000$t0
OR, XOR Operations Useful to include bits in a word
Set some bits to 1, leave others unchanged
or $t0, $t1, $t2
0000 0000 0000 0000 0000 1101 1100 0000
0000 0000 0000 0000 0011 1100 0000 0000
$t2
$t1
0000 0000 0000 0000 0011 1101 1100 0000$t0
• MIPS also has a bitwise exclusive-or instruction xor $t0, $t1, $t2
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
0000 0000 0000 0000 0011 1100 0000 0000$t1
1111 1111 1111 1111 1100 0011 1111 1111$t0
Register 0: always read as zero
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.
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 1111Solution
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
Loading data via memory
It is possible to load a value into register by writing it into memory and reading it from there using lw.
This is done in two steps in MIPS:
1) Load the memory address using la.
2) Then use lw to read the content.
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
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:
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
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, bneslt $t0, $s1, $s2 # if ($s1 < $s2)bne $t0, $zero, L # branch to L
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 caseThis is a good design compromise
Branch Addressing
Branch instructions specify Opcode, two registers, target address
Most branch targets are near branch Forward or backward
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
PC-relative addressing Target address = PC + offset × 4 PC already incremented by 4 by this time
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
Jump InstructionUnconditional jump instruction
j verify # go to mem loc named “verify”
Jump Addressing
Jump (j and jal) targets could be anywhere in text segment Encode full address in instruction
op address
6 bits 26 bits
(Pseudo)Direct jump addressing Target address = PC31…28 : (address ×
4)
Examples for Conditional BranchingIf the branch target is too far to be reachable with a 16-bit offset (rare occurrence), the assembler replaces the branch instruction beq $s0,$s1,L1 with:
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 ij add $t1,$t1,$t2 # execute the “then” partendif: ...
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 ij
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:...
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: …
MIPS Instructions Covered So Far
Instruction UsageLoad upper immediate lui rt,imm
Add add rd,rs,rt
Subtract sub rd,rs,rt
Set less than slt rd,rs,rt
Add immediate addi rt,rs,imm
Set less than immediate
slti rd,rs,imm
AND and rd,rs,rt
OR or rd,rs,rt
XOR xor rd,rs,rt
NOR nor rd,rs,rt
AND immediate andi rt,rs,imm
OR immediate ori rt,rs,imm
XOR immediate xori rt,rs,imm
Load word lw rt,imm(rs)
Store word sw rt,imm(rs)
Jump j L
Branch less than 0 bltz rs,L
Branch equal beq rs,rt,L
Branch not equal bne rs,rt,L
Copy
Control transfer
Logic
Arithmetic
Memory access
op150008
100000
121314354320145
fn
323442
36373839
8
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.
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 1beq $t1,$s2,done # if all elements examined,
quitsll $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 $t3slt $t4,$t0,$t3 # maximum < A[i]?beq $t4,$zero,loop # if not, repeat with
no changeaddi $t0,$t3,0 # if so, A[i] is the new
maximum j loop # change completed; now repeat
done: # continuation of the program
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
SPIM simulator windows
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.
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:
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.
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:
We use the code for finding the maximum
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 = "
Output the result
Input array: 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13
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
Output read from simulators
SPIM
Output read from simulators
MARS
Procedure Calling
Steps required1. Place parameters (arguments) in
registers2. Transfer control to procedure3. Acquire storage for procedure4. Perform procedure’s operations5. Place result in register for caller6. Return to place of call
§2.8
Su
pportin
g P
roce
dure
s in C
om
pute
r Hard
ware
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)
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
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
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
Restore $s0
Result
Return
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
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
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