Machine Language Introduction - Lab 8

While we humans can read and understand algorithms written in pseudocode and various programming languges, a computer can only read and understand something called Machine Language.

To us, machine language is just long sequences of 1's and 0's. With some work, and a table showing what the different sequences of  1' and 0's mean, we can decipher it. It is very difficult to write a simple program in ML, yet alone an entire software application.

With some practice, we can learn to write simple ML programs. Each type of computer has its own machine language.

Lets look at a short example which shows the relationship between the pseudocode we have been using to write algorithms , something called assembly  language and finally machine language. For this example we will use the languages of our laboratory simulators.

Pseudocode Algorithm                Assembly Language Program                  Machine Language Program
Steps                  label    op    operand  comment     address        Machine Instruction
Set the value of A to 8                LOAD    A                 0000 0000 0000:    0000 0000 0000 0100
Set the value of B to 7                ADD     B                 0000 0000 0001:    0011 0000 0000 0101
Set the value of C to                  STORE   C                 0000 0000 0010:    0001 0000 0000 0110
the value of A + B                   HALT                      0000 0000 0011:    1111 0000 0000 0000
A:  .DATA    8                 0000 0000 0100:    0000 0000 0000 1000
B:  .DATA    7                 0000 0000 0101:    0000 0000 0000 0111
C:  .DATA    0                 0000 0000 0110:    0000 0000 0000 0000

All three examples above add the values of variables A and B together and store the sum in the variable C.

The pseudocode algorithm should need no explanation.

Lets look at the Assembly Language (AL) program which implements the algorithm.

Each line in the AL program consists of 4 parts:

1) an optional label,

2) an op or a pseudo-op, ( an operation code such as ADD, SUM etc. )

3) an operand which refers to a memory location, or a piece of data and

4) an optional comment.

A program called an assembler takes the AL program and converts it into the corresponding  Machine Language (ML) program.

The ML program is just the following:

0000 0000 0000 0100
0011 0000 0000 0101
0001 0000 0000 0110
1111 0000 0000 0000
0000 0000 0000 1000
0000 0000 0000 0111
0000 0000 0000 0000

The column labeled "address" is just for our reference.

For our simulator, and the textbook we use a machine language where each machine instruction or command takes 16 bits or 2 bytes of memory space. Each instruction has the same format:

 OpCode Address 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0

where Opcode is 4 bits and address is 12 bits.

For example the first instruction above: 0000 0000 0000 0100 tells the computer to Load the information at memory location 0000 0000 0100 and store it in a special memory location in the Arithmetic / Logic Unit called Register R.

0000 is the operation code for 'Load' and 0000 0000 0100 is the address.

Let's back up a minute. Our ML simulator uses a very simple computer model:

Memory Unit

Control Unit

Register Unit

ALU Unit

This picture shows the Virtual Machine after fetching and decoding the instruction at memory location 0.

There is one Assembly Language OP (operation) for every opcode (instruction type) in the computer's vocabulary.

In the case of our simulated computer, there are 16 different instructions that it understands.

The table below lists these op codes along with the corresponding Assembly Language Instruction and the meaning. In the table, 'CON(X)' denotes the contents of memory cell X.

 Binary Op Code Instruction Meaning 0000 load X CON(X) --> R 0001 store X R --> CON(X) 0010 clear X 0 --> CON(X 0011 add X R + CON(X) -->R 0100 increment X CON(X) + 1 --> CON(X) 0101 subtract X R - CON(X) --> R 0110 decrement X CON(X) - 1 --> CON(X) 0111 compare X if CON(X) > R then GT =1 else GT=0 if CON(X) is equal to R then EQ=1 else EQ=0 if CON(X) < R then LT =1 else LT=0 1000 jump X X --> PC 1001 jumpgt X if  GT is 1, X --> PC 1010 jumpeq X if  EQ is 1, X --> PC 1011 jumplt X if  LT is 1, X --> PC 1100 jumpneq X if  EQ is 0, X --> PC 1101 in X Read in a value and store it in memory cell X 1110 out X Print out the value in memory cell X 1111 halt Stops the program

In Laboratory Experiment #8 you will be working with the Machine Language Simulator

Machine Simulator Powerpoints