3-Instruction Set Architecture
3-Instruction Set Architecture
N SETS
INSTRUCTIONS
Instructions: Language of the Machine
Instruction set architecture - the attributes of
a computer system seen by the programmer
Attributes
🢝organization of programmable storage
🢝data types and data structures, encodings, representations
🢝instruction format, operands
🢝instruction set (operation code set)
🢝addressing modes, accessing data items and instructions
🢝exceptional (interrupt) conditions
THE ISA
5
PROCEDURE CALLING
Steps required
1. Place parameters in registers
2. Transfer control to procedure
3. Acquire storage for procedure
4. Perform procedure’s operations
5. Place result in register for caller
6. Return to place of call
TRANSLATION AND
STARTUP
Many compilers produce
object modules directly
Static linking
STARTING JAVA
APPLICATIONS
Simple
portable
instruction set
for the JVM
Compiles
Interprets
bytecodes of
bytecodes
“hot”
methods into
native code
for host
machine
MIPS ISA
MIPS ARCHITECTURE
MIPS – semiconductor
company that built
one of the first
commercial RISC
architectures
MIPS widely used in
embedded apps
Almost 100 million MIPS
processors manufactured in
2002
Used by NEC, Nintendo,
Cisco, Silicon Graphics, Sony,
…
MIPS ARCHITECTURE
32-bit RISC Processor
32-bit Registers, $0..$31
Pipelined Execution of Instructions
All instructions are 32-bits
Most Instructions executed in one clock cycle
230 32-bit memory words
Byte addressable memory
🢝A 32-bit word contains four bytes
🢝To address the next word of memory add 4
ARM & MIPS ISA
ARM & MIPS SIMILARITIES
ARM: the most popular embedded core
Similar basic set of instructions to MIPS
ARM MIPS
Date announced 1985 1985
Instruction size 32 bits 32 bits
Address space 32-bit flat 32-bit flat
Data alignment Aligned Aligned
Data addressing modes 9 3
Registers 15 × 32-bit 31 × 32-bit
Input/output Memory Memory
mapped mapped
ISA COMPARISON
COMPARE AND BRANCH
IN ARM
Uses condition codes for result of an
arithmetic/logical instruction
🢝Negative, zero, carry, overflow
🢝Compare instructions to set condition codes
without keeping the result
Design principles
🢝small, regular & simple design 🡺 fast
🢝make the common case fast
🢝good design requires good compromises
Features
🢝32 32-bit registers, r0 = 0 always
🢝only load and store instructions access memory
🢝32-bit instructions, fixed size opcode, leftmost 6 bits
🢝 fixed-field decoding
🢝all ALU operations are 3 address register operations
🢝 add r1, r2, r3, meaning: r1 🡸 r2 + r3
REGISTERS
32 general-purpose registers.
Registers all begin with a dollar-
symbol ($)
32 floating-point registers. The
floating-point registers are named
$f0, $f1, ..., $f31.
When programming in MIPS
assembly, it is usually best to use
the register names.
28
REGISTERS
Number Name Comments
$0 $zero Always zero
$1 $at Reserved for assembler
$2, $3 $v0, $v1 First and second return values, respectively
$4, ..., $7 $a0, ..., $a3 First four arguments to functions
$8, ..., $15 $t0, ..., $t7 Temporary registers
$16, ..., $23 $s0, ..., $s7 Saved registers
$24, $25 $t8, $t9 More temporary registers
$26, $27 $k0, $k1 Reserved for kernel (operating system)
$28 $gp Global pointer
$29 $sp Stack pointer
$30 $fp Frame pointer
$31 $ra Return address
29
MIPS POLICY
op rs rt immediate or offset
op address
J 6 bits 26 bits
MIPS R-FORMAT
INSTRUCTIONS
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
MIPS I-FORMAT
INSTRUCTIONS
op rs rt immediate or offset
6 bits 5 bits 5 bits 16 bits
I(immediate)-format Instructions
Immediate arithmetic and load/store instructions
🢝rs: source register number
🢝rt: destination register number
🢝Constant: –215 to +(215 – 1), used as immediate
🢝Address: offset added to base address in rs
I-FORMAT EXAMPLE:
ADDI
op rs rt immediate or offset
6 bits 5 bits 5 bits 16 bits
810 17 8 8
001000100010100000000000000010002 =
22280008
I-FORMAT EXAMPLE:
LOAD
op rs rt immediate or offset
6 bits 5 bits 5 bits 16 bits
3510 17 8 8
100011100010100000000000000010002 =
8E28000816
I-FORMAT EXAMPLE:
STORE
op rs rt immediate or offset
6 bits 5 bits 5 bits 16 bits
4310 17 8 8
101011100010100000000000000010002 =
I-FORMAT EXAMPLE:
BEQ
op rs rt offset
6 bits 5 bits 5 bits 16 bits
410 17 8 8
000100100010100000000000000010002 =
1228000816
MIPS J-FORMAT
INSTRUCTIONS
op address
6 bits 26 bits
op address
6 bits 26 bits
register
ADDRESSING MODES
Base addressing (aka displacement)
lw $1, 100($2) # $2 == 400, M[500]
op== 42
r r Offset/
s t displacement
registe 10
r 0
40 Memor
0 y
Effective
4
addres 2
s
ADDRESSING MODES
Immediate addressing
addi $1, $2, 100
op r r immediate
s t
ADDRESSING MODES
PC relative addressing
beq $1, $2, 100 # if ($1==$2) PC = PC + 100
op r r addres
s t s
PC
Memor
y
Effective
addres
s
4
MIPS ARITHMETIC AND
LOGICAL INSTRUCTIONS
ADD $1,$2,$3
🢝Register 1 = Register 2 + Register 3
SUB $1,$2,$3
🢝Register 1 = Register 2 - Register 3
AND $1,$2,$3
🢝Register 1 = Register 2 AND Register 3
ADDI $1,$2,10
🢝Register 1 = Register 2 + 10
SW $1,100
🢝Contents of memory location 100 = Register 1
🢝Used to save registers
LUI $1,100
🢝Register 1 upper 16-bits = 100
🢝Lower 16-bits are set to all 0’s
🢝Used to load a constant in the upper 16-bits
🢝Other constants in instructions are only 16-bits, so this
instruction is used when a constant larger than 16-bits is
required for the operation
MIPS JUMP
INSTRUCTIONS
J 100
🢝Jumps to PC+100
JAL 100
🢝Save PC in $31 ($ra: Return address) and
Jumps to PC+100
🢝Used for subroutine calls
JR $31
🢝Jumps to address in register 31
🢝Used for subroutine returns
MIPS BRANCH
INSTRUCTIONS
BEQ $1, $2, 100
🢝If register 1 equal to register 2 jump to
PC+100
🢝Used for Assembly Language If statements
55
MIPS LABELS
.WORD
🢝Reserve a 32-bit word in memory for a variable
.ASCII
🢝Place a string in memory using the ASCII
character code
.TEXT
🢝The following lines are instructions
MIPS EXAMPLES
main: lw $t0, size
.data # "data section" global, static
la $t1, arr1
modifiable data
Loop: beq $0, $t0, Exit
la $a0, msg1
arr1: .word 100, 101, 102 li $v0, 4 # "print string" system call
syscall
size: .word 3
nl: .asciiz "\n" lw $a0, ($t1)
li $v0, 1 #Print int
msg1: .asciiz "Hello, World "
syscall
.text # "text section" code and read-
la $a0, nl
only data
li $v0, 4 # "print string" system call
syscall
Exit:
60
MIPS EXAMPLES
#A = B + C;
LW $2, B #Register 2 = value of
B
LW $3, C #Register 3 = value of
C
ADD $4, $2, $3 #Register 4 =
B+C
SW $4, A #A = B + C
MIPS EXAMPLES
# f=(g+h)-(i+j);
# compiler puts f,g,h,i in
# $16,$17,$18,$19,$20
add $8,$17,$18 # $8 =g+h
add $9,$19,$20 #9=i+j
sub $16,$8,$9 # $16=(g+h)-(i+j)
MIPS EXAMPLES
#for loop
ADDI $5,$0,10
Loop: …
…
…
ADDI $5,$5,-1
BNE $5,$0,Loop
C SORT EXAMPLE
MIPS code:
swap: sll $t1, $a1, 2 # $t1 = k * 4
add $t1, $a0, $t1 # $t1 = v+(k*4)
# (address of v[k])
lw $t0, 0($t1) # $t0 (temp) = v[k]
lw $t2, 4($t1) # $t2 = v[k+1]
sw $t2, 0($t1) # v[k] = $t2 (v[k+1])
sw $t0, 4($t1) # v[k+1] = $t0 (temp)
jr $ra # return to calling routine
THE SORT PROCEDURE
IN C
Non-leaf (calls swap)
void sort (int v[], int n)
{
int i, j;
for (i = 0; i < n; i += 1) {
for (j = i – 1;
j >= 0 && v[j] > v[j + 1];
j -= 1) {
swap(v,j);
}
}
}
🢝v in $a0, n in $a1, i in $s0, j in $s1
THE PROCEDURE BODY
move $s2, $a0 # save $a0 into $s2
move $s3, $a1 # save $a1 into $s3
move $s0, $zero #i=0
for1tst: slt $t0, $s0, $s3 # $t0 = 0 if $s0 ≥ $s3 (i ≥ n)
beq $t0, $zero, exit1 # go to exit1 if $s0 ≥ $s3 (i ≥ n)
addi $s1, $s0, –1 #j=i–1
for2tst: slti $t0, $s1, 0 # $t0 = 1 if $s1 < 0 (j < 0)
bne $t0, $zero, exit2 # go to exit2 if $s1 < 0 (j < 0)
sll $t1, $s1, 2 # $t1 = j * 4
add $t2, $s2, $t1 # $t2 = v + (j * 4)
lw $t3, 0($t2) # $t3 = v[j]
lw $t4, 4($t2) # $t4 = v[j + 1]
slt $t0, $t4, $t3 # $t0 = 0 if $t4 ≥ $t3
beq $t0, $zero, exit2 # go to exit2 if $t4 ≥ $t3
move $a0, $s2 # 1st param of swap is v (old $a0)
move $a1, $s1 # 2nd param of swap is j
jal swap # call swap procedure
addi $s1, $s1, –1 # j –= 1
j for2tst # jump to test of inner loop
exit2: addi $s0, $s0, 1 # i += 1
j for1tst # jump to test of outer loop
THE FULL PROCEDURE
sort: addi $sp,$sp, –20 # make room on stack for 5
registers
sw $ra, 16($sp) # save $ra on stack
sw $s3,12($sp) # save $s3 on stack
sw $s2, 8($sp) # save $s2 on stack
sw $s1, 4($sp) # save $s1 on stack
sw $s0, 0($sp) # save $s0 on stack
… # procedure body
…
exit1: lw $s0, 0($sp) # restore $s0 from stack
lw $s1, 4($sp) # restore $s1 from stack
lw $s2, 8($sp) # restore $s2 from stack
lw $s3,12($sp) # restore $s3 from stack
lw $ra,16($sp) # restore $ra from stack
addi $sp,$sp, 20 # restore stack pointer
jr $ra # return to calling routine
ARRAYS VS. POINTERS
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