# A-level Computing/AQA/Paper 2/Fundamentals of computer organisation and architecture/Machine-code/assembly language operations
## Machine code

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:
* LDA - Loads the contents of the memory address or integer into the accumulator
* ADD - Adds the contents of the memory address or integer to the accumulator
* 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:
| Machine code | Assembly code |
| 000000110101 = | Store 53 |
Let's take a look at a quick coding example using assembly code.
The code above is the equivalent of saying x = 23 + 42 in VB.NET.

## Instruction set

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. Below are a list of some other instructions that might be used:
Let us look at a more complex example of assembly code instructions:
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 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 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.
* Exercise: Instruction sets
* What is the instruction set:
* Answer:
* the range of instructions that a CPU can execute
* Name and explain the two parts that make up an machine code instruction:
* Answer:
* opcode - the command to be executed
* operand - the data or address being worked upon
* 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?
* What is the largest number that I could use as data?
* Answer:
* Number of instructions: $2^{4}=16$
* largest operand: $2^{6}-1=63$
* 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?
* Answer:
* Number of instructions: $2^{6}=64$
* largest operand: $2^{10}-1=1023$
* Why might a manufacturer choose to increase the instruction set size?
* Answer:
* so that they can increase the number of discrete instructions that can be executed
* What might be the problem with increasing the space taken up by the opcode?
* Answer:
* less space for the operand, meaning reduced range and precision in data be processed in a single instruction
* Give two benefits for increasing the word size of a processor?
* Answer:
* more space available to increase the instruction set size
* greater range and precision available in the operand

### 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:
Let's take a look at a quick example:
| Assembly code | Main memory start | Main memory end |
| LOAD #10 ADD #12 STORE 12 | AddressContents 109 112 127 1310 1412 | AddressContents 109 112 1222 1310 1412 |
| This code loads the number 10 into the accumulator, then adds the number 12, it then stores the result 22 into memory location 12. |||
Let's take a look at doing this without the hashes:
| Assembly code | Main memory start | Main memory end |
| LOAD 10 ADD 12 STORE 12 | AddressContents 109 112 127 1310 1412 | AddressContents 109 112 1216 1310 1412 |
| This code loads the value stored in memory location 10 into the accumulator (9), then adds the value stored in memory location 12 (7), it then stores the result into memory location 12 (9 + 7 = 16). |||
There are many types of addressing modes. But we only need to know 3, they are:
| Addressing Mode | Symbol | Example | Description |
| Memory Location | | LOAD 15 | 15 is treated as an address |
| Integer | # | LOAD #15 | 15 is treated as a number |
| Nothing | | HALT | Some instruction don't need operands such as halting a program |
Exercise: Assembly code and Addressing modes
For the following memory space, what would it look like after executing the assembly code below:
AddressContents
101
114
124
13100
145
LOAD 14
ADD #12
STORE 12
Answer:
AddressContents
101
114
1217
13100
145
For the following memory space, what would it look like after executing the assembly code below:
AddressContents
2116
2123
21378
21421
LOAD #100
STORE 213
LOAD 214
ADD 213
STORE 214
Answer:
AddressContents
2116
2123
213100
214121
For the following memory space, what would it look like after executing the assembly code below:
AddressContents
996
1006
1018
1029
LOAD 100
ADD 101
DIV #7
STORE 102
Answer:
AddressContents
996
1006
1018
1022
Write some assembly code to do the following:
34 + 35 and store in memory location 100
Answer:
LOAD #34
ADD #35
STORE 100
Write some assembly code to do the following:
4 + (100 / 2) and store in memory location 100
Answer:
LOAD #100
DIV #2
ADD #4
STORE 100
List and give examples of three addressing modes:
Answer:
Memory Locations - LOAD 15
Integers (Whole Numbers) - LOAD #15
Nothing - HALT :

### 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 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.
| Machine code | Instruction | Addressing mode | Hexadecimal | Example |
| 0000 | STORE | Address | 0 | STO 12 |
| 0001 | LOAD | Number | 1 | LDA #12 |
| 0010 | LOAD | Address | 2 | LDA 12 |
| 0100 | ADD | Number | 4 | ADD #12 |
| 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:
* LOAD 12
* ADD #6
* Answer:
* 0010 00001100
* 0100 00000110
* Using the table above give the assembly code for the following machine code:
* 0001 00000111
* 0100 00001001
* 0000 00011110
* Answer:
* LOAD #7
* ADD #9
* STORE 30
* Explain what the above code does:
* Answer:
* loads the integer 7 into the Accumulator, adds the integer 9 to the Accumulator, stores the result, 16, in memory location 30
* Convert the following machine code into hexadecimal:
* 0001 00111011
* 0100 00001001
* 0000 00011110
* 1111 00000000
* Answer:
* 1 3 B
* 4 0 9
* 0 1 E
* F 0 0
* If we were lacking Assembly code, why might we want to convert machine code into Hexadecimal?
* Answer:
* It makes it easier for humans to read and understand.
* Extension: Little Man Computer
* If you would like to play around with Assembly language a great place to start is the Little man computer. You can find a Java applet and some examples at the York University website or a javascript version created by Peter Higginson