Transcript ppt

MIPS Assembly
Review
•
A computer has processor, memory, and IO devices.
–
–
•
The processor stores values in registers, and modifies values by
sending them to the ALU.
Memory is where the computer stores a large number of
values. Every byte can be accessed by specifying a unique
address. The address goes from 0 to 0xffffffff in MIPS. In MIPS
we mainly work with word which is 4 bytes.
MIPS instructions learned:
–
–
add, sub.
lw, sw.
Constant or Immediate Operands
Many times we use a constant in an operation


For example, i++, i--, i += 4, and so on
Since constant operands occur frequently, we should
include constants inside arithmetic operations so that
they are much faster



MIPS has an add instruction that allows one operand to be a
constant
The constant must be in 16 bits, as a signed integer in 2’s
complement format
addi $s1, $s2, 100
3
# $s1 = $s2 + 100
CDA3100
4/10/2016
Logical Operations
Often we need to operate on bit fields within a word.


4
Which allow us to pack and unpack bits into words and
perform logical operations such as logical and, logical or, and
logical negation
CDA3100
4/10/2016
Bit-wise AND

Apply AND bit by bit

The resulting bit is 1 if both of the input bits are 1 and zero
otherwise


There is also a version of AND with an immediate


5
and $t2, $t0, $t1
andi $t2, $t1, 12
The immediate is treated as an unsigned 16-bit number
CDA3100
4/10/2016
Bit-wise OR

Apply OR bit by bit

The resulting bit is 1 if at least one of the input bits is 1 and
zero otherwise


There is also a version of OR with an immediate


6
or $t2, $t0, $t1
ori $t2, $t1, 12
The immediate is treated as an unsigned 16-bit number
CDA3100
4/10/2016
Bit-wise XOR

Apply XOR bit by bit

The resulting bit is 1 if two bits are different


There is also a version of OR with an immediate


7
xor $t2, $t0, $t1
xori $t2, $t1, 12
The immediate is treated as an unsigned 16-bit number
CDA3100
4/10/2016
NOR

Since NOT takes one operand and results in one
operand, it is not included in MIPS as an instruction


Because in MIPS each arithmetic operation takes exactly three
operands
Instead, NOR is included



How to implement NOT using NOR?


8
The resulting bit is 0 if at least one of the input bits is 1
nor $t2, $t0, $t1
Using $zero as one of the input operands
It is included in MIPS as a pseudoinstruction
CDA3100
4/10/2016
Exercise 1

After learnt these instructions, how can we load an
integer value (like 100) into a register ($t0)?
Exercise 1

After learnt these instructions, how can we load an
integer value (like 100) into a register ($t0)?



addi $t0, $zero, 100
ori $t0, $zero, 80
Which should we prefer?

ori. Because it is simpler than add. Simpler means less time, less power
consumption.
Shifts

Shift instructions move all the bits in a word to the left
or to the right

Shift left logical (sll) move all the bits to the left by the specified
number of bits


Shift right logical (srl) move all the bits to the right


11
sll $t2, $t0, 2
srl $t2, $t0, 2
Filling the emptied bits with 0’s
CDA3100
4/10/2016
Example
Suppose register $s0 ($16) is 9ten

31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1

12
What do we have in $t2 ($10) after
CDA3100
4/10/2016
Example
Suppose register $s0 ($16) is 9ten

31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1

31
30
We have in $t2 ($10) after
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0


13
The value is 144ten = 9ten  24
In general, shifting left by i bits gives the same result as
multiplying by 2i
CDA3100
4/10/2016
Example
Suppose register $s0 ($16) is 9ten

31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1

31
30
We have in $t2 ($10) after
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


14
The value is NOT 9ten  228
Note that overflow happens this time (for signed numbers)
CDA3100
4/10/2016
Instructions for Making Decisions

A distinctive feature of programs is that they can make
different decisions based on the input data
15
CDA3100
4/10/2016
Instruction beq (branch if equal)

To support decision making, MIPS has two conditional
branch instructions, similar to an “if” statement with
a goto

In C, it is equivalent to

Note that L1 is a label and we are comparing values in
register1 and register2
Label is an address of an instruction

16
CDA3100
4/10/2016
Instruction bne

Similarly, bne (branch not equal) means go to the
statement labeled with L1 if the value in register1
does not equal to the value in regster2

17
Equivalent to
CDA3100
4/10/2016
Instruction j (jump)

MIPS has also an unconditional branch, equivalent to goto
in C

18
Jump to the instruction labeled with L1
CDA3100
4/10/2016
Compiling if-then-else

Suppose variables f, g, h, i, and j are in registers $s0
through $s4, how to implement the following in MIPS?
19
CDA3100
4/10/2016
Compiling if-then-else

Suppose variables f, g, h, i, and j are in registers $s0
through $s4, how to implement the following in MIPS?
20
CDA3100
4/10/2016
Compiling if-then-else

Suppose variables f, g, h, i, and j are in registers $s0
through $s4, how to implement the following in MIPS?
21
CDA3100
4/10/2016
MIPS Assembly for if-then-else

Now it is straightforward to translate the C
program into MIPS assembly
22
CDA3100
4/10/2016