lc3 Intro
lc3 Intro
lc3 Intro
CS 135
CS 135 CS 135
+ Indicates instructions that modify condition codes + Indicates instructions that modify condition codes
1
LC-3 Overview: Memory and Registers LC-3 Overview: Instruction Set
•Memory •Opcodes
¾ address space: 216 locations (16-bit addresses) ¾ 15 opcodes
¾ Operate instructions: ADD, AND, NOT
¾ addressability: 16 bits
¾ Data movement instructions: LD, LDI, LDR, LEA, ST, STR, STI
•Registers ¾ Control instructions: BR, JSR/JSRR, JMP, RTI, TRAP
¾ temporary storage, accessed in a single machine ¾ some opcodes set/clear condition codes, based on result:
cycle ¾ N = negative, Z = zero, P = positive (> 0)
¾ accessing memory generally takes longer than a single cycle •Data Types
¾ eight general-purpose registers: R0 - R7 ¾ 16-bit 2’s complement integer
¾ each 16 bits wide •Addressing Modes
¾ how many bits to uniquely identify a register?
¾ How is the location of an operand specified?
¾ other registers ¾ non-memory addresses: immediate, register
¾ not directly addressable, but used by (and affected by) instructions
¾ memory addresses: PC-relative, indirect, base+offset
¾ PC (program counter), condition codes
CS 135 CS 135
CS 135 CS 135
2
Addressing Modes Data Movement Instructions
•Load -- read data from memory to register
• Where can operands be found? ¾ LD: PC-relative mode
¾ LDR: base+offset mode
1 ¾ LDI: indirect mode
•Want to specify address directly in the instruction •Used to alter the sequence of instructions
¾ But an address is 16 bits, and so is an instruction! (by changing the Program Counter)
¾ After subtracting 4 bits for opcode •Conditional Branch
and 3 bits for register, we have 9 bits available for address. ¾ branch is taken if a specified condition is true
•Solution: ¾ signed offset is added to PC to yield new PC
¾ Use the 9 bits as a signed offset from the current PC. ¾ else, the branch is not taken
¾ PC is not changed, points to the next sequential instruction
•TRAP
•Remember that PC is incremented as part of the FETCH phase; ¾ changes PC to the address of an OS “service routine”
•This is done before the EVALUATE ADDRESS stage. ¾ routine will return control to the next instruction (after TRAP)
CS 135 CS 135
3
Condition Codes Branch Instruction
•LC-3 has three condition code registers: •Branch specifies one or more condition codes.
N -- negative •If the set bit is specified, the branch is taken.
Z -- zero ¾ PC-relative addressing:
P -- positive (greater than zero) target address is made by adding signed offset (IR[8:0])
to current PC.
¾ Note: PC has already been incremented by FETCH
•Set by any instruction that writes a value stage.
to a register ¾ Note: Target must be within 256 words of BR instruction.
(ADD, AND, NOT, LD, LDR, LDI, LEA)
•If the branch is not taken,
the next sequential instruction is executed.
•Exactly one will be set at all times
¾ Based on the last instruction that altered a
register
CS 135 CS 135
CS 135 CS 135
4
NOT (Register) Operate Instructions
• ADD, AND
Must be all 1’s in bits [0:5]
• Addressing Mode?
Note: Src and Dst
could be the same register.
CS 135 CS 135
AND:
Dst= Src1 AND Src2
R2 = R1 AND R3
CS 135 CS 135
5
Using Operate Instructions Data Movement Instructions
CS 135 CS 135
ST 0011 SR PCoffset9
6
Basic Format Data Movement Instructions
•Load -- read data from memory to register
¾ LD: PC-relative mode
0010 DR or SR Address generation bits
¾ LDR: base+offset mode
¾ LDI: indirect mode
on
n s •Store -- write data from register to memory
io s
at dre
rm d ¾ ST: PC-relative mode
fo t a
in bi
d e 16 ¾ STR: base+offset mode
co a
en rm ¾ STI: indirect mode
e fo
he s to
T ow
h •Load effective address -- compute address,
save in register
¾ LEA: immediate mode
¾ does not access memory
CS 135 CS 135
CS 135 CS 135
7
ST (PC-Relative) Indirect Addressing Mode
CS 135 CS 135
CS 135 CS 135
8
Base + Offset Addressing Mode LDR (Base+Offset)
CS 135 CS 135
CS 135 CS 135
9
LEA (Immediate) Example
x30F7 0 0 0 1 0 1 0 0 0 1 1 0 1 1 1 0 R2 ← R1 + 14 = x3102
M[PC - 5] ← R2
x30F8 0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1 M[x30F4] ← x3102
x30F9 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 ← 0
x30FA 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 R2 ← R2 + 5 = 5
M[R1+14] ← R2
x30FB 0 1 1 1 0 1 0 0 0 1 0 0 1 1 1 0 M[x3102] ← 5
R3 ← M[M[x30F4]]
x30FC 1 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1 R3 ← M[x3102]
R3 ← 5
opcode
CS 135 CS 135
•TRAP
RTI 1000 000000000000
¾ changes PC to the address of an OS “service routine”
¾ routine will return control to the next instruction (after TRAP) TRAP 1111 0000 trapvect8
CS 135 CS 135
10
Condition Codes Branch Instruction
•LC-3 has three condition code registers: •Branch specifies one or more condition codes.
N -- negative •If the set bit is specified, the branch is taken.
Z -- zero ¾ PC-relative addressing:
P -- positive (greater than zero) target address is made by adding signed offset (IR[8:0])
to current PC.
¾ Note: PC has already been incremented by FETCH
•Set by any instruction that writes a value stage.
to a register ¾ Note: Target must be within 256 words of BR instruction.
(ADD, AND, NOT, LD, LDR, LDI, LEA)
•If the branch is not taken,
the next sequential instruction is executed.
•Exactly one will be set at all times
¾ Based on the last instruction that altered a
register
CS 135 CS 135
CS 135 CS 135
What happens if bits [11:9] are all zero? All one?
11
Program
R2 <- x3100
R1 <- 0
R4 <- 12 x3000 R2 <- x3100
x3001 R4 <- 0
x3002 R1 <- 0
YES x3003 R4 <- 12
R4 ?= 0 x3004 BRz x300A
x3005 R3 <- M[R2]
NO x3006 R1 <- R1 + R3
x3007 R2 <- R2 + 1
R3 <- M[R2]
R1 <- R1 + R3 x3008 R4 <- R4 - 1
Increment R2 x3009 BRnzp x3004
Decrement R4
CS 135 CS 135
CS 135 CS 135
12
TRAP Instruction TRAP
CS 135 CS 135
CS 135 CS 135
+ Indicates instructions that modify condition codes
13
LDR+ 0110 DR BaseR offset6 Von Neumann Model: Outline
LEA+ 1110 DR PCoffset9
CS 135 CS 135
+ Indicates instructions that modify condition codes
CS 135 CS 135
14
LC-3 Data Path gateMARMUX gatePC
3
16
DR REG
MARMUX
MARMUX LD.PC PC
16
PCMUX +1 FILE
16 16 2 LD.REG
PCMUX
SR2 SR1
Combinational ZEXT
16
SR2
3
OUT OUT
3
SR1
Logic +
ADDR2MUX
[7:0]
2
ADDR1MUX
ADDR2MUX ADDR1MUX 16 16
16
16 16 16 16
[10:0] 16
SEXT
0
Storage SEXT
16
[8:0] [4:0]
SEXT
FINITE SR2MUX
STATE
[5:0] SEXT
N Z P LD.CC MACHINE B A
2
R ALU
LD.IR IR LOGIC
gateALU
16 16 16
State Machine
16 GateMDR 16
16
LD.MDR LD.MAR
MDR MEMORY MAR INPUT OUTPUT
CS 135 CS 135
MEM.EN, R.W
¾ memory: MAR, MDR (also control signal for read/write) ¾ Two 16-bit outputs
¾ used by ALU, PC, memory address
¾ data for store instructions passes through ALU
CS 135 CS 135
15
Data Path Components Data Path Components
CS 135 CS 135
CS 135 CS 135
16
Implementing the Control Logic
CS 135 CS 135
CS 135 CS 135
17