0% found this document useful (0 votes)
30 views33 pages

Chapter 4

Uploaded by

Eshaal Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views33 pages

Chapter 4

Uploaded by

Eshaal Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 4: Processor Fundamentals

Internal and external hardware components of a computer

Modern computing (arguably) started in 1822 when Charles Babbage, a British Mathematician,
proposed 'the difference engine'. This was a mechanical machine that could calculate
numbers from given inputs. Unfortunately Babbage never got enough funding to realise his
plans and there was no Victorian Computing Revolution, however, you can see a completed
modern version in the Science Museum in London (along with half of Babbage's brain!).

m
Since Babbage there have been several different designs of computers, and the one we are
going to focus on here is called the 3-Box Model, or Von Neumann machine. In this machine:

.co
• All data and instructions are stored in the Main Memory
• Instructions are sent to the Processor along the System Bus to be executed
• Any input and output (such as printing and entering instruction) is performed by I/O
devices with the data travelling from the I/O devices to the Processor and Main

ail
Memory by means of the System Bus:

gm
id@
Von Neumann's Architecture

Consider a program stored on a DVD, to get the machine to run it, you will have to input
ah

the data from the DVD to the memory using the system bus. Once the program is loaded into
memory the instructions it will be sent to the CPU line by line using the system bus and
executed there. Any things to be printed or shown on a screen will be sent to the Output
box.
az

We'll now look in more detail at these components:

The processor (or Central Processor Unit - CPU) is one of the most complex parts of any
hr

computer system. The processor executes programs and supervises the operation of the rest
of the system. Single chip processors are otherwise known as microprocessors. Gordon E
Moore theorized that the number of transistors that could be integrated onto the chip
would double every 18–24 months, most modern processors will contain billions of
za

transistors. Multicore microprocessors are now very popular, where the processor will have
several cores allowing for multiple programs or threads to be run at once.

  1  
om
Main Memory

il.c
Main memory - data store that can be directly addressed by the CPU
Main memory is used to store program instruction and data, using the System Bus to
communicate with CPU. Main memory is often created using Random Access Memory (or RAM) or
Read Only Memory (ROM). Modern computers will have gigabytes of RAM, meaning that large

ma
programs can run and multiple programs can run at once. The more main memory that you have
the larger the number of programs you can run at
once.
@g
Main memory is often made up of RAM modules, where
you can adjust the amount by swapping in higher
capacity modules or adding
Address Contents more modules
id
Main memory consists of data
stored in addresses, in
1024 Cabbage
general, the more main memory
ah

you have the more addresses


you'll have and vice versa.
1025 Celery

In the above example if we were to perform the following assembly


az

code instruction:
1026 Courgette

LDA 1026 ;LOAD memory location 1026


hr

1027 Carrot
This would return the word: "Courgette"

If we were to perform the following assembly code instruction:


1028 Cucumber
za

STO "Beetroot", 1025 ;STORE data given into memory location 1025
1029 Chard
This would change the value stored in memory location 1025 from
"Celery" to "Beetroot"

  2  
ROM and RAM

RAM - Random Access Memory can be read from and written to. Data is cleared when the
power is off

ROM - Read Only Memory can only be read from, data is maintained when the power is off

The two main types of main memory are ROM and RAM. Whilst RAM might be several gigabytes

om
in size, ROM will often be a few kilobytes. As ROM is read only memory, it tends to store
core software instructions such as the code needed to load the Operating System into RAM
(known as bootstrapping) or change the bios. RAM is much larger and stores the code to run
the operating system and programs that you run on your computer. When you load a disc into
a games console, the code won't do anything until it has loaded from the disc into the

il.c
system RAM, that's why you see a loading screen.

Exercise: Central Processing Unit

Give a definition for main memory:

ma
Answer :

Main memory is a data store that can be directly addressed by the CPU
@g
What is the difference between ROM and RAM?

Answer :

ROM is Read Only Memory, RAM is random access memory. This means that ROM cannot be
id

written to, it can only be read from. RAM can be both read from and written to.

Name the four components of Von Neumann Architecture


ah

Answer :

Processor
az


• Main memory
• I/O devices
• System bus
hr
za

  3  
System Bus

A Bus is a connection between different devices. This connection will normally consist of
multiple wires along which signals, instructions and data will be carried. In Von Neumann
Architecture there is a single bus to manage the connection between the three main
components. The System Bus consists of 3 separate buses, each with a specific task that
you need to know. This three-bus model is an expansion of the Von Neumann architecture
showing greater detail.

om
• The flow chart has an error. The arrow going to 'Input and Output' from 'Address
Bus', shouldn't be bi-directional.

il.c
ma
id @g
ah

Address Bus
az

A single-directional bus that carries address signals from the CPU to Main Memory and
I/O devices. This might involve the CPU requesting some data from Main Memory, sending the
address of the data to Main Memory, then Main Memory returning the data along the data
bus. Let's take a look at some code:
hr

LDA 23
za

This code is asking to load the data from memory address 23 into the CPU, the address bus
does not send addresses to the processor, but only sends them from the processor. To do
this the CPU would send 23 along the Address Bus, and the value from memory location 23
would be sent along the Data Bus back to the CPU. The size of the Address Bus can dictate
how much Main Memory you can have in your system. For example, if you had an Address Bus
of 3 bits, then:

  4  
Maximum value = 111 = 7
Range of values = 000
001
010
011
100
101
110
111

om
This would mean that your Main Memory could only have 8 different addressable blocks

Data bus

il.c
A bi-directional bus, typically consisting of 32 wires, used to transport data and
instructions between the three components of the three-box model. The larger the Data Bus
the more data can be transported at one time. For example if we have an 8 bit Data Bus,
the maximum value we could send along the Bus would be:

ma
1111 1111 = 255

The larger the Data Bus the more data we can send at once and the more complex
instructions we can use.
@g
Control bus

A bi-directional bus, typically consisting of more than 16 wires, used to transport


control signals between the three components of the three-box model. The control bus is
id
used to carry important information such as messages to say when a device has finished a
job or when a device has just been plugged in. A simple example would be when you plug in
your USB key and after a few moments a screen pops up asking you what you want to do with
it. The control bus also contains interrupt signals, which allow devices (printers,
ah

keyboards, disks, etc.) to signal that they have finished a request. The CPU temporarily
suspends its current program, services the device and then resumes the previous program.
az
hr
za

  5  
Exercise: Central Processing Unit

Complete the following table:

Name Direction Description


Address Bus
Data Bus
Control Bus

om
Answer :

Name Direction Description

il.c
Address Bus uni-directional Carries addresses from the CPU to main memory or other devices
Data Bus bi-directional Transports data and instructions
Control Bus bi-directional Transports control signals

ma
What is the largest number the following width data buses can carry at one time:

width 4 wires

Answer :
@g
width 6 wires
id
Answer :
ah

width 10 wires
az

Answer :
hr

How many addresses can the following width addresses buses address:

width 4 wires
za

Answer :

width 5 wires

  6  
Answer :

width 8 wires

Answer :

om
An address bus that can address a maximum of 2GB memory is 31 wires in width. How many
wires would it need if it were to address a maximum of 4GB of memory?

il.c
Answer :

32 as bytes or 4GB of memory

ma
But you don't have to be really good at math to answer this. 4GB is twice 2GB and we know
that 2^x is half the size of 2^x+1.

Peripherals
@g
Input/Output devices are used by the system to get information in and out, as they are not
internal but are connected to the CPU, we refer to them as peripherals (your hands are
peripheral to your torso). We'll cover the specific ones you need to learn a little later,
but for the moment you need to know the fundamental difference:
id
• Input Devices - used to get information into the system. E.g. Keyboard
• Output Devices - used to send information out of the system. E.g. Visual Display
Unit (VDU)
ah

If you look at the Von Neumann Architecture notice that it doesn't mention Keyboard or
display, this is a very smart move as you don't want to force every computer to have a
keyboard (think about a games console, there is no keyboard on that) or a VDU (some
az

computer such as MP3 players don't have a screen). However, some computer architecture
does include specific I/O controllers:

I/O controllers
hr

An I/O controller is an electronic circuit that connects to a system bus and an I/O
device; it provides the correct voltages and currents for the system bus and the I/O
za

device. Examples would include:

• keyboard controller, attached to a keyboard


• disk controller for a Hard Disk (Hard Disks are not main memory!)
• video display controller, attaching a video display unit (monitor)

  7  
I/O ports

I/O ports is a complementary method of performing input/output between the CPU and
peripheral devices in a computer. This allows I/O devices to be connected to the CPU
without having to have specialist hardware for each one. Think about the USB port on your
computer, you can connect Keyboards, Mice, Game pads, Cameras, Phones, etc. and they all
connect using the same port.

Secondary storage

om
Main memory can be very expensive and you often require storing data that you won't use
constantly. Think about a computer game that you haven't played for a couple of months.
The last thing you want to do is to store this code in main memory taking up all that
precious and expensive space. To get past this issue we use secondary storage. This is

il.c
normally inexpensive data storage sitting external to the CPU, connected through an I/O
controller, that we can use as and when we need. Secondary Storage will store data
permanently, without the need for the electricity to remain always on (Think about a USB
key, it doesn't need to be plugged in to keep its data). So taking the game example again,
we only load the game into main memory (maybe from a DVD or hard disk), as and when we

ma
need it.

Examples of secondary storage include:


@g
• Hard Disk drive
• USB thumb drives
• CD-ROM / DVD / Blu-ray
• Tape drives
id

A hard disk drive with protective cover


removed.
ah

Exercise: I/O and Peripherals

Name 2 input peripherals:


az

Answer :

• Keyboard
Mouse
hr


• Scanner
• Camera
• Microphone
za

• Games pad

Name 2 output peripherals:

Answer :

• Speakers

  8  
• Printer
• Visual Display Unit

Name two ways to connect peripherals to a CPU:

Answer :

• I/O controller
• I/O ports

om
Name 2 secondary storage devices:

Answer :

il.c
• Hard Disk
• USB thumb stick
• CD-ROM / DVD / Blu-Ray

Name 2 I/O controllers

ma
Answer :

• Hard Disk controller


Keyboard controller
@g

• Visual Display Unit Controller

Addressable memory
A computer must be able to access main memory for reading and writing, they do this by
id

using addressable memory. Main memory is a little like a set of school lockers, each with
a different number. Each locker contains a block of data and if you fill up one locker you
can use the next locker to expand into.
ah
az
hr

Looking at the example above you can see locker '0' contains '8975', whilst lockers 1 to 6
contain the sentence "The Cat sat on the dog!". Locker '7' is empty, locker '8' contains a
za

Boolean value and locker '9' contains the number 48. As you can see if we only used one
character for the locker number then we could only ever have 10 lockers. If you limit the
number of addresses you can use then you limit the amount of memory you can talk to. If
you have a small address bus then you won't be able to have much main memory.
The way that data is stored in a computer is very similar:

  9  
om
il.c
ma
Stored program concept
stored program concept - a program must be in main memory in order for it to be
executed. The instructions are fetched, decoded and executed one at a time
Building on the Von Neumann architecture we get the idea of how the stored program concept
@g
works. If you have ever loaded a game on a console you might notice that:
1. you need to insert a disc
2. the disc whirrs
3. the game says loading
4. the game plays
id
This is the stored program concept in motion! Let's take apart what is happening:
1. You insert an optical disk (secondary storage) with the code on
2. The code is loaded (whirrs) into main memory
ah

3. The processor fetches, decodes and executes instructions from main memory to play
game
az
hr
za

  10  
Exercise: Characteristics of a processor

How many different addresses can an 8 line address bus address?

Answer :

How does the address bus affect main memory?

om
Answer :

If you have a small address bus then you will be limited in the number of addresses you
can talk to and therefore how much main memory you can directly address.

il.c
How wide would the address bus have to be to talk to 1024 addresses?

Answer :

ma
10 lines wide since

What is wrong with using a 9 bit address bus but having 700 memory locations in main
memory?
@g
Answer :

We can only address different locations. It wouldn't be able to talk to


id

address locations .

Define the stored program concept:


ah

Answer :

A program must be (resident) in main memory in order for it to be executed. The


az

instructions are fetched from main memory, then decoded and executed in the CPU.

Arithmetic logic unit


hr

A simple example of an arithmetic logic unit (2-bit ALU) that does AND, OR, XOR, and
za

addition
The Arithmetic Logic Unit or the ALU is a digital circuit that performs arithmetic and
logical operations. Where arithmetic operations include things such as ADD and SUBTRACT
and the logical operations include things such as AND, OR, NOT.

  11  
The ALU is a fundamental building block in the central processing unit (CPU) of a computer
and without it the computer wouldn't be able to
calculate anything! Some examples of assembly code
instructions that would use the ALU are as follows
(not all processors will have all these
instructions):
ADD ;add one number to another number
SUB ;subtract one number to another number
INC ;increment a number by 1

om
DEC ;decrements a number by 1
MUL ;multiply numbers together
OR ;Boolean algebra function
AND ;Boolean algebra function
NOT ;Boolean algebra function

il.c
XOR ;Boolean algebra function
JNZ ;jump to another section of code if a number
is not zero (used for loops and ifs)
JZ ;jump to another section of code if a number
is zero (used for loops and ifs)

ma
Control unit
The control unit sits inside the CPU and coordinates the input and output devices of a
computer system. It coordinates the fetching of program code from main memory to the CPU
@g
and directs the operation of the other processor components by providing timing and
control signals.

Clock
Processor clock - A timing device connected to the processor that synchronises when the
id
fetch, decode execute cycle runs
Your computer might contain several clocks that each regulate different things. The clock
we are going to look at here will keep the processor in line. It will send the processor a
signal at regular times telling it to start the fetch decode execute
ah

routine.

Lights flash at frequency f = 0.5 Hz (Hz = hertz), 1.0 Hz and 2.0 Hz,
where Hz means flashes per second.
az

Clock speed - The number of cycles that are performed by the CPU per
second
Clock speed is measured in Hertz, which means 'per second'. You have
probably heard of clock speeds such as 1 MHz, this means 1,000,000
hr

cycles per second and potentially a million calculations. A computer


of speed 3.4 GHz means it might be capable of processing 3,400,000,000
instructions per second! However it isn't as simple at that, as some
za

processors can perform more than one calculation on each clock cycle,
and processors from different manufacturers and using different
architecture are often difficult to compare. Also with the increase in
multi-core processors such as the PS3 (7 cores) and the Xbox 360 (3 cores) there might be
times where the clock might be ticking but there is nothing for the processor to
calculate, the processor will then sit idle.

  12  
General purpose and dedicated registers
Registers - a small amount of fast storage, which is part of the processor
For immediate calculations, using main memory is too slow. Imagine having to send a signal
along the address bus and some data along the data bus when all you want to do is store
the result of adding two numbers together. The distance between the processor and main
memory, even though it might be a few centimeters, is far enough for the signal to take a
significant time to get there. To get past this issue there are small amounts of memory
stored inside the processor itself, these are called registers. Registers are incredibly
fast pieces of memory that are used to store the results of arithmetic and logic

om
calculations.
Different processors will have different sets of registers. A common register is the
Accumulator (acc), which is a data register, where the user is able to directly address
(talk to) it and use it to store any results they wish. Processors may also have other
registers with particular purposes:

il.c
5. General purpose register - allow users to use them as they wish
6. Address registers - used for storing addresses
7. Conditional registers - hold truth values for loop and selection
There are also 4 registers in particular that you need to know, we'll meet them in more
detail in the next chapter:

ma
4. Program Counter (PC) - an incrementing counter that keeps track of the memory
address of which instruction is to be executed next...
5. Memory Address Register (MAR) - holds the address in memory of the next
instruction to be executed
@g
6. Memory Buffer Register (MBR) - a two-way register that holds data fetched from
memory (and ready for the CPU to process) or data waiting to be stored in memory
Current Instruction register (CIR) - a temporary holding ground for the instruction
that has just been fetched from memory

Exercise: Structure and role of the processor


id

Give a description of the Arithmetic Logic Unit:


ah

Answer :

The Arithmetic Logic Unit or the ALU is a digital circuit that performs arithmetic and
logical operations.
az

What does 3MHz mean:

Answer :
hr

3,000,000 clock cycles per second


za

What does a processor clock do:

Answer :

Synchronises the operation of the processor

What are registers:

  13  
Answer :

a small amount of fast storage, which is part of the processor

Name 3 registers used by the processor and explain what each does:

Answer :

• Program Counter (PC) - an incrementing counter that keeps track of the memory

om
address of which instruction is to be executed next...
• Memory Address Register (MAR) - holds the address in memory of the next instruction
to be executed
• Memory Buffer Register (MBR) - a two-way register that holds data fetched from
memory (and ready for the CPU to process) or data waiting to be stored in memory

il.c
• Current Instruction register (CIR) - a temporary holding ground for the instruction
that has just been fetched from memory
• General purpose registers -
• Accumulator - Used to store results of calculations

ma
Increasing performance
If we want to increase the performance of our computer, we can try several things
8. Increasing the clock speed
9. Adjusting word length
@g
10.Increasing bus widths
For each different method we are going to look at these old games consoles to see how
performance increase was achieved:
Word
System Year Speed Notes
size
id
NES 1983 1.79 MHz 8 bit
SNES 1990 3.58 MHz 16 bit
Nintendo
1996 93.75 MHz 64 bit
ah

64
cooling fan
Gamecube 2001 486 MHz 128 bit
introduced
az

Clock speed
Clock speed - The number of cycles that are performed by the CPU per second
hr

The most obvious way to increase the speed of a computer would be to increase the speed of
the computer clock. With a faster clock speed the processor would be forced to perform
more instructions per second.
za

Example: Clock Speed


As you can see on the console table above, each successive console showed an increase in
clock speed. A clock speed of 800 MHz is twice as fast as a clock speed of 400 MHz,
meaning it should be able to calculate twice as many calculations in a given time.

  14  
But what is to stop us increasing the clock speed as much as we want? If you study Physics
you might already know this, but the problem with increased clock speed is that an
increased current will have to flow through the circuits. The more current that flows, the
hotter things get. You might notice that a laptop will get hot or even your mobile phone
when you are doing something processor intensive like playing a game. The faster the clock
speed, the hotter the processor runs. To counter this computer scientists have come up
with smarter chip designs and introduced heat sinks, fans, and even liquid cooling into
computers. If a processor runs too hot it can burn out!
NES processor Heat sink CPU fan water cooling

om
il.c
ma
Metal contacts are
Draws the
placed on
heat away
Fan draws cold air the cpu drawing heat
No need for fans or heat from the
over the cpu to cool away, water
sinks processor,
it then passes over these
which sits
@g
contacts to
beneath
draw heat away

Word size
id
Word size - The number of bits of information that a processor can process at one time
Another way to increase the performance of a computer is to increase the word size. This
means increasing the number of bits a computer can process at one time. As you can see
ah

from our console table, increasing word size was a big part of creating faster consoles,
they even named a console the N64 to boast about its word size. With a larger word,
computers can handle larger or more precise calculations and do more complicated things.
Modern computer mostly have 32 or 64 bit word sizes, with specialist hardware such as
az

games consoles being able to handle up to 128bit words.

Bus size
Bus Size - The number of bits of information a bus can carry at one time (the number of
hr

wires making up a bus)


Now that we understand what word size is. Imagine that you have a processor able to
understand 32 bit words at a single time. This is pretty standard. But what happens if the
bus sending the words from memory to the processor was only 8 bits wide? We'd get a bottle
za

neck. It would involve four chunks of data to be sent along the Data Bus before we had a
word for the processor to execute. In other words, to increase performance we must also
increase the bus size to avoid bottle necks:

  15  
Example: Bus Size
Imagine our friend can understand words of 28 bit length and we are sending the word:
antidisestablishmentarianism to them. If the link between us (the bus), say a chat window
on a website, only allowed for 4 letters at a time (the bus width). We'd have to send the
following:
anti
dise
stab
lish

om
ment
aria
nism
Requiring seven data sends before our friend is ready to process the word we sent them. If
we were able to send more letters at once, through a larger bus width, then our friend

il.c
wouldn't have to wait around until they had received all the data.

Exercise: Increasing Processor Performance

ma
Name three ways to increase Processor performance:

Answer :

Increase clock speed


@g

• Adjust word length
• Increase bus width

What draw back might increasing clock speed bring?


id
Answer :

Processor might need extra cooling hardware to stop it over heating


ah

What is a benefit of increasing word length?

Answer :
az

Computers can perform more complex instructions in one go, dealing with larger numbers and
greater number accuracy
hr

How might bus width impact on the speed of a computer?

Answer :
za

If the bus width is smaller than the word size, then the CPU will have to wait around
whilst the bus delivers data and instructions to it.

  16  
How does it all fit together?
A very common exam question is to name the components of a computer architecture diagram.
Now we have met the processor, buses and various other computer components we can start to
answer questions like the following:
Example: Name the component
Match the following components to the numbers on the diagram: Processor, Data bus, Control
Bus, Main memory, Keyboard, Secondary storage, Address bus, Clock, Monitor, VDU
controller, disk controller, keyboard controller

om
il.c
ma
@g
Don't worry about the size of this problem, we are going to tackle it bit by bit. the
id
first step is to remember the differences between each of the components:
7. Processor - connected to other devices using buses
8. Data bus - bi-directional connection between devices
9. Main memory - internal to the computer and linked through the buses
ah

10.Keyboard - external to the computer, an input device


11.Secondary storage - external device, an input and output device
12.Address bus - uni-directional connection between devices
13.Clock - regulates the processor
az

14.Monitor - external output device


15.VDU controller - connects system to external monitor
16.Disk controller - connects system to external secondary storage
17.Keyboard controller - connects system to external keyboard device
hr

18.Control Bus - A bi-directional bus used to control signals between the components
Now we have remembered what each device does, can you label them all?
za

Answer :

1. Clock
2. Processor
3. Main memory
4. keyboard controller
5. VDU controller

  17  
6. Disk controller
7. Data bus (or Control bus)
8. Control bus (or Data bus)
9. Address bus
10.Keyboard
11.Monitor
12.Secondary storage

Exercise: System Diagrams

om
For the following diagram where applicable add single or multi directional connections
between devices and buses

il.c
ma
@g
Answer :
id
ah
az
hr
za

  18  
Machine code
Machine code - simple instructions that are executed directly by the CPU
As we should hopefully already know, computers can only understand binary, 1s and 0s. We
are now going to look at the simplest instructions that we can give a computer. This is
called machine code.
Machine code allows computers to perform the most basic, but essential tasks. For this
section we are going to use the Accumulator (you met this register earlier) to store the
intermediate results of all our calculations. Amongst others, the following instructions
are important for all processors:

om
11.LDA - Loads the contents of the memory address or integer into the accumulator
12.ADD - Adds the contents of the memory address or integer to the accumulator
13.STO - Stores the contents of the accumulator into the addressed location
Assembly code is the easy to read interpretation of machine code, there is a one to one
matching, one line of assembly equals one line of machine code:

il.c
Assembly
Machine code
code
000000110101 = Store 53
Let's take a look at a quick coding example using assembly code.

ma
LDA #23 ;loads the number 23 into the accumulator
ADD #42 ;adds the number 42 to the contents of the accumulator = 65
STO 34 ;save the accumulator result to the memory address 34
The code above is the equivalent of saying x = 23 + 42 in VB.NET.
@g
Instruction set
Instruction set - the range of instructions that a CPU can execute
There are many different instructions that we can use in machine code, you have already
met three (LDA, ADD, STO), but some processors will be capable of understanding many more.
The selection of instructions that a machine can understand is called the instruction set.
id
Below are a list of some other instructions that might be used:
ADD ;add one number to another number
SUB ;subtract one number to another number
ah

INC ;increment a number by 1


DEC ;decrement a number by 1
MUL ;multiply numbers together
OR ;boolean algebra function
az

AND ;boolean algebra function


NOT ;boolean algebra function
XOR ;boolean algebra function
JNZ ;jump to another section of code if a number is not zero (used for loops and
hr

ifs)
JZ ;jump to another section of code if a number is zero (used for loops and ifs)
JMP ;jump to another section of code (used for loops and ifs)
Let us look at a more complex example of assembly code instructions:
za

19.LDA #12 ;loads the number 12 into the accumulator


20.MUL #2 ;multiplies the accumulator by 2 = 24
21.SUB #6 ;take 6 away from the accumulator = 18
22.JNZ 6 ;if the accumulator <> 0 then goto line 6
23.SUB #5 ;take 5 away from the accumulator (this line isn't executed!)
24.STO 34 ;saves the accumulator result (18) to the memory address 34
  19  
You'll notice that in general instructions have two main parts:
• opcode - instruction name
• operand - data or address

Depending on the word size, there will be different


numbers of bits available for the opcode and for the
operand. There are two different philosophies at play,
with some processors choosing to have lots of different

om
instructions and a smaller operand (Intel, AMD) and
others choosing to have less instructions and more
space for the operand (ARM).
• CISC - Complex Instruction Set Computer - more
instructions allowing for complex tasks to be executed, but range and precision of

il.c
the operand is reduced. Some instruction may be of variable length, for example
taking extra words (or bytes) to address full memory addresses, load full data
values or just expand the available instructions.
• RISC - Reduced Instruction Set Computer - less instructions allowing for larger and
higher precision operands.

ma
Exercise: Instruction sets
What is the instruction set:
Answer :
@g
Name and explain the two parts that make up an machine code instruction:
Answer :
id

For a word with 4 bits for an opcode and 6 bits for an operand
• How many different instructions could I fit into the instruction set?
ah

• What is the largest number that I could use as data?


Answer :
az

For a 16 bit word with 6 bits for an opcode


• How many different instructions could I fit into the instruction set?
• What is the largest number that I could use as data?
hr

Answer :
za

Why might a manufacturer choose to increase the instruction set size?


Answer :

  20  
What might be the problem with increasing the space taken up by the opcode?
Answer :

Give two benefits for increasing the word size of a processor?


Answer :

om
Addressing modes
You might notice that some instructions use a # and others don't, you might even have an
inkling as to what the difference is. Well here is the truth:

il.c
# = number
[no hash] = address
Let's take a look at a quick example:

Assembly code Main memory start Main memory end

ma
LOAD #10 Address Contents Address Contents
ADD #12 10 9 10 9
STORE 12 11 2 11 2
12 7 12 22
@g
13 10 13 10
14 12 14 12

This code loads the number 10 into the accumulator, then adds the number 12, it then
stores the result 22 into memory location 12.
id
Let's take a look at doing this without the hashes:
There are many types of codes addressing modes. But we only need to know 3, they are:
Assembly code Main memory start Main memory end
LOAD 10 Address Contents Address Contents
ah

ADD 12 10 9 10 9
STORE 12 11 2 11 2
12 7 12 16
13 10 13 10
az

14 12 14 12

This code loads the value stored in memory location 10 into the accumulator (9), then
hr

adds the value stored in memory location 12 (7), it then stores the result into memory
location 12 (9 + 7 = 16).
za

Addressing
Symbol Example Description
Mode
Memory
LOAD 15 15 is treated as an address
Location
Integer # LOAD #15 15 is treated as a number
Some instruction don't need operands such as halting
Nothing HALT
a program
  21  
Exercise: Assembly code and Addressing modes

For the following memory space, what would it look like after executing the assembly code
below:

Content
Address
s
10 1

om
11 4
12 4
13 100
14 5

il.c
LOAD 14
ADD #12
STORE 12
Answer :

ma
For the following memory space, what would it look like after executing the assembly code
below:
Content
Address
@g
s
211 6
212 3
213 78
214 21
id
LOAD #100
STORE 213
LOAD 214
ah

ADD 213
STORE 214
Answer :
az

For the following memory space, what would it look like after executing the assembly code
below:
Content
Address
s
hr

99 6
100 6
101 8
za

102 9
LOAD 100
ADD 101
DIV #7
STORE 102
Answer :
Write some assembly code to do the following:

  22  
34 + 35 and store in memory location 100
Answer :

Write some assembly code to do the following:


4 + (100 / 2) and store in memory location 100

om
Answer :

il.c
List and give examples of three addressing modes:

ma
Answer : @g
Machine code and instruction sets
There is no set binary bit pattern for different opcodes in an instruction set. Different
processors will use different patterns, but sometimes it might be the case that you are
id
given certain bit patterns that represent different opcodes. You will then be asked to
write machine code instructions using them. Below is an example of bit patterns that might
represent certain instructions.
ah

Machine Addressing
Instruction Hexadecimal Example
code mode
0000 STORE Address 0 STO 12
az

0001 LOAD Number 1 LDA #12


0010 LOAD Address 2 LDA 12
0100 ADD Number 4 ADD #12
hr

1000 ADD Address 8 ADD 12


1111 HALT None F HALT
Exercise: Machine Code
Using the table above provide machine code to do the following:
za

LOAD 12
ADD #6
Answer :

  23  
Using the table above give the assembly code for the following machine code:
0001 00000111
0100 00001001
0000 00011110
Answer :
Explain what the above code does:

Answer :

om
Convert the following machine code into hexadecimal:
0001 00111011

il.c
0100 00001001
0000 00011110
1111 00000000
Answer :

Hexadecimal?
Answer :
ma
If we were lacking Assembly code, why might we want to convert machine code into
id @g
ah

The Fetch–Execute cycle and the role of registers within it

The Fetch-Decode-Execute cycle of a computer is the process by which a computer:


az

1. fetches a program instruction from its memory,


2. determines what instruction wants to do,
3. and carries out those actions.
hr

This cycle is repeated continuously by the central processing unit (CPU), from bootup to
when the computer is shut down. In modern computers this means completing the cycle
billions of times a second! Without it nothing would be able to be calculated.
za

  24  
Registers/circuits involved

The circuits used in the CPU during the cycle are:

• Program Counter (PC) - an incrementing counter that keeps track of the memory
address of which instruction is to be executed next...
• Memory Address Register (MAR) - the address in main memory that is currently
being read or written
• Memory Buffer Register (MBR) - a two-way register that holds data fetched from

om
memory (and ready for the CPU to process) or data waiting to be stored in memory
• Current Instruction register (CIR) - a temporary holding ground for the
instruction that has just been fetched from memory
• Index Register (IR)- An index register in a computer's CPU is a processor register
used for modifying operand addresses during the run of a program, typically for

il.c
doing vector/array operations. The contents of an index register is added to (in
some cases subtracted from) an immediate address (one that is part of the
instruction itself) to form the "effective" address of the actual data (operand).

Status Register (SR) The status register is a hardware register which contains

ma

information about the state of the processor. Individual bits are implicitly or
explicitly read and/or written by the machine code instructions executing on the
processor. The status register in a traditional processor design includes at least
three central flags: Zero, Carry, and Overflow, which are set or cleared
automatically as effects of arithmetic and bit manipulation operations.
@g

• Control Unit (CU) - decodes the program instruction in the CIR, selecting machine
resources such as a data source register and a particular arithmetic operation, and
coordinates activation of those resources
Arithmetic logic unit (ALU) - performs mathematical and logical operations
id

Register notation
ah

To describe the cycle we can use register notation. This is a very simple way of noting
all the steps involved. In all cases where you see brackets e.g. [PC], this means that the
contents of the thing inside the brackets is loaded. In the case of the first line, the
contents of the program counter is loaded into the Memory Address Register.
az

(Increment the PC for next cycle at the same time)


hr

decoded then executed


za

  25  
Detailed description of Fetch-Decode-Execute Cycle

To better understand what is going on at each stage we'll now look at a detailed
description:

om
il.c
ma
The contents of the Program Counter, the address of the next instruction to be executed,
is placed into the Memory Address Register
id @g
ah
az

The address is sent from the MAR along the address bus to the Main Memory. The instruction
at that address is found and returned along the data bus to the Memory Buffer Register. At
hr

the same time the contents of the Program Counter is increased by 1, to reference the next
instruction to be executed.
za

  26  
om
il.c
The MBR loads the Current Instruction Register with the instruction to be executed.

ma
id @g
ah

The instruction is decoded and executed using the ALU if necessary.

The Cycle starts again!


az

Exercise: Fetch Execute Cycle

Name 3 registers involved in the Fetch Execute Cycle and describe what each does:
hr

Answer :

Describe the Fetch Execute Cycle using register notation:


za

Answer :

  27  
Complete the following diagrams showing each step of the fetch decode execute cycle:

om
il.c
ma
id @g
ah
az
hr
za

  28  
Addressing modes
The operation field of an instruction specifies the operation to be performed.
This operation must be executed on the data stored in computer registers or the memory
words.
The way the operands are chosen during program execution is dependent on the addressing
mode of the instruction.
The addressing mode:
Specifies a rule for interpreting or modifying the address field of the instruction before
the operand is actually referenced.

om
Computers use addressing mode techniques for the purpose of accomodating the following
purposes:-
• to give programming versatility to the user by providing such facilities as pointers
to memory, counters for loop control, indexing of data and various other purposes.
• to reduce the number of bits in the addressing field of the instructions.

il.c
• to understand the various addressing modes, it is imperative that we understand the
basic operation cycle of the computer.
The control unit of a computer is designed to go through an instruction cycle that is
divided into 3 major parts:-
• fetch the instruction from memory

ma
• decode the instruction and
• execute the instruction.
Now to complete the above mentioned process we need to study some terminology that forms
an important part of instruction cycle.
Program counter:
@g
• There is 1 register in the computer called the program counter or pc that keeps
track of the instructions in the program stored in memory.
• PC holds the address of the instructions to be executed next and is incremented each
time an instruction is fetched from memory.
• The decoding of the instruction is done in the next step tells which operation is to
id
be performed, the addressing mode of the instruction, and the location of the
operands.
• The computer then executes the instruction and returns to the step1, i.e. to fetch
ah

the next instruction.


Mode field:
An example of an instruction format with a distinct addressing mode field is shown below:
az

Opcode Mode Address

The mode field is used to locate the operands needed for the operation. There may or may
not be the address field in the instruction.
hr

Although most addressing modes modify the address field of the instruction, there are two
modes that need no address field at all:
Implied mode
za

Immediate mode.
Implied mode:
in this mode the operands are specified implicitly in the definition of the instruction.
for example:-
“Compliment accumulator” is an implied-mode instruction because the operand in the
accumulator register is implied in the definition of the instruction. In fact, all
register reference instructions that use an accumulator are implied-mode instructions.

  29  
Immediate mode:
In this mode the operand is specified in the instruction itself. In other words, an
Immediate-mode instruction has an operand field rather than an address field.
The operand field contains the actual operand to be used in conjunction with the operation
specified in the instruction.
The address field of an instruction may specify either a memory word or a processor
register. When the address field specifies a processor register, the instruction is
said to be in register-mode.
Register mode:

om
In this mode the operands are in registers that reside within the CPU.
The particular register is selected from the register field in the instruction.
A k-bit field can specify any one of 2k registers.
Register indirect mode:
In this mode the instruction specifies a register in the CPU whose contents give the

il.c
address of the operand in the memory.
In other words, the selected register contains the address of the operand rather than the
operand itself.
Before using a register indirect mode instruction, the programmer must ensure that the
memory address of the operand is placed in the processor register with a previous

ma
instruction.
Advantage:

The address field of the instruction uses fewer bits to select a register than would have
been required to specify a memory address directly.
@g
Auto Increment or Auto Decrement:
This is similar to register indirect mode except that the register is incremented or
decremented after (or before) its value is used to access memory.
When the address stored in the registers refers to a table of data in memory, it is
necessary to increment or decrement the registers after every access to the table.
id
This can be achieved by using the increment or decrement instruction. In some computers it
is automatically accessed.
The address field of an instruction is used by the control unit in the CPU to obtain the
ah

operands from memory.


Sometimes the value given in the address field is the address of the operand, but
sometimes it is the address from which the address has to be calculated.
For that we need to know about the concept of ‘effective address’.
az

Effective Address:
The effective address is defined to be the memory address obtained from the computation
dictated by the given addressing mode.
hr

The effective address is the address of the operand in a computational-type instruction.


Direct Address mode:-
In this mode the effective address is equal to the address part of the instruction. The
za

operand resides in memory and its address is given directly by the address field of the
instruction.
Indirect Address mode:-
In this mode the address field of the instruction gives the address where the effective
address is stored in memory.
Control unit fetches the instruction from the memory and uses its address part to access
memory again to read the effective address.

  30  
Some addressing modes requires the following to calculate the effective address:
effective address= address part of instruction + content of CPU register
Relative Address mode:-
In this mode the content of the program counter is added to the address part of the
instruction in order to obtain the effective address.
The address part of the instruction is usually a signed number(either a +ve or a –ve
number).
When the number is added to the content of the program counter, the result produces an

om
effective address whose position in memory is relative to the address of the next
instruction.
Indexed Addressing mode:-
In this mode the content of an index register is added to the address part of the
instruction to obtain the effective address.

il.c
The index register is a special CPU register that contains an index value.

Note:
If an index-type instruction does not include an address field in its format, the

ma
instruction is automatically converted to the register indirect mode of operation.
Base Register Addressing mode
In this mode the content of a base register is added to the address part of the
instruction to obtain the effective address.
This is similar to the indexed addressing mode except that the register is now called a
@g
base register instead of the index register.
The base register addressing mode is used in computers to facilitate the relocation of
programs in memory. When programs and data are moved from one segment of memory to
another.
id
ah
az
hr
za

  31  
Numerical example

om
il.c
ma
id @g
ah
az
hr

Two-pass assemblers
za

An assembler is a translator, that translates an assembler program into a conventional


machine language program. Basically, the assembler goes through the program one line at a
time, and generates machine code for that instruction. Then the assembler procedes to the
next instruction. In this way, the entire machine code program is created. For most
instructions this process works fine, for example for instructions that only reference

  32  
registers, the assembler can compute the machine code easily, since the assembler knows
where the registers are.

Consider an assembler instruction like the following

JMP LATER
...
...
LATER:

om
This is known as a forward reference. If the assembler is processing the file one line at
a time, then it doesn't know where LATER is when it first encounters the jump instruction.
So, it doesn't know if the jump is a short jump, a near jump or a far jump. There is a
large difference amongst these instructions. They are 2, 3, and 5 bytes long respectively.
The assembler would have to guess how far away the instruction is in order to generate the

il.c
correct instruction. If the assembler guesses wrong, then the addresses for all other
labels later in the program woulds be wrong, and the code would have to be regenerated.
Or, the assembler could alway choose the worst case. But this would mean generating
inefficiency in the program, since all jumps would be considered far jumps and would be 5
bytes long, where actually most jumps are short jumps, which are only 2 bytes long.

ma
Soooooooo, what is to be done to allow the assembler to generate the correct instruction?
Answer: scan the code twice. The first time, just count how long the machine code
instructions will be, just to find out the addresses of all the labels. Also, create a
table that has a list of all the addresses and where they will be in the program. This
@g
table is known as the symbol table. On the second scan, generate the machine code, and use
the symbol table to determine how far away jump labels are, and to generate the most
efficient instruction.

This is known as a two-pass assembler. Each pass scans the program, the first pass
id
generates the symbol table and the second pass generates the machine code.
ah
az
hr
za

  33  

You might also like