L2 Instructions Language of The Computer
L2 Instructions Language of The Computer
L2 Instructions Language of The Computer
COMPUTER
2
MIPS INSTRUCTION
FORMATS
R format - Uses three register operands
• Used by all arithmetic and logical instructions
I format - Uses two register operands and an
address/immediate value
• Used by load and store instructions
• Used by arithmetic and logical instructions with a constant
J format - Contains a jump address
• Used by Jump instructions
THE MIPS INSTRUCTION FORMATS
All MIPS instructions are 32 bits long. The three instruction formats:
31 26 21 16 11 6 0
• R-type op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
• I-type 31 26 21 16 0
op rs rt immediate
6 bits 5 bits 5 bits 16 bits
• J-type
31 26 0
op target address
The different fields are:6 bits 26 bits
4
Gazipur
MIPS INSTRUCTION LAYOUT
5
Gazipur
MIPS ADDRESSING MODES/INSTRUCTION
FORMATS
• All instructions 32 bits wide
Register (direct) op rs rt rd
register
Immediate op rs rt immed
Displacement
op rs rt immed
Memory
register +
PC-relative
op rs rt immed
Memory
PC +
6
Gazipur
§2.2 Operations of the Computer Hardware
ARITHMETIC
OPERATIONS
Add and subtract, three operands
• Two sources and one destination
add a, b, c # a gets b + c
All arithmetic operations have this form
Design Principle 1: Simplicity favours regularity
• Regularity makes implementation simpler
• Simplicity enables higher performance at lower cost
7
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
ARITHMETIC
EXAMPLE
C code:
f = (g + h) - (i + j);
Compiled MIPS code:
8
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
§2.3 Operands of the Computer Hardware
REGISTER OPERANDS
Arithmetic instructions use register
operands
MIPS has a 32 × 32-bit register file
• Use for frequently accessed data
• Numbered 0 to 31
• 32-bit data called a “word”
Assembler names
• $t0, $t1, …, $t9 for temporary values
• $s0, $s1, …, $s7 for saved variables
Design Principle 2: Smaller is faster
• c.f. main memory: millions of locations
9
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
REGISTER OPERAND
EXAMPLE
C code:
f = (g + h) - (i + j);
• f, …, j in $s0, …, $s4
Compiled MIPS code:
10
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
MEMORY OPERANDS
Main memory used for composite data
• Arrays, structures, dynamic data
To apply arithmetic operations
• Load values from memory into registers
• Store result from register to memory
Memory is byte addressed
• Each address identifies an 8-bit byte
Words are aligned in memory
• Address must be a multiple of 4
MIPS is Big Endian
• Most-significant byte at least address of a word
• c.f. Little Endian: least-significant byte at least address
11
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
MEMORY OPERAND
EXAMPLE 1
C code:
g = h + A[8];
• g in $s1, h in $s2, base address of A in $s3
Compiled MIPS code:
• Index 8 requires offset of 32
• 4 bytes per word
12
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
MEMORY OPERAND
EXAMPLE 2
C code:
A[12] = h + A[8];
• h in $s2, base address of A in $s3
Compiled MIPS code:
• Index 8 requires offset of 32
lw $t0, 32($s3) # load word
add $t0, $s2, $t0
sw $t0, 48($s3) # store word
13
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
REGISTERS VS.
MEMORY
Registers are faster to access than memory
Operating on memory data requires loads and stores
• More instructions to be executed
Compiler must use registers for variables as much as
possible
• Only spill to memory for less frequently used variables
• Register optimization is important!
14
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
IMMEDIATE
OPERANDS
Constant data specified in an instruction
15
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
THE CONSTANT ZERO
MIPS register 0 ($zero) is the constant 0
• Cannot be overwritten
Useful for common operations
• E.g., move between registers
add $t2, $s1, $zero
16
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
§2.5 Representing Instructions in the Computer
REPRESENTING
INSTRUCTIONS
Instructions are encoded in binary
• Called machine code
MIPS instructions
• Encoded as 32-bit instruction words
• Small number of formats encoding operation code
(opcode), register numbers, …
• Regularity!
Register numbers
• $t0 – $t7 are reg’s 8 – 15
• $t8 – $t9 are reg’s 24 – 25
• $s0 – $s7 are reg’s 16 – 23
17
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
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: destination register number
• shamt: shift amount (00000 for now)
• funct: function code (extends opcode)
18
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
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
000000100011001001000000001000002 = 0232402016
19
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
HEXADECIMAL
Base 16
• Compact representation of bit strings
• 4 bits per hex digit
20
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf
MIPS I-FORMAT
INSTRUCTIONS
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
21
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf