Computer Science
Machine Code Operations

Machine Code

We already know that the language of the digital computer is binary. The patterns of 1's and 0's represent both the data we want to work with and the instructions we want the computer to carry out.

A machine operation is usually expressed in two parts. 32 bits are typically used to express the entire instruction which is divided into,

Operation Code + Operand

The operation code represents a specific machine operation (eg add, load, store). The operand is an item of data or an address of an item of data.

High & Low-Level Languages

C# is a high-level compiled language. We write our programs using statements which are easily understandable to us. When we compile our source code into object code (the machine code instructions that the computer can understand), the statements we wrote in C# are converted into the binary patterns that represent these instructions translated into machine code. Each high-level statement may require many machine code operations.

Assembly language is a low-level language. Instead of writing out binary patterns, the programmer uses mnemonics - a word like LOAD is used in place of the binary pattern that represents this machine operation. Assembly programs are turned into executable machine code using an assembler. Unlike with the high-level compiled language, each statement in assembly language has a single equivalent in machine code.

Assembly Language Instructions

The term Instruction Set is used to refer to the list of binary patterns that represent the machine operations of a specific processor. Low-level languages are platform-specific. Each processor has its own set of instructions and requires a different assembler to turn the programs into machine code. You should also appreciate that low-level programming requires a level of detail and a regard for the architecture of the specific machine that is being programmed that makes it more time-consuming to write than a high-language program. It is used where specific machine registers need to be accessed, where programs need to address specific memory locations, occupy as little space in memory as possible or execute very quickly.

At this stage, you do not need to be able to write programs in Assembly. You do need to appreciate its uses and limitations as well as recognise roughly what the program may look like.

Remember that the operand part of the machine code instruction could refer to data, an address in memory and may include a reference to a general-purpose machine register. The following examples show this,

LOAD #5 - Load the value 5 into the accumulator
STORE 27 - Store a copy of the accumulator in memory location 27
ADD R1, #46 - Add the value 46 to the contents of machine register R1

The meaning of the operand is determined by what is called the addressing mode. The hash symbol in operands tends to mean that the number which follows is a value and not the address of a location in main memory.

Addressing Modes

Immediate Addressing

The operand contains the value that is being operated on,

LOAD #11101011B - Load the binary value 11101011 into the accumulator
LOAD #&84 - Load the hex value 84 into the accumulator

Direct Addressing

The operand contains an address of a location in memory which contains the value to be operated upon,

LOAD 35 - Load the contents of memory location 35 into the accumulator.

Indirect Addressing

The operand contains an address of a location in memory. This location contains the address of the location in memory where the value is stored,

LOAD (1000) - load the contents of the memory location whose address is stored in location 1000.

Indexed Addressing

Indexed addressing is used in machines containing an index register. With this mode of addressing, values in the operand are incremented by the value stored in the index register,

LOAD X, #50 - load into the accumulator the contents of the location in memory which is formed by adding 50 to the contents of register X

Base Register Addressing

Similar to indexed addressing except that a base address is stored in a register. The address field contains an offset which is added to the base address to form the address to be used in the operation.

LOAD 35, B - load the contents of the address formed by adding 35 to the base address stored in register B.

Relative Addressing

Used to branch to an address relative to the instruction held in the Program Counter.

JMP +100 - branch to the instruction 100 bytes on.

The advantage of using Base Register and Relative addressing modes is that programs can become relocatable in memory.