Lecture 6 - 10 - On - MIPS - ISA
Lecture 6 - 10 - On - MIPS - ISA
Lecture 6 - 10 - On - MIPS - ISA
on
MIPS Instruction Set Architecture
We are going to learn
• Register Set
• Memory Organization
– Big Endian Vs Little Endian
• Instruction Formats
• Design Principles
Instructions: Overview
• Language of the machine
• More primitive than higher level languages, e.g., no sophisticated
control flow such as while or for loops
• Very restrictive
– e.g., MIPS arithmetic instructions
• We’ll be working with the MIPS instruction set architecture
– inspired most architectures developed since the 80's
– used by NEC, Nintendo, Silicon Graphics, Sony
– the name is not related to millions of instructions per second !
– it stands for microcomputer without interlocked pipeline
stages !
• Design goals: Maximize performance and minimize
cost and reduce design time
Types of Instructions
• Arithmetic Instructions
– Use only register operand
• Example:
C code: A = B + C
Control Input
Memory
Datapath Output
Processor I/O
Memory Organization
• Viewed as a large single-dimension array with access by address
• A memory address is an index into the memory array
• Byte addressing means that the index points to a byte of memory,
and that the unit of memory accessed by a load/store is a byte
0 8 bits of data
1 8 bits of data
2 8 bits of data
3 8 bits of data
4 8 bits of data
5 8 bits of data
6 8 bits of data
...
Memory Organization
• Bytes are load/store units, but most data items use larger words
• For MIPS, a word is 32 bits or 4 bytes.
0 32 bits of data
4 32 bits of data Registers correspondingly hold 32 bits of data
8 32 bits of data
12 32 bits of data
...
26/11/2024 12
So far we’ve learned:
• MIPS
– loading words but addressing bytes
– arithmetic on registers only
• Instruction Meaning
31 26 25 21 20 1615 11 10 6 5 0
rd
rt
add $4, $3, $2
rs
31 26 25 21 20 1615 11 10 6 5 0
00000000011000100010000000100000
opcode rs rt rd shamt funct
00000000011000100010000000100000
Encoding = 0x00622020
15
Machine Language
31 26 25 21 20 1615 0
rt
Immediate
lw $5, 3000($2)
rs
31 26 25 21 20 1615 0
10001100010001010000101110111000
opcode rs rt Immediate Value
10001100010001010000101110111000
Encoding = 0x8C450BB8
17
MIPS Encoding: I-Type
31 26 25 21 20 1615 0
rt
Immediate
sw $5, 3000($2)
rs
31 26 25 21 20 1615 0
10101100010001010000101110111000
opcode rs rt Immediate Value
10101100010001010000101110111000
Encoding = 0xAC450BB8
18
The immediate value is signed
Immediate Operands
• Make operand part of instruction itself!
op r rt 16 bit number
s
Logical Instructions
• Along with operating on full words there is a
need to process data at the bit level.
• Logical Operations
Shift Logical Left (SLL $S1,$S2,10)
Shift Logical Right (SRL $S1,$S2,10)
AND (AND $S1,$S2,$S3)
OR (OR $S1,$S2,$S3)
NOR (NOR $S1,$S2,$S3)
ANDI (ANDI $S1,$S2,100)
ORI (ORI $S1,$S2,100)
Shift Operations: shamt: how many positions to shift
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
•Shift left logical (SLL $S1,$S2,10) • Shift right logical (SRL $S1,$S2,10)
– Shift left and fill with 0 bits – Shift right and fill with 0 bits
– sll by i bits multiplies by 2i – srl by i bits divides by 2i
(unsigned only)
AND Operations
• Useful to mask bits in a word
– Select some bits, clear others to 0
• Example: if (i==j) h = i + j;
word-relative addressing:
25 words = 100 bytes;
bne $s0, $s1, Label also PC-relative (more…)
add $s3, $s0, $s1
Label: ....
Addresses in Branch
• Instructions:
bne $t4,$t5,Label Next instruction is at Label if $t4 != $t5
beq $t4,$t5,Label Next instruction is at Label if $t4 = $t5
• I Type Instruction Format:
op rs rt 16 bit offset
31 26 25 21 20 1615 0
31 26 25 21 20 1615 0
00010000000010010000000000001010
opcode rs rt Immediate Value
00010000000010010000000000001010
Encoding = 0x1009000A
30
MIPS assemble code and corresponding machine
instruction
J op 26 bit address
• Formats:
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
Control Flow
• Example C code:
int add10(int i)
{ return (i + 10);}
Procedures
• Translated MIPS assembly
• Note more efficient use of registers possible! save register
.text in stack, see
.globl main figure below
add10:
addi $sp, $sp, -4
main: sw $s0, 0($sp)
addi $s0, $0, 5
add $a0, $s0, $0 addi $s0, $a0, 10
argument add $v0, $s0, $0
to callee jal add10 result
control returns here to caller lw $s0, 0($sp)
jump and link restore
add $s1, $v0, $0 addi $sp, $sp, 4
add $s0, $s1, $0 values
jr $ra
li $v0, 10 return
system code
syscall & call to MEMO High address
exit $sp RY
Content of $s0
Low address
MIPS Addressing Modes
1. Immediate addressing
op rs rt Immediate
2. Register addressing
op rs rt rd ... funct Registers
Register
3. Base addressing
op rs rt Address Memor y
4. PC-relative addressing
op rs rt Address Memor y
PC + Word
5. Pseudodirect addressing
op Address Memor y
PC Word
MIPS Instruction Formats
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
MIPS assembly language
Category Instruction Example Meaning Comments
add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers
Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers
add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants
load word lw $s1, 100($s2) $s1 = Memory[$s2 + 100] Word from memory to register
store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory
Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100] Byte from memory to register
store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory
load upper immediate lui $s1, 100 $s1 = 100 * 2
16 Loads constant in upper 16 bits
branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC + 4 + 100
branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to Not equal test; PC-relative
PC + 4 + 100
Conditional
branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; for beq, bne
else $s1 = 0
set less than slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0