CPU and Machine Code

Icodewithben
6 min readDec 2, 2023

--

Components of a CPU (registers, buses, control unit)

Little man machine CPU instructions

How to code and understand a machine program

See the video demonstration of components of the CPU and beginner machine code here: https://youtu.be/tiV_s4NtUis

Demonstration of machine code branching, looping and variables: https://youtu.be/hdb_rrHbhY0

Further reading: GPUs and Multicore CPUs and Parallel and Concurrent Processing | by Icodewithben | Feb, 2024 | Medium

Revision summary card:

Von Neumann Architecture:

You need to understand the different components of The Von Neumann Architecture (to understand the difference between Von Neumann and Harvard read about that here https://medium.com/@icodewithben/harvard-vs-von-neumann-architecture-04c8c5de28ae ) OR

The von Neumann architecture vs Harvard architecture | by Icodewithben | Feb, 2024 | Medium

Program Counter (PC) — This holds the address of the next instruction to be fetched.

Memory Address Register (MAR) — The MAR holds the address of a memory location that is being read from or written to. The address is copied from the PC before being sent along the address bus or it holds the address sent from memory.

Address Bus — It’s a unidirectional bus that carries address information from the CPU to memory and I/O devices.

Control Unit — The CU coordinates activities of the CPU, interpreting and executing instructions, controlling the data flow within and between the CPU and other components. For example, it determines whether the RAM needs to read or write data or instructions to RAM.

Control Bus — This is used to send read or write signals to the RAM and other parts of the computer system.

Data Bus — Bi-directional bus that carries data to and from memory and I/O devices.

Memory Data Register (MDR) — This stores any data from the RAM delivered here by the data bus. Also, results of calculations that need to be stored in RAM will be held in this register too.

Current Instruction Register (CIR) — Current Instruction Register holds the current instruction that has just been fetched from memory, awaiting execution.

Arithmetic Logic Unit (ALU) — This will carry out any calculations that need to be done.

Accumulator (ACC) — This will store the result of any calculations done in the ALU.

Components of the Von Neumann Architecture

Fetch, Decode, Execute Cycle:

Little Man Machine instructions:

Using this https://peterhigginson.co.uk/lmc/ and the instructions below we are going to write a computer program in machine code that will add two numbers and output the result.

Slow down the machine and then you will be able to see the data and instructions being passed to and from memory along the buses and stored in the different registers such as the MDR, MAR, ACC, CIR and how the ALU is used for calculations and how the PC is used to get the address of the next instruction.

Use these arrows to slow down the execution of the LMC

Put this code into the LMC https://peterhigginson.co.uk/lmc/ this program:

INP ​
STO 8 ​
INP ​
ADD 8 ​
OUT ​
HLT

Asks the user for an inputs

Stores that in memory location 8

Asks for second input, which is saved in the accumulator

ADDs whatever is in that memory location 8 to whatever is stored in the Accumulator

Outputs it and then it halts

The machine code instructions that can be entered into the little man machine are below. When the machine compiles the instructions, they are turned into numbers:

The machine code instructions consist of the instruction, the first digit and the address part the next 2 digits

STO 8 becomes 308.

3 is the instruction part

08 is the address part.

So it means store whatever is in the Accumulator in address 8.

The full instruction set for the LMC

Task:

  • Create a program to ask the user for 2 inputs and then subtract the first number from the second one.

Using DAT or data variable

Now enter this program into the LMC and press execute:

INP
STO FIRST
INP
STO SECOND
LDA FIRST
ADD SECOND
OUT
HLT
FIRST DAT
SECOND DAT

This code:

Stores whatever is input into the ACC into the variable FIRST

Then enters whatever is entered second into the variable called SECOND

Then it loads LDA the data in variable FIRST into the ACC

Adds the number stored in SECOND from whatever is in the ACC

Outputs OUT this

Then halts

Task:

  • Create a program that asks the user for input and stores it in a variable called AGE

Branching and Looping

Just like with normal code you can branch (if statements) and loop.

BRA is used to tell the code to branch or jump to the line with LOOP next to it in the code below. You can of course name the line whatever you want it does not have to be LOOP.

BRZ is used to branch or jump to the line labelled END if whatever is in the ACC is zero.

BRP although not used here will just to whatever line is labelled if whatever is in the ACC is 0 or positive.

Task: What does the program below do? Run the code in the LMC if you cannot guess by tracing through the code (you will not have access to the LMC in the exam so figuring out how machine code works by reading it is essential!)

LOOP LDA X
BRZ END
OUT
SUB Y
STO X
BRA LOOP
END HLT
Y DAT 1
X DAT 10

Task: See if you can change the program to make it count up from 1 to 10

Past Paper Questions Practice:

From 2018 Paper:

Answers:

PPQ 2021

Answers Q6 2021:

--

--