MIPS Instruction Set Architecture PDF
MIPS Instruction Set Architecture PDF
Spring 2011
2
Assembly Language
]
V;K= V;K=
V;K= TEMP (source code)
#OMPILER
!SSEMBLY SWAP
LANGUAGE MULI
(assembly code)
PROGRAM ADD
FOR -)03 LW
LW
SW
SW
JR
!SSEMBLER
(machine code)
LANGUAGE
PROGRAM
FOR -)03
1,
Ê£°ÎÊÊÊÊ
Ê«À}À>ÊV«i`ÊÌÊ>ÃÃiLÞÊ>}Õ>}iÊ>`ÊÌ
iÊ>ÃÃiLi`ÊÌÊL>ÀÞÊ
>V
iÊ >}Õ>}i°Ê !LTHOUGH THE TRANSLATION FROM HIGH
LEVEL LANGUAGE TO BINARY MACHINE LANGUAGE IS
SHOWN IN TWO STEPS SOME COMPILERS CUT OUT THE MIDDLEMAN AND PRODUCE BINARY MACHINE LANGUAGE DIRECTLY
4HESE LANGUAGES AND THIS PROGRAM ARE EXAMINED IN MORE DETAIL IN #HAPTER #OPYRIGHT Ú %LSEVIER )NC
What is an ISA?
• An Instruction Set Architecture, or ISA, is an interface between the hardware
and the software.
5
Why have an ISA?
• An ISA provides binary compatibility across machines that share the ISA
• Any machine that implements the ISA X can execute a program encoded
using ISA X.
• You typically see families of machines, all with the same ISA, but with different
power, performance and cost characteristics.
6
MIPS Architecture
• Smaller is faster
9
An example Program in MIPS: Factorial(n)
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C 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 return
L1: 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
MIPS code
10
An Program in MIPS
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C code! fact:
addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
Instructions slti
beq
$t0,
$t0,
$a0, 1
$zero, L1
# test for n < 1
11
An Program in MIPS
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C 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
12
An Program in MIPS
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C 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
13
An Program in MIPS
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C code! fact:
addi $sp, $sp, -8 # adjust stack for 2 items
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
Access to
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, L1
14
An Program in MIPS
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C code! fact:
addi $sp, $sp, -8 # adjust stack for 2 items
Control Labels
sw $ra, 4($sp) # save return address
sw $a0, 0($sp) # save argument
and “Jump”
slti $t0, $a0, 1 # test for n < 1
beq $t0, $zero, L1
15
Instruction Classes
• Memory Access: Move data to/from memory from/to registers
• Conditional branch: test values in registers. If test returns true, move control
to different part of program. Otherwise, proceed to next word
16
Arithmetic Instructions
• Addition and subtraction
• add a, b, c # a gets b + c
Design principle:
17
Arithmetic Example 1
18
Arithmetic Example 1 w. Registers
19
Memory Operands
• Main memory used for composite data (e.g., arrays, structures, dynamic data)
• Load values from memory into registers (load instruction = mem read)
• Words (32-bits) are aligned in memory (meaning each address must be a multiple
of 4)
• MIPS is big-endian (i.e., most significant byte stored at least address of the word)
20
Memory Operands
• Main memory used for composite data (e.g., arrays, structures, dynamic data)
• Load values from memory into registers (load instruction = mem read)
• Words (32-bits) are aligned in memory (meaning each address must be a multiple
of 4)
• MIPS is big-endian (i.e., most significant byte stored at least address of the word)
21
Memory Operand Example 1
g = h + A[8]
C code
22
Memory Operand Example 2
A[12] = h + A[8]
C code
23
Registers v. Memory
• Registers are faster to access than memory
24
Immediate Operands
• Constant data encoded in an instruction
25
The Constant Zero
• MIPS register 0 ($zero) is the constant 0
• Useful for many operations, for example, a move between two registers
26
Register Numbers
0RESERVED ON
.AME 2EGISTER NUMBER 5SAGE CALL
ZERO 4HE CONSTANT VALUE NA
VnV n 6ALUES FOR RESULTS AND EXPRESSION EVALUATION NO
AnA n !RGUMENTS NO
TnT n 4EMPORARIES NO
SnS n 3AVED YES
TnT n -ORE TEMPORARIES NO
GP 'LOBAL POINTER YES
SP 3TACK POINTER YES
FP &RAME POINTER YES
RA 2ETURN ADDRESS YES
&)'52% -)03 REGISTER CONVENTIONS 2EGISTER CALLED AT IS RESERVED FOR THE ASSEMBLER SEE
3ECTION AND REGISTERS
CALLED K
K ARE RESERVED FOR THE OPERATING SYSTEM 4HIS INFORMATION
Note: Register 1 ($at) is reserved for the assembler, and
IS ALSO FOUND IN #OLUMN OF THE -)03 2EFERENCE $ATA #ARD AT THE FRONT OF THIS BOOK #OPYRIGHT Ú
26-27
%LSEVIER )NC !LL ($k0-$k1) are reserved for the OS.
RIGHTS RESERVED
27
MIPS instructions to date
1,
ÊÓ°xÊ *-ÊÃÌÀÕVÌÊiV`}°Ê)N THE TABLE ABOVE hREGv MEANS A REGISTER NUMBER BETWEEN
AND hADDRESSv MEANS A
BIT ADDRESS AND hNAv NOT APPLICABLE MEANS THIS lELD DOES NOT APPEAR IN THIS
FORMAT .OTENB:
THAT ADDreg = register
AND SUB INSTRUCTIONSnumber
HAVE THE SAMEbetween 0 and
VALUE IN THE OP 31;
lELD THE HARDWARE USES THE FUNCT
lELD TO DECIDE THE VARIANT OF THE OPERATION ADD OR SUBTRACT #OPYRIGHT Ú %LSEVIER )NC !LL RIGHTS
RESERVED
address = 16-bit address
28
MIPS R-format Instructions
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
• Instruction fields
• op: operation code (opcode)
• rs: first source register number
• rt: second source register number
• rd: register destination number
• shamt: shift amount (00000 for now)
• funct: function code (extends opcode)
29
R-format Example
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
0 17 18 8 0 32
30
MIPS I-format Instructions
op rs rt constant
6 bits 5 bits 5 bits 16 bits
31
MIPS Logical Operations
• Instructions for bitwise manipulation
}V>Ê«iÀ>ÌÃ
Ê«iÀ>ÌÀà >Û>Ê«iÀ>ÌÀà *-ÊÃÌÀÕVÌÃ
Ê -
vÌÊivÌ SLL
Ê -
vÌÊÀ}
Ì SRL
Ê ÌLÞLÌÊ ANDÊANDI
Ê ÌLÞLÌÊ", \ \ ORÊORI
Ê ÌLÞLÌÊ "/ ^ ^ NOR
1,
ÊÓ°nÊ
Ê>`Ê>Û>Ê}V>Ê«iÀ>ÌÀÃÊ>`ÊÌ
iÀÊVÀÀië`}Ê*-ÊÃÌÀÕVÌðÊ-)03
• Useful
IMPLEMENTSfor./4
inserting andWITH
USING A ./2 extracting
ONE OPERANDgroups
BEING ZEROof#OPYRIGHT
bits inÚa
word%LSEVIER )NC !LL RIGHTS
RESERVED
•
32
Shift Operations
• Shift left logical (op = sll)
• R-format
0 0 16 10 4 0
33
Full Complement of Shift Instructions
• sll: shift left logical (sll $t0, $t1, 5 # $t0 <= $t1 << 5)
• srl: shift right logical (srl $t0, $t1, 5 # $t0 <= $t1 >> 5)
• sra: shift right arithmetic (sra $t0, $t1, 5 # $t0 <= $t1 >>> 5)
34
Generating Constants
• 16-bit constants using addi:
*lui loads the 16-bit immediate into the upper half of the register and sets
the lower half to 0.
35
AND Operations
• Useful for masking bits in a word (selecting some bits, clearing others to 0)
36
OR Operations
• Useful to include bits in a word (set some bits to 1, leaving others unchanged)
37
NOT Operations
38
Conditional Operations
• Branch to a labeled instruction if a condition is true
• Instruction labeled with colon e.g. L1: add $t0, $t1, $t2
39
Compiling an If Statement
40
Compiling a Loop Statement
Loop:
sll $t1, $s3, 2
while (save[i] == k) add $t1, $t1, $s5
i += 1 lw $t0, 0($t1)
bne $t0, $s4, Exit
C code
addi $s3, $s3, 1
j Loop
Exit:
MIPS assembly
41
Basic Blocks
• A basic block is a sequence of instructions with
42
More Conditional Operations
• Set result to 1 if a condition is true
43
Branch Instruction Design
• Why not blt, bge, etc.?
• As beq and bne are the common case, this is a good compromise
44
Signed v. Unsigned
• Signed comparison: slt, slti
• Example:
45
Procedure Calling
• Steps required:
“caller”
1
2. Transfer control to procedure
2
3. Acquire storage for procedure “callee”
46
Register Usage
• $a0-$a3: arguments
47
Memory Layout
• Text: program code
$YNAMIC DATA
• $gp initialized to an address allowing +/-
offsets in this segment GP HEX 3TATIC DATA
HEX
4EXT
• Dynamic data: heap PC HEX
2ESERVED
• e.g., malloc in C, new in Java
1,
Ê Ó°£ÎÊ /
iÊ *-Ê iÀÞÊ >V>ÌÊ vÀÊ «À}À>Ê >`Ê `>Ì>°Ê
ONLY A SOFTWARE CONVENTION AND NOT PART OF THE -)03 ARCHITECTUREÊ 4HE STACK PO
FFF FFFCHEX AND GROWS DOWN TOWARD THE DATA SEGMENT !T THE OTHER END THE PROGRA
• Stack: automatic storage
AT HEX 4HE STATIC DATA STARTS AT HEX $YNAMIC DATA ALLOCATED
BY NEW IN *AVA IS NEXT )T GROWS UP TOWARD THE STACK IN AN AREA CALLED THE HEAP 4HE G
SET TO AN ADDRESS TO MAKE IT EASY TO ACCESS DATA )T IS INITIALIZED TO HEX SO TH
HEX TO FFFFHEX USING THE POSITIVE AND NEGATIVE
BIT OFFSETS FROM 48
Local Data on the Stack
• Local data allocated by the callee
FP FP
SP SP
FP 3AVED ARGUMENT
REGISTERS IF ANY
3AVED RETURN ADDRESS
3AVED SAVED
REGISTERS IF ANY
,OW ADDRESS
A B C
• jal ProcedureLabel
• jr $ra
• can also be used for computed jumps (e.g., for case/switch statements)
50
Leaf Procedure Example
• f will go in $s0 (so will have to save existing contents of $s0 to stack)
• result in $v0
51
Leaf Procedure Example 2
int leaf_example(int g,h,i,j) {
int f;
f = (g+h) - (i+j);
return f;
}
C code
leaf_example:
addi $sp, $sp, -4 save $s0 on stack
sw $s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a2 procedure body
sub $s0, $t0, $t1
add $v0, $s0, $zero result
lw $s0, 0($sp)
restore $s0
addi $sp, $sp, 4
jr $ra return
MIPS assembly
52
Non-Leaf Procedures
• A non-leaf procedure is a procedure that calls another procedure
• After the call, the caller must restore these values from the stack
53
Non-Leaf Procedure Example
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C code
54
Non-Leaf Procedure Example 2
int fact(int n) {
if (n < 1) return 1;
else return (n * fact(n - 1));
}
C 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 return
L1: 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
MIPS assembly
55
Character Data
• Byte-encoded character sets
56
Byte/Halfword Operations
• Could use bitwise operations
57
String Copy Example
void strcpy (char x[], char y[]) {
int i;
i = 0;
while ((x[i]=y[i]) != ‘\0’)
i += 1;
}
C code (naive)
• Null-terminated string
• i in $s0
58
String Copy Example 2
void strcpy (char x[], char y[]) {
int i;
i = 0;
while ((x[i]=y[i]) != ‘\0’)
i += 1;
}
C code (naive)
! strcpy :
addi $sp, $sp, -4 # adjust stack for 1 item
sw $s0, 0($sp) # save $s0
add $s0, $zero, $zero # i = 0
L1: add $t1, $s0, $a1 # addr of y[i] in $t1
lbu $t2, 0($t1) # $t2 = y[i]
add $t3, $s0, $a0 # addr of x[i] in $t3
sb $t2, 0($t3) # x[i] = y[i]
beq $t2, $zero, L2 # exit loop if y[i] == 0
addi $s0, $s0, 1 # i = i + 1
j L1 # next iteration of loop
L2: lw $s0, 0($sp) # restore saved $s0
addi $sp, $sp, 4 # pop 1 item from stack
jr $ra # and return
MIPS assembly
59
32-bit constants
• Most constants are small, 16 bits usually sufficient
• example usage:
lui $s0, 61 $s0: 0000 0000 0111 1101 0000 0000 0000 0000
ori $s0, $s0, 2304 $s0: 0000 0000 0111 1101 0000 1001 0000 0000
60
Branch Addressing
• Branch instructions specify: opcode, two registers, branch target
op rs rt constant
6 bits 5 bits 5 bits 16 bits
• PC-relative addressing
61
Jump Addressing
• Jump (j and jal) targets could be anywhere in a text segment, so, encode the
full address in the instruction
op address
6 bits 26 bits
62
Target Addressing Example
• Loop code from earlier example
63
Addressing Mode Summary
)MMEDIATE ADDRESSING
OP RS RT )MMEDIATE
2EGISTER ADDRESSING
OP RS RT RD FUNCT 2EGISTERS
2EGISTER
"ASE ADDRESSING
OP RS RT !DDRESS -EMORY
0#
RELATIVE ADDRESSING
OP RS RT !DDRESS -EMORY
0# 7ORD
0SEUDODIRECT ADDRESSING
OP !DDRESS -EMORY
0# 7ORD
• Example:
65
Assembler Pseudoinstructions
• Most assembler instructions represent machine instructions, one to one.
becomes
move $t0,$t1 add $t0,$zero,$t1
66
Programming Pitfalls
• Sequential words are not at sequential addresses -- increment by 4 not by 1!
67
Interpreting Machine Language Code
• Start with opcode
• Opcode tells how to parse the remaining bits
• If opcode is all 0’s
• R-type instruction
• Function bits tell what instruction it is
• Otherwise, opcode tells what instruction it is
68
In conclusion: Fallacies
1. Powerful (complex) instructions lead to higher performance
• More lines of code (in assembly) means more errors and lower productivity
69
In conclusion: More Fallacies
3. Backwards compatibility means instruction set doesn’t change
.UMBER OF )NSTRUCTIONS
9EAR
1,
Ê Ó°{ÎÊ ÀÜÌ
Ê vÊ ÝnÈÊ ÃÌÀÕVÌÊ ÃiÌÊ ÛiÀÊ Ìi°Ê 7HILE THERE IS CLEAR TECHNICAL VALUE TO
SOME OF THESE EXTENSIONS THIS RAPID CHANGE ALSO INCREASES THE DIFlCULTY FOR OTHER COMPANIES TO TRY TO BUILD
COMPATIBLE PROCESSORS #OPYRIGHT Ú %LSEVIER )NC !LL RIGHTS RESERVED
70