0% found this document useful (0 votes)
38 views21 pages

L2 Instructions Language of The Computer

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 21

LECTURE 2 INSTRUCTIONS: LANGUAGE OF THE

COMPUTER

Dr. M. A. Rouf, Professor, Dept. of CSE, DUET, Gazipur


TOP 10 80X86
INSTRUCTIONS

CSE-4821 Dr. M. A. Rouf, Dept. of CSE, DUET,

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

• op: operation of the instruction


• rs, rt, rd: the source and destination register specifiers
• shamt: shift amount
• funct: selects the variant of the operation in the “op” field
• address / immediate: address offset or immediate value
• target address: target address of the jump instruction

CSE-4821 Dr. M. A. Rouf, Dept. of CSE, DUET,

4
Gazipur
MIPS INSTRUCTION LAYOUT

CSE-4821 Dr. M. A. Rouf, Dept. of CSE, DUET,

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 +

CSE-4821 Dr. M. A. Rouf, Dept. of CSE, DUET,

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:

add t0, g, h # temp t0 = g + h


add t1, i, j # temp t1 = i + j
sub f, t0, t1 # f = t0 - t1

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:

add $t0, $s1, $s2


add $t1, $s3, $s4
sub $s0, $t0, $t1

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

lw $t0, 32($s3) # load word


add $s1, $s2, $t0
offset base register

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

addi $s3, $s3, 4


No subtract immediate instruction
• Just use a negative constant
addi $s2, $s1, -1
Design Principle 3: Make the common case fast
• Small constants are common
• Immediate operand avoids a load 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

add $t0, $s1, $s2

special $s1 $s2 $t0 0 add

0 17 18 8 0 32

000000 10001 10010 01000 00000 100000

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

0 0000 4 0100 8 1000 c 1100


1 0001 5 0101 9 1001 d 1101
2 0010 6 0110 a 1010 e 1110
3 0011 7 0111 b 1011 f 1111

■ Example: eca8 6420


■ 1110 1100 1010 1000 0110 0100 0010 0000

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

Immediate arithmetic and load/store


instructions
• rt: destination or source register number
• Constant: –215 to +215 – 1
• Address: offset added to base address in rs
Design Principle 4: Good design demands
good compromises
• Different formats complicate decoding, but allow 32-bit
instructions uniformly
• Keep formats as similar as possible

21
Lecture 2 Instruction Set: Language of the Computer, by
Dr. M. A. Rouf

You might also like