Instructions: Language of The Machine
Instructions: Language of The Machine
Instructions: Language of The Machine
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
stages !
Design goals: maximize performance and minimize cost and
reduce design time
MIPS Arithmetic
Example:
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
...
Instruction Meaning
31 26 25 21 20 16 15 11 10 6 5 0
rd
rt
add $4, $3, $2
rs
31 26 25 21 20 16 15 11 10 6 5 0
0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
opcode rs rt rd shamt funct
0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
Encoding = 0x00622020
13
Machine Language
Consider the load-word and store-word instructions,
what would the regularity principle have us do?
we would have only 5 or 6 bits to determine the offset from a base
register - too little…
rt
Immediate
lw $5, 3000($2)
rs
31 26 25 21 20 16 15 0
1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
opcode rs rt Immediate Value
1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
Encoding = 0x8C450BB8
15
MIPS Encoding: I-Type
31 26 25 21 20 16 15 0
rt
Immediate
sw $5, 3000($2)
rs
31 26 25 21 20 16 15 0
1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
opcode rs rt Immediate Value
1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
Encoding = 0xAC450BB8
The immediate value is signed 16
Stored Program Concept
Instructions are bit sequences, just like data
Programs are stored in memory
to be read or written just like data
Processor Memory
memory for data, programs,
compilers, editors, etc.
Big-endian Little-endian
Memory Memory
Byte 0 Byte 1 Byte 2 Byte 3 Word 0 Byte 3 Byte 2 Byte 1 Byte 0 Word 0
Byte 4 Byte 5 Byte 6 Byte 7 Word 1 Byte 7 Byte 6 Byte 5 Byte 4 Word 1
Control: Conditional Branch
Decision making instructions
alter the control flow,
i.e., change the next instruction to be executed
rs
Offset
beq $0, $9, 40 Encoded by
40/4 = 10
rt
31 26 25 21 20 16 15 0
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
opcode rs rt Immediate Value
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
Encoding = 0x1009000A
23
Control: Unconditional Branch (Jump)
J op 26 bit address
MIPS Instructions:
addi $29, $29, 4
slti $8, $18, 10
andi $29, $29, 6
ori $29, $29, 4
op rs rt 16 bit number
How about larger constants?
First we need to load a 32 bit constant into a register
Must use two instructions for this: first new load upper immediate
instruction for upper 16 bits
lui $t0, 1010101010101010 filled with zeros
1010101010101010 0000000000000000
0000000000000000 1010101010101010
ori
1010101010101010 1010101010101010
Formats:
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
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
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
New instruction:
if $s1 < $s2 then
$t0 = 1
slt $t0, $s1, $s2 else
$t0 = 0
31 26 25 21 20 16 15 11 10 6 5 0
jalr r2 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 0 0 1
Or opcode rs 0 rd 0 funct
jalr r31, r2 (default=31)
31 26 25 21 20 16 15 11 10 6 5 0
jr r2 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
opcode rs 0 0 0 funct
42
Byte/Halfword Operations
Could use bitwise operations
MIPS byte/halfword load/store
String processing is a common case
lb rt, offset(rs) lh rt, offset(rs)
Sign extend to 32 bits in rt
lbu rt, offset(rs) lhu rt, offset(rs)
Zero extend to 32 bits in rt
sb rt, offset(rs) sh rt, offset(rs)
Store just rightmost byte/halfword
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
Overview of MIPS
Simple instructions – all 32 bits wide
Very structured – no unnecessary baggage
Only three instruction formats
R op rs rt rd shamt funct
I op rs rt 16 bit address
J op 26 bit address
Summarize MIPS:
MIPS operands
Name Example Comments
$s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform
32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero always equals 0. Register $at is
$fp, $sp, $ra, $at reserved for the assembler to handle large constants.
Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so
30
2 memory Memory[4], ..., sequential words differ by 4. Memory holds data structures, such as arrays,
words Memory[4294967292] and spilled registers, such as those saved on procedure calls.
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