VLSI
VLSI
VLSI
2005-07-05
Big Idea: Stored-Program Concept
• Everything
E thi has
h a memory address:
dd
instructions, data words
• MIPS Instruction:
add $8,$9,$10
• MIPS Instruction:
add $8,$9,$10
Decimal number per field representation:
0 9 10 8 0 32
Binary number per field representation:
000000 01001 01010 01000 00000 100000
hex
hex representation: 012A 4020hex
decimal representation: 19
19,546,144
546 144ten
• Called a Machine Language Instruction
I-Format Instructions (1/4)
• What about instructions with
immediates (e.g. addi and lw)?
• 5-bit field only represents numbers up to
the value 31: immediates may be much
larger than this
• Ideally,
y, MIPS would have onlyy one
instruction format (for simplicity):
unfortunately, we need to compromise
• Define new instruction format that is
partially consistent with R
R-format:
format:
• Notice that, if instruction has an immediate,
(if immediate > 31) then it uses at most 2
registers.
I-Format Instructions (2/4)
• Define “fields” of the following number
of bits each: 6 + 5 + 5 + 16 = 32 bits
6 5 5 16
• Again, each field has a name:
opcode rs rt immediate
• Key Concept: Only one field is
inconsistent with R-format. Most
importantly, opcode is still in same
location.
• Notice: No rd field
I-Format Instructions (3/4)
• What do these fields mean?
•opcode: same as before except that, since
there’s
there s no funct field,
field opcode uniquely
specifies an instruction in I-format
• This also answers question of why
R-format has two 6-bit fields to identify
instruction instead of a single 12-bit field:
in order to be consistent with other
formats. (to be consistent with 6 bit I
Format opcode
p format))
•rs: specifies the only register operand (if
there is one)
•rt: specifies register which will receive
result of computation (this is why it’s
called the target register “rt”) (source for R format
before)
I-Format Instructions (4/4)
• The Immediate Field:
addi, slti, sltiu, the immediate is
•addi,
sign-extended to 32 bits. Thus, it’s
treated as a signed integer.
• 16 bits can be used to represent
immediate up p to 216 different values
• This is large enough to handle the offset
in a typical
yp lw or sw,, plus
p a vast majority
j y
of values that will be used in the slti
instruction.
I-Format Example (1/2)
• MIPS Instruction:
addi
ddi $21
$21,$22,-50
$22 50
• MIPS Instruction:
addi $21,$22,-50
Decimal/field representation:
8 22 21 -50
50
Binary/field representation:
001000 10110 10101 1111111111001110
hexadecimal representation: 22D5 FFCEhex
decimal representation: 584,449,998ten
I-Format Problems (0/3)
• Problem 0: Unsigned # sign
sign-extended?
extended?
•addiu, sltiu, sign-extends immediates
to 32 bits
bits. Thus
Thus, # is a “signed”
signed integer.
integer
• Rationale
•addiu so that can add w/out overflow
(of immediate #) - See K&R pp. 230, 305
•sltiu suffers so that we can have ez HW
(
(easier
i hardware
h d but
b t problems
bl on sltiu)
lti )
- Does this mean we’ll get wrong answers?
- Nope,
Nope it means assembler has to handle any an
unsigned immediate 2 ≤ n < 2 (I.e., with a
15 16
1 in the 15th bit and 0s in the upper 2 bytes)
as it does for numbers that are too large
large.
Problem 1 rationale
I-Format Problems (1/3)
• Problem 1:
• Chances are that addi, lw, sw and slti
will use immediates small enough to fit in
the immediate field
field.
• …but what if it’s too big?
• We need a way to deal with a 32-bit
immediate in any I-format instruction.
I-Format Problems (2/3)
• Solution to Problem 1:
• Handle it in software + new instruction
• Don’t change the current instructions:
instead add a new instruction to help out
instead,
• New instruction: ((16b immediate solution))
lui register, immediate
• stands
t d forf Load
L d Upper
U I
Immediate
di t
• takes 16-bit immediate and puts these bits
i the
in th upper half
h lf (high
(hi h order
d half)
h lf) off the
th
specified register
• sets lower half to 0s
I-Format Problems (3/3)
• Solution
S l ti tot Problem
P bl 1 ((continued):
ti d)
• So how does lui help
p us?
• Example:
addi $t0
$t0,$t0,
$t0 0xABABCDCD
becomes:
l i
lui $
$at,
t 0xABAB
0 ABAB
ori $at, $at, 0xCDCD
add $t0,$t0,$at
• Now each I-format instruction has only a 16-
bit immediate.
• Wouldn’t it be nice if the assembler would
tthis
s for
o us automatically?
auto at ca y ((later)
ate )
J-Format Instructions (0/5)
“jj <label>
<label>”
means
• Optimization:
• jumps will only jump to word aligned
addresses,
addresses
- so last two bits of address are always 00 (in
binary).
binary)
- let’s just take this for granted and not even
specify them.
J-Format Instructions (4/5)
• Now : we have 28 bits of a 32-bit address
• Where
Wh d
do we gett the
th other
th 4 bits?
bit ?
• Byy definition,, take the 4 highest-order
g bits
from the PC.
• Technically, y, this means that we cannot jump
j p
to anywhere in memory, but it’s adequate
99.9999…% of the time, since programs
aren’t
’t that
th t long
l
- only if jump straddles a 256 MB (28bits)
boundary
- If we absolutely need to specify a 32-bit
address we can always put it in a register and
address,
use the jr instruction. (long jump)
J-Format Instructions (5/5)
• Summary:
• Next PC = { PC[31..28], target address, 00 }
• Understand where each part came from!
• Note: { , , } means concatenation
{ 4 bit
bits , 26 bits
bit , 2 bit
bits } = 32 bit address
dd
• { 1010,, 11111111111111111111111111,, 00 }
= 10101111111111111111111111111100
• Note: Book uses ||, Verilog uses { , , }
• We won’t actually be learning Verilog, but
it is useful to know a little of its notation
Other Jumps and Branches
decimal representation:
4 9 0 3
binary representation:
000100 01001 00000 0000000000000011
Questions on PC-addressing
• Branches
B h use PC-relative
PC l ti addressing,
dd i
Jumps use PC-absolute addressing.
Decoding Machine Language
• How do we convert 1s and 0s to C code?
Machine language C?
• For each 32 bits:
• Look at opcode: 0 means R-Format, 2 or 3
mean J J-Format,
Format otherwise II-Format.
Format
• Use instruction type to determine which
fields exist.
exist
• Write out MIPS assembly code, converting
eachh field
fi ld to
t name, register
i t number/name,
b /
or decimal/hex number.
• Logically
i ll convert this
hi MIPS coded into
i valid
lid
C code. Always possible? Unique? (No)
Decoding Example (1/7)
• Here are six machine language
instructions in hexadecimal:
00001025hex
0005402Ahex
11000003hex
00441020hex
20A5FFFFhex
08100001hex
• Let the first instruction be at address
4 194 304ten (0x00400000hex).
4,194,304 )
p convert hex to binary
• Next step: y
Decoding Example (2/7)
R 0 rs rt rd shamt funct
I 1, 4-31 rs rt immediate
J 2 or 3 target address
Decoding Example (3/7)
• Select the opcode (first 6 bits)
to determine the format:
Format:
R 00000000000000000001000000100101
R 00000000000001010100000000101010
I 00010001000000000000000000000011
R 00000000010001000001000000100000
I 00100000101001011111111111111111
J 00001000000100000000000000000001
• Look at opcode:
0 means R-Format,,
2 or 3 mean J-Format,
otherwise I-Format.
• Next step: separation of fields
Decoding Example (4/7)
or $v0,$0,$0
Loop: slt $t0,$0,$a1
beq $t0,$0,Exit
add
dd $
$v0,$v0,$a0
0 $ 0 $ 0
addi $a1,$a1,-1
j Loop
Exit: