Program Counter

Download Report

Transcript Program Counter

GCSE Computing – the LMC
Candidates should be able to:

describe the characteristics of an assembler
© GCSE Computing
Slide 1
What is the LMC?


LMC stands for Little Man Computer.
The LMC is a CPU simulator that models a simple computer using the
von Neumann architecture and memory use









a central processing unit consisting of an arithmetic logic unit and registers
a control unit containing an instruction register and program counter
input and output mechanisms
RAM to store both data and instructions
external secondary storage
The simulation uses the idea of a ‘Little Man’ inside the computer fetching
instructions from RAM and executing them.
The LMC can be programmed directly by entering machine code (but in
decimal) directly into RAM.
However, the LMC is usually programmed in assembly code.
An assembler then translates the assembly code into machine code
(but in decimal) and loads it into RAM.
© GCSE Computing
Slide 2
What are the components of the LMC?







RAM consisting of 100 memory addresses (0-99).
Each address can hold a decimal format number
up to 999.
INPUT using a 0-9 digit keypad.
OUTPUT using a multi-line display.
A PROGRAM COUNTER that stores the address
of the next instruction in RAM.
An INSTRUCTION REGISTER that stores the
OPP CODE of the current instruction.
An ADDRESS REGISTER that stores the address
part of the current instruction (if it has one).
An ACCUMULATOR that stores the results of any
calculations.
© GCSE Computing
Slide 3
Programming the LMC

As the simulation runs the ‘Little Man’ inside the computer carries out the
following steps:
1. Check the Program Counter to find the RAM address with the
program instruction to be fetched.
2. Fetch the INSTRUCTION from that RAM address.
3. Increment the Program Counter (so that it contains the RAM address
of the next instruction).
4. Decode the instruction (this might include finding the RAM address for
the data it will work on).
5. If necessary, fetch the DATA from the RAM address found in the
previous step.
6. Execute the instruction.
7. Repeat the cycle or halt.
© GCSE Computing
Slide 4
Parts of the LMC – before code is assembled
A program in
ASSEMBLY
LANGUAGE
ready to be
translated into
machine code
INPUT,
allowing
number
data input
© GCSE Computing
OUTPUT,
displaying
number
data
output
Memory
addresses
0-99
An explanation of
the instruction to
be executed next
Slide 5
Parts of the LMC – after code is assembled
The
PROGRAM
COUNTER
The
program in
ASSEMBLY
LANGUAGE
without
LABELS
The contents of
the
ACCUMULATOR
© GCSE Computing
The
INSTRUCTION
REGISTER,
containing
the current
INSTRUCTION
CODE
The ADDRESS REGISTER,
containing the ADDRESS
that the current
instruction code refers to
Slide 6
Parts of the LMC – after code is assembled
The
program in
ASSEMBLY
LANGUAGE
DATA
stored in
RAM
MACHINE
CODE
instructions
stored in
RAM
DATA
© GCSE Computing
Slide 7
The LMC – using labels with data


Labels can be used to label a memory address that contains DATA. Being
able to refer to the data as a label is much easier than having to refer to the
address the data is stored at.
Example:

data1 DAT


data1 DAT 50


Explanation: The memory address where this data is stored is labelled data1 and
stores the data 50. If this was the 7th line of assembly code to be compiled then the
data 50 would be stored at memory address 6 (RAM addresses start at 0)
STA data1


Explanation: The memory address where this data is stored is labelled data1. No data
would initially be stored at this location. If this was the 7th line of assembly code to be
compiled then the label would refer to memory address 6 (RAM addresses start at 0)
Explanation: The contents of the accumulator would be stored at memory address 6.
LDA data1

Explanation: The data which is stored at memory address 6 would be loaded into the
accumulator.
© GCSE Computing
Slide 8
The LMC – using labels with branches


Labels can be used to label a memory address that contains an
INSTRUCTION. Being able to refer to the label rather than the address of
the instruction makes it much easier when using BRANCH instructions
such as BRA, BRP and BRZ.
Example:
 loop1 INP


Explanation: The memory address where this instruction is stored would
be labelled loop1.
BRZ loop1



© GCSE Computing
Explanation: If the contents of the accumulator were zero, the program
would branch to the memory address labelled loop1 and carry out the
instruction there.
To achieve this, the Program Counter would be set to the memory
address labelled loop1.
If the contents of the accumulator were not zero then the Program
Counter would simply be incremented by one.
Slide 9
The LMC – how labels are used
Labels used
with BRANCH
instructions
Labels used
with DATA
© GCSE Computing
Slide 10
Running a program using the LMC

1.
2.
3.
4.
5.
6.
7.
After a program is assembled into machine code the
Program Counter is always reset to memory address 0.
The ‘Little Man’ checks the Program Counter to find the RAM
address with the instruction to be fetched (in this case address
0).
Here, the instruction 523 is fetched from RAM address 0.
The Program Counter is incremented (to RAM address 1).
The instruction part (5) is loaded into the INSTRUCTION
REGISTER and the address part (23, the RAM address for the
data the instruction will work on) is loaded into the ADDRESS
REGISTER.
The instruction is then decoded.
The instruction is executed (5 = LOAD into ACCUMULATOR so
the 0 stored at RAM address 23 is loaded into the ACCUMULATOR).
Back to step 1 to repeat the cycle until a HALT instruction is
reached.
© GCSE Computing
Slide 11
INPUT and OUTPUT using the LMC


An instruction starting with a 9 (901 or 902) means the instruction is
an INPUT/OUTPUT command.
The ‘address’ part of the instruction (01 or 02) decides if the
instruction is actually an INPUT or an OUTPUT.
 01 means INPUT - entered using the number keypad and stored
in the ACCUMULATOR when the Enter key is pressed.
 02 means OUTPUT – the value stored in the ACCUMULATOR is
passed to the OUTPUT box.
© GCSE Computing
Slide 12
The LMC instruction set
Instruction
Mnemonic
Machine Code
Load
LDA
5xx
Store
STA
3xx
Add
ADD
1xx
Subtract
SUB
2xx
Input
INP
901
Output
OUT
902
End
HLT
000
Branch always
BRA
6xx
Branch if zero
BRZ
7xx
Branch if zero or positive
BRP
8xx
Data storage
DAT
© GCSE Computing
NOTE: xx
represents a
memory
address
between 0
and 99.
Slide 13