The program counter - Start page at www.sm.luth.se

Download Report

Transcript The program counter - Start page at www.sm.luth.se

Basic Building Blocks
Multiplexer
Demultiplexer
Adder
+
Computer Engineering AddSub page 1
The Program Counter

There is a special register inside the processor.

Big enough to hold an instruction address (32 bits).

Called the program counter (PC).
Computer Engineering AddSub page 2
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Computer Engineering AddSub page 3
Fetch - Execute

Fetch:
– Send the value in the PC to the instruction memory.
– The instruction memory gives out one instruction.

Execute:
– Carry out the fetched instruction.
– Also: PC := PC+4;
Fetch
Execute
 108 times per second
Computer Engineering AddSub page 4
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Computer Engineering AddSub page 5
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Computer Engineering AddSub page 6
The Register File


32 word (32 bit) registers.
r0 is special:
– Read: always zero.
– Write: allowed, but won´t change it.

r31 is special:
– Hard-wired return address (lab1).
Computer Engineering AddSub page 7
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Computer Engineering AddSub page 8
Add Instructions

32 bit operands.

Example:
– Add rd rs rt.
– rd := rs + rt.
6
Opcode
5
5
rs
rt

There is also:
– Addu rd rs rt.

These are not add signed and add unsigned.
The “u”-variant ignores overflow.
5
rd
Computer Engineering AddSub page 9
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Add rd rs rt
Computer Engineering AddSub page 10
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Add rd rs rt
Computer Engineering AddSub page 11
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Add rd rs rt
Computer Engineering AddSub page 12
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Add rd rs rt
Computer Engineering AddSub page 13
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
Add
31
+
rd rs rt
Computer Engineering AddSub page 14
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Add rd rs rt
… next instr
Computer Engineering AddSub page 15
Sub Instructions

32 bit operands.

Example:
– Sub rd rs rt
– rd := rs - rt

There is also:
– Subu rd rs rt

These are not sub signed and sub unsigned
The “u”-variant ignores overflow
Computer Engineering AddSub page 16
How to Negate

Y := -X?

Sub rd $0 rt

rd := 0 - rt

Careful: Neg  Not
($0 means r0)
Computer Engineering AddSub page 17
Compare Instructions

Signed integers:
– Slt rd rs rt
– if rs < rt then
rd := 1
else
rd := 0

Unsigned integers:
– Sltu rd rs rt
– if rs < rt then
rd := 1
else
rd := 0
Computer Engineering AddSub page 18
Immediate Variants
of the arithmetic instructions:






Addi
Addiu
rt rs Imm
rt rs Imm
Slti
Sltiu
rt rs Imm
rt rs Imm
Imm sign-extend
No sub instruction
Computer Engineering AddSub page 19
Sign/Zero extension

The immediate field is 16 bits
Immediate

But most operations work on 32 bits!
Zero extension
0000000000000000
31
16 15
Sign extension
Immediate
xxxxxxxxxxxxxxxx x
0
31
Immediate
16 15
0
Bit 15, the Sign bit, is copied into bits 16 - 31
Computer Engineering AddSub page 20
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Computer Engineering AddSub page 21
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Addi rt rs Imm
Computer Engineering AddSub page 22
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Addi rt rs Imm
Computer Engineering AddSub page 23
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Addi rt rs Imm
Computer Engineering AddSub page 24
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
Addi
31
+
rt rs Imm
Computer Engineering AddSub page 25
Zero ext.
Branch
logic
0
A
ALU
4
B
+
Sgn/Ze
extend
31
+
Addi rt rs Imm
… next instr
Computer Engineering AddSub page 26