# Machine Code and Global Memory

updated 1 Jan 2020

To understand how memory works, it can be useful to consider how the CPU itself works, and what follows from that is how memory works.  Languages are built on these underlying principles and automate using the different types of memory.

This page uses a ‘hypothetical programmable calculator’ (Magic Calculator), to illustrate the principles of how instructions are executed and how global memory works with the instructions described below.

Follow the link to run the calculator in a browser window. You can also view the source code, or clone and play with the code yourself.

## Hypothetical Programmable Calculator

#### What is Machine code?

The actual CPU (central processing unit), the “intel i7” or “arm-9” chip, has a ‘native code’ instruction set, the ‘machine code’.  These instructions have no concept of variables, or ‘for loops’, or even characters. Everything is reduced to a number (an ‘A’ is normally reduced to the number 65).

Instructions are like “move from this numbered memory location to this register”, “shift this register left 3 bits”, and “mov this value to the instruction pointer”(last last one is a ‘jump’).  On a 16 bit computer, that ‘mov’ instruction for ‘register 7’ would have a specific number of ‘bits’ of the 16 bit instruction indicating the instruction, another group of ‘bits’ within the instruction with the value of the register ‘0111’ (for register 7) and the next 16 bits of memory supplying the number of memory location. In machine code the ‘mov value from the memory location following this instruction to register 7’ because simply a number.

Even those who best understand machine code do not write programs in machine code, except as a learning exercise. If fact as a learning exercise I highly recommend trying machine code, and as long as you don’t try and write a program that will actually do very much, it can be fun. In place of the complexity of a processor such as an Intel ‘I7’, this page proposes a hypothetical simple CPU.

#### How does a CPU work?

The example of the ‘hypothetical programming calculator is used for ‘how does a  ‘Central Processing Unit’ (CPU) work.  The working memory inside a CPU is the CPU ‘registers’, and the value displayed on a calculator screen is very analogous to simple computer with a main register.  For this exercise, consider this displayed value as the ‘a’ register (Many original computer did have an ‘a’ register, or ‘accumulator’ register, and some even provided a continuous display of the value of this register).  To add two numbers on a calculator, we enter the first number to our display or ‘a’ register, then activate ‘+’, which has to store the operation as plus, and save the first number into a second register which we can call ‘b’. Then we enter the second number into the ‘a’ register (or display), and with the ‘=’ we do the stored operation with add ‘a’ and ‘b’ and leaves the result in a.

So we have some program steps, but how do we ‘run’ these steps? Well first we need some memory.

#### Program Memory and program counter

Many calculators have one or more ‘memories’.  Our programmable calculator is going to have 100 memories!  The simplest calculators have one memory, and you can save from the ‘a’ register to the memory, or load from the memory to the ‘a’ register.  On some calculators you can even add the ‘a’ register into the memory, but I digress. The big thing with our programmable calculator, is that values in memories represent instructions.  Number ‘1’ when used as instruction could be our ‘+’, number ‘2’ our ‘=’ and number ‘3’ could mean ‘set a,n’  to set a from value in the memory following the instruction.   To make this work, we need a new register, a ‘Program Counter’ register for pointing to instructions.  Every time we load an instruction, or load information with the ‘Program Counter’, the program counter increases by 1.

So our program to add 7 and 8 (in memory locations 0, 1, 2, 3, 4, 5, 6 )now looks like:

• 3  7  1  3  8  2  0  (enter this string into the emulator ‘code’ field)

The steps are:

1. The “program counter (PC) starts at zero so the instruction at zero, (3- load a) is run, and this instruction loads the next value from the memory location specified PC register (and again adds one to the register), so the result is the ‘7’ from location ‘1’ is loaded into ‘a’ and 7 is displayed.
2. The PC register is now 2 (increased to 1 after loading the ‘3’ – load instruction, and again increased to 2 as the load instruction loaded the ‘7’ from location 1.  The plus instruction sets operation register to ‘add’ and copies the ‘7’ from the ‘a’ register to the ‘b’ register.
3. The ‘load’ instruction (3) from location ‘3’ is loaded from the program counter and this instruction then loads the ‘8’ from memory location 4 into ‘a’ register
4. the ‘=’ instructions (2) from memory location ‘5’ is loaded and this causes the ‘7’ from ‘b’ to be added to ‘a’ so the calculator then display our answer: ’15’
5. the ‘stop’ instruction (0) from memory location 6 causes our program to stop.

This simple example illustrates how a program actually runs in a computer. The main memory can have both data and instructions.

#### Adding global variables: the instructions.

Currently the binary program for the ‘programmable calculator’  just does the equivalent of  ‘7 + 8’ in python.

This is only useful because we can see the ‘a’ register on the calculator display.  The equivalent of ‘7+8’ being useful in ‘idle’, because idle prints the answer. Now consider the program ‘answer = 7 + 8’.  This program stores the answer in a variable.  The previous program is stored in  7 memory locations, so there is lots of free memory locations for variables.   If we plan to use half of the memories for code, and half for variables, then all memories below 50 would hold code and numbers used inside code, and memories 50 and above would be for variables.

None of the current instructions use memory as variables, so consider  two new instructions, load a,(n) and  save a,(n) to load ‘a’ register from the memory location we want, or save the ‘a’ register. The ‘load’ (instruction code 4)  and ‘save’ (instruction code 5) will both use the memory following the instruction to specify which memory is to be loaded or saved.

Currently the ‘Magic Calculator’ does not support these last two instructions(load and save), but if desired for experimentation, this could be added.