Computer Organization CS1403: Mayank Pandey, MNNIT, Allahabad, India
Computer Organization CS1403: Mayank Pandey, MNNIT, Allahabad, India
CS1403
Instruction Set
The repertoire of instructions of a
computer
Different computers: different
instruction sets
But with many aspects in common
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
Mayank Pandey, MNNIT,
Allahabad,performance
India
Arithmetic Example
C code:
f = (g + h) - (i + j);
# temp t0 = g + h
# temp t1 = i + j
# f = t0 - t1
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
Memory Operands
lw $t0, 32($s3)
# load word
add $s1,
$s2, $t0
offset
base register
Mayank Pandey, MNNIT, Allahabad,
India
requires offset of 32
32($s3)
# load word
$s2, $t0
48($s3)
# store word
Immediate Operands
Constant data specified in an
instruction
addi $s3, $s3, 4
x x n1 2n1 x n2 2n2 x1 21 x 0 20
Range: 0 to +2n 1
Example
0000 0000 0000 0000 0000 0000 0000 1011
= 0 + + 12
= 0 + + 8 + 0 + 2 + 1 = 11
10
Using 32 bits
0 to +4,294,967,295
Mayank Pandey, MNNIT, Allahabad,
India
2s-Complement Signed
Integers
Given an n-bit number
x x n1 2n1 x n2 2n2 x1 21 x 0 20
Range: 2n 1 to +2n 1 1
Example
1111 1111 1111 1111 1111 1111 1111 1100
2
31 + 1230 + + 122 +021 +020
= 12
= 2,147,483,648 + 2,147,483,644 = 4
10
Using 32 bits
2,147,483,648 to +2,147,483,647
Mayank Pandey, MNNIT, Allahabad,
India
2s-Complement Signed
Integers
Signed Negation
Example: negate +2
Sign Extension
Representing a number using more bits
Preserve the numeric value
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 regs 8 15
$t8 $t9 are regs 24 25
$s0 $s7 are regs 16 23
Mayank Pandey, MNNIT, Allahabad,
India
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)
Mayank Pandey, MNNIT, Allahabad,
India
R-format Example
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
$s1
$s2
$t0
add
17
18
32
000000
10001
10010
01000
00000
100000
000000100011001001000000001000002 = 0232402016
Mayank Pandey, MNNIT, Allahabad,
India
Hexadecimal
Base 16
0000
0100
1000
1100
0001
0101
1001
1101
0010
0110
1010
1110
0011
0111
1011
1111
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
Binary compatibility
allows compiled programs
to work on different
computers
Standardized ISAs
Mayank Pandey, MNNIT, Allahabad,
India
Logical Operations
Instructions for bitwise manipulation
Operation
Java
MIPS
Shift left
<<
<<
sll
Shift right
>>
>>>
srl
Bitwise AND
&
&
and, andi
Bitwise OR
or, ori
Bitwise NOT
nor
Shift Operations
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
AND Operations
Useful to mask bits in a word
Select some bits, clear others to 0
and $t0, $t1, $t2
$t2
$t1
$t0
OR Operations
$t1
$t0
NOT Operations
Useful to invert bits in a word
Change 0 to 1, and 1 to 0
Register 0: always
read as zero
$t1
$t0
Conditional Operations
Branch to a labeled instruction if a
condition is true
Otherwise, continue sequentially
j L1
unconditional
jump to instruction
Mayank Pandey, MNNIT, Allahabad,
labeled L1 India
Compiling If Statements
C code:
if (i==j) f = g+h;
else f = g-h;
f, g, in $s0, $s1,
while (save[i] == k) i += 1;
i in $s3, k in $s5, address of save in $s6
$t1,
$t1,
$t0,
$t0,
$s3,
Loop
$s3, 2
$t1, $s6
0($t1)
$s5, Exit
$s3, 1
slt
# signed
1 < +1 $t0 = 1
# unsigned
Mayank Pandey,
+4,294,967,295
>MNNIT,
+1 Allahabad,
$t0 = 0
India
Procedure Calling
Steps required
1.
2.
3.
4.
5.
6.
Register Usage
$a0 $a3: arguments (regs 4 7)
$v0, $v1: result values (regs 2 and 3)
$t0 $t9: temporaries
Can be overwritten by callee
Non-Leaf Procedures
Procedures that call other procedures
For nested call, caller needs to save
on the stack:
Its return address
Any arguments and temporaries needed
after the call
Non-Leaf Procedure
Example
C code:
int fact (int n)
{
if (n < 1) return f;
else return n * fact(n - 1);
}
Argument n in $a0
Result in $v0
Non-Leaf Procedure
Example
Memory Layout
Text: program code
Static data: global
variables
e.g., static variables in
C, constant arrays and
strings
$gp initialized to
address allowing
offsets into this
segment
Character Data
Byte-encoded character sets
ASCII: 128 characters
95 graphic, 33 control
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)
sh rt, offset(rs)
32-bit Constants
Most constants are small
16-bit immediate is sufficient
ori $s0, $s0, 2304 0000 0000 0011 1101 0000 1001 0000 0000
Mayank Pandey, MNNIT, Allahabad,
India
Branch Addressing
op
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
PC-relative addressing
Jump Addressing
op
address
6 bits
26 bits
80000
19
add
80004
22
32
lw
$t0, 0($t1)
80008
35
bne
80012
21
80016
19
19
80020
Loop: sll
Exit:
Loop
80024
20000
bne $s0,$s1, L2
j L1
L2:
Mayank Pandey, MNNIT, Allahabad,
India
Static linking
Assembler Pseudo-instructions
Most assembler instructions
represent machine instructions oneto-one
Pseudoinstructions: figments of the
assemblers imagination
add $t0, $zero, $t1
blt $t0, $t1, L
slt $at, $t0, $t1
move $t0, $t1
Loading a Program
Load from image file on disk into
memory
1. Read header to determine segment
sizes
2. Create virtual address space
3. Copy text and initialized data into
memory
Or set page table entries so they can be
faulted in
4.
5.
Dynamic Linking
Only link/load library procedure when
it is called
Requires procedure code to be
relocatable
Avoids image bloat caused by static
linking of all (transitively) referenced
libraries
Automatically picks up new library
versions
Mayank Pandey, MNNIT, Allahabad,
India
Lazy Linkage
Indirection table
Stub: Loads routine ID,
Jump to linker/loader
Linker/loader code
Dynamically
mapped code
Compiles
bytecodes of
hot methods
into native
code for host
machine
Interprets
bytecodes
C Sort Example
Illustrates use of assembly
instructions for a C bubble sort
function
Swap procedure (leaf)
void swap(int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
Mayank Pandey, MNNIT, Allahabad,
v in $a0, kIndiain $a1, temp in $t0
int i;
int *p;
array[i] = 0;
p = p + 1)
*p = 0;
}
move $t0,$zero
loop1: sll $t1,$t0,2
add $t2,$a0,$t1
# i = 0
move $t0,$a0
# p = & array[0]
# $t1 = i * 4
sll $t1,$a1,2
# $t1 = size * 4
# $t2 =
&array[i]
&array[size]
addi $t0,$t0,1
# i = i + 1
addi $t0,$t0,4
slt $t3,$t0,$a1
# $t3 =
(i < size)
bne $t3,$zero,loop1 # if ()
# p = p + 4
#(p<&array[size])
bne $t3,$zero,loop2 # if ()
# goto loop1
# goto loop2
Fallacies
Powerful instruction higher performance
Fewer instructions required
But complex instructions are hard to
implement
May slow down all instructions, including simple ones
Fallacies
Chapter 2
Instructions:
Pitfalls
Sequential words are not at
sequential addresses
Increment by 4, not by 1!
Design principles
1. Simplicity favors regularity
2. Smaller is faster
3. Make the common case fast
4. Good design demands good
compromises
Layers of software/hardware
Compiler, assembler, hardware
Concluding Remarks
Concluding
Remarks
Measure MIPS instruction executions
in benchmark programs
MIPS examples
SPEC2006 Int
SPEC2006 FP
Arithmetic
16%
48%
Data transfer
35%
36%
Logical
12%
4%
Cond. Branch
34%
8%
Jump
j, jr, jal
2%
0%
Chapter 2
Instructions: