MIPS Intro / Decisions: CS270 Unit 3 Max Luttrell, Spring 2017
MIPS Intro / Decisions: CS270 Unit 3 Max Luttrell, Spring 2017
MIPS Intro / Decisions: CS270 Unit 3 Max Luttrell, Spring 2017
CS270 Unit 3
Max Luttrell, Spring 2017
MIPS
• Microprocessor without Interlocking Pipe Stages
# a = b + c + d + e
add a, b, c # put b+c in a
add a, a, d # put b+c+d in a
add a, a, e # put b+c+d+e in a
example
• let's say we have a,b,c,d,e "variables" declared
in a C program. What is the equivalent
assembly for this?
a = b + c;
d = a - e;
f = (g+h) - (i+j);
register
register type # registers
name
$s0, $s1,
variable 8 ..., $s7
$t0, $t1,
temporary 10 ..., $t9
argument 4 $a0-$a3
zero 1 $zero
...
register numbers
arithmetic with registers
• let's say we have f,g,h,i,j variables declared in a
C program, assigned to registers $s0, $s1, $s2,
$s3, and $s4 respectively. What is the
equivalent MIPS assembly for this?
f = (g+h) - (i+j);
g = h + A[6];
offset base
memory example 2
• let's say we have h in $s2, and base address of
array A in $s3. For this C code, what is the MIPS
code?
A[12] = h + A[6];
# add 4 to $s3
addi $s3, $s3, 4
MIPS instruction -
instruction lw $t0, X
operation and operands
# load $t0 with
comment descriptive text - follows # # our data at X
reference to data or
label Result:
instruction - followed by :
assembler special instructions to .text
directive assembler - begin with .
MIPS assembler directives
name description
.text
__start:
lw $t0, x
MIPS instruction formats
• MIPS has three types of instruction formats:
field meaning
0 17 18 8 0 32
00000010001100100100000000100000binary =
0x02324020
MIPS I-type instructions
field op rs rt constant or address
# bits 6 5 5 16
field meaning
00100000
64decimal 8decimal
logical vs. arithmetic
bit shifting
• previous slide was logical bit shifting. this works
for unsigned values:
1100 logical shift right 1 bit 0110
12dec 6dec
• let's say we right shift a signed value -- doesn't
work!
1100 logical shift right 1 bit 0110
-4dec 6dec
• for signed values we need to use arithmetic bit
shifting (preserve and shift over sign bit):
1100 arithmetic shift right 1 bit 1110
-4dec -2dec
shift operations
field op rs rt rd shamt funct
# bits 6 5 5 5 5 6
• MIPS branches
• j L1
• go to instruction labeled L1
decision example
• let's say we have the f,g,h,i,j variables assigned to
registers $s0 through $s4 for the C code below. What
is the equivalent assembly for this?
if (i==j)
f = g + h;
else
f = g - h;
…
bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit: …
loops
• let's say we have the i and k variables in $s3 and
$s5, and the base of array save is in $s6. What
is the equivalent assembly for this?
while (save[i] == k)
i += 1;
pseudocode:
1. put address of save[i] in $t1
- note: it is i*4 + s6
2. load save[i] to $t0
3. if save[i]!=k, jump out of loop
4. increment i
5. jump back to step 1
loops
i and k variables in $s3 and $s5, base of save in $s6.
pseudocode:
1. get the address of save[i]
- note: it is i*4 + s6
2. load save[i] to $t0
3. if save[i]!=k, jump out of loop
4. increment i
5. jump back to step 1
Loop:
sll $t1, $s3, 2 # $t1 = i*4 (shift left 2 is mult by 4)
add $t1, $t1, $s6 # $t1 = address of save[i]
lw $t0, 0($t1) # $t0 = save[i]
bne $t0, $s5, Exit # branch out of loop if save[i]!=k
addi $s3, $s3, 1 # i++
j Loop # go back to top of loop
Exit:
slt - set on less than
• equality testing is useful, but so is testing if a
variable is less than another
• is equal ==
• not equal !=
$s0
lui $s0, 61 0000 0000 0011 1101 0000 0000 0000 0000
ori $s0,$s0,2304 0000 0000 0011 1101 0000 1001 0000 0000
• the file first.s on hills in exercises/intromips is your starting point for this
exercise. you can copy it to your current directory using scp (replace login with
your hills username)
• scp login@hills.ccsf.edu:/pub/cs/mluttrel/cs270/exercises/intromips/first.s .
result = a1+b1+c1
• check out the registers on the right, and the data segment. can you tell where
the data from first.s is stored in this data window?
• now you can use the button to step through your instructions one
instruction at a time. to start over, hit the button. make sure you see that
result gets stored with the correct answer!
Exercise 3B
Exercise 3B
Exercise 3C
• Write a MIPS program which implements the following C code:
result=0;
for (i=Nelems-1; i!=0; i--) {
if (arr[i] < 0) { arr[i] = -arr[i]; result++; }
}
• You can get a starting file from hills by using the following scp
command (all one line; replace uname with your username):
scp uname@hills.ccsf.edu:/pub/cs/mluttrel/cs270/exercises/
decisions/decisions.s .
• Note: I have placed ugly C code with goto's and labels which is
equivalent to the above C code in this file. It should be easier for
you to translate this ugly C code to MIPS assembly than the
more natural C code above.
• Make sure to store result in register $t7, and the starting code
will output it for you