Machine Level Architecture: Machine code and processor instruction set

 ← Structure and role of the processor Machine code and processor instruction set The Fetch–Execute cycle and the role of registers within it →

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:

• LDA - Loads the contents of the memory address or integer into 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.

```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.

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. 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
INC ;increment a number by 1
DEC ;decrement a number by 1
MUL ;multiply numbers together
OR  ;boolean algebra function
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 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:

1. ```LDA #12 ;loads the number 12 into the accumulator
```
2. ```MUL #2  ;multiplies the accumulator by 2 = 24
```
3. ```SUB #6  ;take 6 away from the accumulator = 18
```
4. ```JNZ 6   ;if the accumulator <> 0 then goto line 6
```
5. ```SUB #5  ;take 5 away from the accumulator (this line isn't executed!)
```
6. ```STO 34  ;saves the accumulator result (18) to the memory address 34
```

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

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:

```        # = number
```

Let's take a look at a quick example:

Assembly code Main memory start Main memory end
```LOAD #10
STORE 12
```
10 9
11 2
12 7
13 10
14 12
10 9
11 2
12 22
13 10
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.

Let's take a look at doing this without the hashes:

Assembly code Main memory start Main memory end
```LOAD 10
STORE 12
```
10 9
11 2
12 7
13 10
14 12
10 9
11 2
12 16
13 10
14 12
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:

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:

10 1
11 4
12 4
13 100
14 5
```LOAD 14
STORE 12
```

10 1
11 4
12 17
13 100
14 5

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

211 6
212 3
213 78
214 21
```LOAD #100
STORE 213
STORE 214
```

211 6
212 3
213 100
214 121

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

99 6
100 6
101 8
102 9
```LOAD 100
DIV #7
STORE 102
```

99 6
100 6
101 8
102 2

Write some assembly code to do the following:

`34 + 35` and store in memory location 100

```LOAD #34
STORE 100
```

Write some assembly code to do the following:

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

```LOAD #100
DIV #2
STORE 100
```

List and give examples of three addressing modes:

• 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.

 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.