Escolar Documentos
Profissional Documentos
Cultura Documentos
Architecture
Numbers
Bits are just bits (no inherent meaning)
conventions define relationship between bits and numbers
Binary numbers (base 2)
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...
decimal: 0...2n-1
Of course it gets more complicated:
numbers are finite (overflow)
fractions and real numbers
negative numbers
e.g., no MIPS subi instruction; addi can add a negative
number)
How do we represent negative numbers?
i.e., which bit patterns will represent which numbers?
Possible Representations
Sign Magnitude:
One's Complement
000 = +0
001 = +1
010 = +2
011 = +3
100 = -0
101 = -1
110 = -2
111 = -3
Two's Complement
000 = +0
001 = +1
010 = +2
011 = +3
100 = -3
101 = -2
110 = -1
111 = -0
000 = +0
001 = +1
010 = +2
011 = +3
100 = -4
101 = -3
110 = -2
111 = -1
MIPS
32 bit signed numbers:
0000
0000
0000
...
0111
0111
1000
1000
1000
...
1111
1111
1111
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1111
1111
0000
0000
0000
1110two
1111two
0000two
0001two
0010two
=
=
=
=
=
+
+
2,147,483,646ten
2,147,483,647ten
maxint
2,147,483,648ten
2,147,483,647ten
minint
2,147,483,646
ten
1010
0111
- 0110
0110
- 0101
Examples
(Versions)
Introduced in
Intel
1978
MIPS
1986
PowerPC
(601, 604, )
1993
Instructions
Instructions are the language of the machine
We will study the MIPS instruction set architecture
Known as Reduced Instruction Set Computer (RISC)
Elegant and relatively simple design
Similar to RISC architectures developed in mid-1980s and 90s
Very popular, used in many products
Silicon Graphics, ATI, Cisco, Sony, etc.
Memory
$0
$1
$2
32 General
Purpose
Registers
Arithmetic &
Logic Unit
$31
ALU
Execution &
Integer Unit
(Main proc)
Integer
mul/div
Hi
FPU
Floating
Point Unit
(Coproc 1)
F0
F1
F2
F31
FP
Arith
Floating-Point
Arithmetic Unit
Lo
TMU
Integer
Multiplier/Divider
32 Floating-Point
Registers
Trap &
BadVaddr
Status Memory Unit
Cause
(Coproc 0)
EPC
$0
$1
= $zero
= $at
$16 = $s0
$17 = $s1
$2
= $v0
$18 = $s2
$3
= $v1
$19 = $s3
$4
= $a0
$20 = $s4
$5
= $a1
$21 = $s5
$6
= $a2
$22 = $s6
$7
= $a3
$23 = $s7
$8
= $t0
$24 = $t8
$9
= $t1
$25 = $t9
$10 = $t2
$26 = $k0
$11 = $t3
$27 = $k1
$12 = $t4
$28 = $gp
$13 = $t5
$29 = $sp
$14 = $t6
$30 = $fp
$15 = $t7
$31 = $ra
Software conventions
There are many registers (32)
Software defines names to all registers
To standardize their use in programs
10
Register
$0
$1
$2 $3
$4 $7
$8 $15
$16 $23
$24 $25
$26 $27
$28
$sp
$fp
$ra
$29
$30
$31
Usage
Always 0
(forced by hardware)
Reserved for assembler use
Result values of a function
Arguments of a function
Temporary Values
Saved registers
(preserved across call)
More temporaries
Reserved for OS kernel
Global pointer
(points to global data)
Stack pointer
Frame pointer
Return address
11
Instruction Formats
All instructions are 32-bit wide, Three instruction formats:
Register (R-Type)
Register-to-register instructions
Op: operation code specifies the format of the instruction
Op6
Rs5
Rt5
Rd5
sa5
funct6
Immediate (I-Type)
16-bit immediate constant is part in the instruction
Op6
Rs5
Rt5
immediate16
Jump (J-Type)
Used by jump instructions
Op6
immediate26
12
Instruction Categories
Integer Arithmetic
Arithmetic, logical, and shift instructions
Data Transfer
Load and store instructions that access memory
Data movement and conversions
Miscellaneous
Instructions that transfer control to/from exception handlers
Memory management instructions
12-Mar-15 | Adeel Israr | COMSATS Institute of Information Technology, Islamabad
13
Stack Segment
Stack Grows
Downwards
Memory
Addresses
in Hex
Dynamic Area
Data Segment
0x10000000
Static Area
Text Segment
0x04000000
Reserved
0
12-Mar-15 | Adeel Israr | COMSATS Institute of Information Technology, Islamabad
14
15
.TEXT directive
Defines the code segment of a program containing instructions
.GLOBL directive
Declares a symbol as global
Global symbols can be referenced from other files
We use this directive to declare main procedure of a program
12-Mar-15 | Adeel Israr | COMSATS Institute of Information Technology, Islamabad
16
R-Type Format
Op6
Rs5
Rt5
Rd5
sa5
funct6
17
Meaning
$s1 = $s2 + $s3
$s1 = $s2 + $s3
$s1 = $s2 $s3
$s1 = $s2 $s3
R-Type Format
op = 0
op = 0
op = 0
op = 0
rs = $s2
rs = $s2
rs = $s2
rs = $s2
rt = $s3
rt = $s3
rt = $s3
rt = $s3
rd = $s1
rd = $s1
rd = $s1
rd = $s1
sa = 0
sa = 0
sa = 0
sa = 0
f = 0x20
f = 0x21
f = 0x22
f = 0x23
18
Addition/Subtraction Example
Consider the translation of: f = (g+h) (i+j)
Compiler allocates registers to variables
Assume that f, g, h, i, and j are allocated registers $s0 thru $s4
Called the saved registers: $s0 = $16, $s1 = $17, , $s7 = $23
# $t0 = g + h
# $t1 = i + j
# f = (g+h)(i+j)
Solution:
sa
func
19
x y
0
0
1
1
0
0
1
1
0
1
0
1
0
0
0
1
0
1
0
1
x or y
0
1
1
1
x y x xor y
x y x nor y
0
0
1
1
0
0
1
1
0
1
0
1
0
1
1
0
0
1
0
1
1
0
0
0
20
10
Meaning
$s1 = $s2 & $s3
$s1 = $s2 | $s3
$s1 = $s2 ^ $s3
$s1 = ~($s2|$s3)
R-Type Format
op = 0
op = 0
op = 0
op = 0
rs = $s2
rs = $s2
rs = $s2
rs = $s2
rt = $s3
rt = $s3
rt = $s3
rt = $s3
rd = $s1
rd = $s1
rd = $s1
rd = $s1
sa = 0
sa = 0
sa = 0
sa = 0
f = 0x24
f = 0x25
f = 0x26
f = 0x27
Examples:
Assume $s1 = 0xabcd1234 and $s2 = 0xffff0000
and $s0,$s1,$s2
# $s0 = 0xabcd0000
or
$s0,$s1,$s2
# $s0 = 0xffff1234
xor $s0,$s1,$s2
# $s0 = 0x54321234
nor $s0,$s1,$s2
# $s0 = 0x0000edcb
21
Shift Operations
Shifting is to move all the bits in a register left or right
Shifts by a constant amount: sll, srl, sra
sll/srl mean shift left/right logical by a constant amount
The 5-bit shift amount field is used by these instructions
sra means shift right arithmetic by a constant amount
The sign-bit (rather than 0) is shifted from the left
sll
shift-out MSB
srl
shift-in 0
sra
shift-in sign-bit
32-bit register
...
shift-in 0
...
shift-out LSB
...
shift-out LSB
22
11
Shift Instructions
Instruction
sll
srl
sra
sllv
srlv
srav
Meaning
$s1,$s2,10
$s1,$s2,10
$s1, $s2, 10
$s1,$s2,$s3
$s1,$s2,$s3
$s1,$s2,$s3
R-Type Format
op = 0
op = 0
op = 0
op = 0
op = 0
op = 0
rs = 0 rt = $s2
rs = 0 rt = $s2
rs = 0 rt = $s2
rs = $s3 rt = $s2
rs = $s3 rt = $s2
rs = $s3 rt = $s2
rd = $s1
rd = $s1
rd = $s1
rd = $s1
rd = $s1
rd = $s1
sa = 10
sa = 10
sa = 10
sa = 0
sa = 0
sa = 0
f=0
f=2
f=3
f=4
f=6
f=7
$s1,$s2,8
$s1 = $s2<<8
$s1 = 0xcd123400
sra
$s1,$s2,4
$s1 = $s2>>4
$s1 = 0xfabcd123
$s1 = $s2>>>$s3
$s1 = 0x0000abcd
srlv $s1,$s2,$s3
23
Binary Multiplication
Shift-left (sll) instruction can perform multiplication
When the multiplier is a power of 2
$t0, $s1, 2
; $t0 = $s1 * 4
sll
$t1, $s1, 5
; $t1 = $s1 * 32
; $s2 = $s1 * 36
24
12
Your Turn . . .
Multiply $s1 by 26, using shift and add instructions
Hint: 26 = 2 + 8 + 16
sll
sll
addu
sll
addu
$t0,
$t1,
$s2,
$t0,
$s2,
$s1,
$s1,
$t0,
$s1,
$s2,
1
3
$t1
4
$t0
;
;
;
;
;
$t0
$t1
$s2
$t0
$s2
=
=
=
=
=
$s1
$s1
$s1
$s1
$s1
*
*
*
*
*
2
8
10
16
26
; $s2 = $s1 * 32
; $s2 = $s1 * 31
25
Next . . .
Instruction Set Architecture
Overview of the MIPS Architecture
R-Type Arithmetic, Logical, and Shift Instructions
I-Type Format and Immediate Constants
Jump and Branch Instructions
Translating If Statements and Boolean Expressions
Load and Store Instructions
Translating Loops and Traversing Arrays
Addressing Modes
12-Mar-15 | Adeel Israr | COMSATS Institute of Information Technology, Islamabad
26
13
I-Type Format
Constants are used quite frequently in programs
The R-type shift instructions have a 5-bit shift amount constant
What about other instructions that need a constant?
Rs5
Rt5
immediate16
# $s1 = $s2 + 5
OR immediate:
# $s1 = $s2 | 5
ori
$s1, $s2, 5
27
$s1, $s2, 10
$s1, $s2, 10
$s1, $s2, 10
$s1, $s2, 10
$s1, $s2, 10
$s1, 10
Meaning
$s1 = $s2 + 10
$s1 = $s2 + 10
$s1 = $s2 & 10
$s1 = $s2 | 10
$s1 = $s2 ^ 10
$s1 = 10 << 16
I-Type Format
op = 0x8
op = 0x9
op = 0xc
op = 0xd
op = 0xe
op = 0xf
rs = $s2
rs = $s2
rs = $s2
rs = $s2
rs = $s2
0
rt = $s1
rt = $s1
rt = $s1
rt = $s1
rt = $s1
rt = $s1
imm16 = 10
imm16 = 10
imm16 = 10
imm16 = 10
imm16 = 10
imm16 = 10
28
14
translated as
addiu $s0,$s1,5
C = B1;
translated as
addiu $s2,$s1,-1
imm = -1 = 1111111111111111
A = B&0xf; translated as
andi
$s0,$s1,0xf
C = B|0xf; translated as
ori
$s2,$s1,0xf
C = 5;
translated as
ori
$s2,$zero,5
A = B;
translated as
ori
$s0,$s1,0
29
32-bit Constants
I-Type instructions can have only 16-bit constants
Op6
Rs5
Rt5
immediate16
load upper
16 bits
clear lower
16 bits
lui $s1,0xAC51
$s1=$17 0xAC51
0x0000
ori $s1,$s1,0x65D9
$s1=$17 0xAC51
0x65D9
30
15
J-Type Format
Op6
immediate26
label
. . .
label:
# jump to label
immediate26
00
least-significant
2 bits are 00
31
bne Rs,Rt,label
bltz Rs,label
bgtz Rs,label
blez Rs,label
bgez Rs,label
32
16
rd,rs,rt
sltu
rd,rs,rt
unsigned <
slti
rt,rs,im16
sltiu rt,rs,im16
unsigned <
results in
$t0 = 0
stlu $t0,$s0,$s1
results in
$t0 = 1
slt
33
bltu
bleu
bgtu
bgeu
(signed/unsigned)
(signed/unsigned)
(signed/unsigned)
(signed/unsigned)
blt $s0,$s1,label
slt $at,$s0,$s1
bne $at,$zero,label
How to implement:
Solution:
ble $s2,$s3,label
slt $at,$s3,$s2
beq $at,$zero,label
34
17
Pseudo-Instructions
Introduced by assembler as if they were real instructions
To facilitate assembly language programming
Pseudo-Instructions
move $s1, $s2
not $s1, $s2
li
$s1, 0xabcd
li
$s1, 0xabcd1234
sgt
blt
35
Meaning
j
beq
label
jump to label
rs, rt, label branch if (rs == rt)
bne rs, rt, label branch if (rs != rt)
blez rs, label
branch if (rs<=0)
bgtz rs, label
branch if (rs > 0)
bltz rs, label
branch if (rs < 0)
bgez rs, label
branch if (rs>=0)
Instruction
slt
sltu
slti
sltiu
Format
op6 = 2
op6 = 4
rs5
rt5
imm26
imm16
op6 = 5
op6 = 6
op6 = 7
op6 = 1
op6 = 1
rs5
rs5
rs5
rs5
rs5
rt5
0
0
0
1
imm16
imm16
imm16
imm16
imm16
op6 = 0
op6 = 0
0xa
0xb
rs5
rs5
rs5
rs5
Meaning
rd, rs, rt
rd=(rs<rt?1:0)
rd, rs, rt
rd=(rs<rt?1:0)
rt, rs, imm16 rt=(rs<imm?1:0)
rt, rs, imm16 rt=(rs<imm?1:0)
Format
rt5
rt5
rt5
rt5
rd5
rd5
0
0x2a
0
0x2b
imm16
imm16
36
18
Translating an IF Statement
Consider the following IF statement:
if (a == b) c = d + e; else c = d e;
Assume that a, b, c, d, e are in $s0, , $s4 respectively
How to translate the above IF statement?
bne
addu
exit
else:
subu
exit:
. . .
then:
37
Translating an IF Statement
Consider the following IF statement:
if (a == b) c = d + e; else c = d e;
Assume that a, b, c, d, e are in $s0, , $s4 respectively
How to translate the above IF statement?
else:
then:
exit:
beq
subu
exit
addu
. . .
38
19
39
Translating an IF Statement
Consider the following IF statement:
if (a == b) c = d + e Assume that a, b, c, d, e are in $s0, ,
$s4 respectively
Code when branch condition is opposite to its C counterpart?
then:
exit:
bne
$s0, $s1, exit
addu $s2, $s3, $s4
. . .
Which
then:
exit:
beq
$s0, $s1, then
j
exit
addu $s2, $s3, $s4
. . .
going to
be faster
40
20
41
...
# skip if false
# skip if false
# both are true
42
21
43
Your Turn . . .
Translate the IF statement to assembly language
$s1 and $s2 values are unsigned
if( $s1 <= $s2 ) {
$s3 = $s4
}
44
22
load
Memory
Registers
store
Store Instruction:
Transfers data from a register to memory
45
# Rt
MEMORY[Rs+imm16]
# Rt
MEMORY[Rs+imm16]
Rs5
Rt5
immediate16
Memory Word
Base address
46
23
$s1, 8($s0)
# $s1 = A[2]
addiu
$s2, $s1, 5
# $s2 = A[2] + 5
sw
$s2, 4($s0)
# A[1] = $s2
Registers
...
...
$s0 = $16 address of A
$s1 = $17 value of A[2]
$s2 = $18
A[2] + 5
...
lw
sw
A[3]
A[2]
A+12
A+8
A[1]
A+4
A[0]
...
47
sign extend
s s
zero extend
bu
sign extend
s s
zero extend
hu
48
24
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
rt, imm16(rs)
Meaning
I-Type Format
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
rt = MEM[rs+imm16]
MEM[rs+imm16] = rt
MEM[rs+imm16] = rt
MEM[rs+imm16] = rt
rs5
rs5
rs5
rs5
rs5
rs5
rs5
rs5
0x20
0x21
0x23
0x24
0x25
0x28
0x29
0x2b
rt5
rt5
rt5
rt5
rt5
rt5
rt5
rt5
imm16
imm16
imm16
imm16
imm16
imm16
imm16
imm16
If Immediate16 = 0 then
49
Memory
...
A[i]
...
A+4i
A[2]
A+8
A[1]
A[0]
A+4
...
xor
move
loop: lw
beq
addiu
sll
Address A[i] = A[0] + 4*i
addu
j
exit: . . .
$s1,
$t0,
$t1,
$t1,
$s1,
$t0,
$t0,
loop
$s1, $s1
$s0
0($t0)
$s2, exit
$s1, 1
$s1, 2
$s0, $t0
#
#
#
#
#
#
#
i = 0
$t0 = address A
$t1 = A[i]
exit if (A[i]== k)
i = i+1
$t0 = 4*i
$t0 = address A[i]
50
25
$s1,
$t0,
$t1,
$t1,
$s1,
$t0,
$t0,
loop
$s1, $s1
$s0
0($t0)
$s2, exit
$s1, 1
$s1, 1
$s0, $t0
#
#
#
#
#
#
#
i = 0
$t0 = address A
$t1 = A[i]
exit if (A[i]== k)
i = i+1
$t0 = 2*i
$t0 = address A[i]
51
$s1,
$t0,
$t1,
$t1,
$s1,
$t0,
loop
$s1, $s1
$s0
0($t0)
$s2, exit
$s1, 1
$s0, $t0
#
#
#
#
#
#
i = 0
$t0 = address A
$t1 = A[i]
exit if (A[i]== k)
i = i+1
$t0 = address A[i]
52
26
Memory
...
A[i]
...
A+4i
A[2]
A+8
A[1]
A[0]
A+4
...
xor
move
loop: lw
beq
addiu
sll
Address A[i] = A[0] + 4*i
addu
j
exit: . . .
$s1,
$t0,
$t1,
$t1,
$s1,
$t0,
$t0,
loop
$s1, $s1
$s0
0($t0)
$s2, exit
$s1, 1
$s1, 2
$s0, $t0
#
#
#
#
#
#
#
i = 0
$t0 = address A
$t1 = A[i]
exit if (A[i]== k)
i = i+1
$t0 = 4*i
$t0 = address A[i]
53
Memory
...
A[i]
...
A+4i
A[2]
A+8
A[1]
A[0]
A+4
...
xor
move
j
loop: addiu
sll
addu
cond: lw
bne
exit: . . .
$s1,
$t0,
cond
$s1,
$t0,
$t0,
$t1,
$t1,
$s1, $s1
$s0
# i = 0
# $t0 = address A
$s1, 1
$s1, 2
$s0, $t0
0($t0)
$s2, loop
#
#
#
#
#
i = i+1
$t0 = 4*i
$t0 = address A[i]
$t1 = A[i]
exit if (A[i]== k)
5 Loop
This
code
Inst.,
is not
3 Non-Loop
good for Inst.
A[0]
54
27
$t0,
cond
$s1,
$t0,
$t1,
$t1,
$s0
$s1, 1
$t0, 4
0($t0)
$s2, loop
# $t1 = A[i]
# loop if A[i]!= k
55
Copying a String
The following code copies source string to target string
Address of source in $s0 and address of target in $s1
Strings are terminated with a null character (C strings)
i = 0;
do {target[i]=source[i]; i++;} while (source[i]!=0);
move
move
L1: lb
sb
addiu
addiu
bne
$t0,
$t1,
$t2,
$t2,
$t0,
$t1,
$t2,
$s0
$s1
0($t0)
0($t1)
$t0, 1
$t1, 1
$zero, L1
#
#
#
#
#
#
#
56
28
$t0,
$t1,
$s2,
cnd
$t2,
$s2,
$t0,
$t1,
$t1,
$s0
$t1, $t1
$s2, $s2
#
#
#
#
#
#
#
#
#
0($t0)
$s2, $t2
$t0, 4
$t1, 1
$s1, L1
57
Addressing Modes
Where are the operands?
How memory addresses are computed?
Immediate Addressing
Op6
Rs5
Rt5
immediate16
Operand is a constant
Register Addressing
Op6
Rs5
Rt5
Rd5
sa5
Operand is in a register
funct6
Register
Rs5
Rt5
immediate16
Byte
Halfword
Word
58
29
PC-Relative Addressing
Op6
Rs5
Rt5
immediate16
Word = Target Instruction
+1
PC30
00
PC30 + immediate16 + 1
Pseudo-direct Addressing
Op6
immediate26
Word = Target Instruction
:
PC4
00
PC26
00
PC4
immediate26
00
59
PC4
immediate26
00
PC30 + immediate16 + 1
00
60
30
61
2. Smaller is faster
Limit the number of registers for faster access (typically 32)
62
31