Low Level Programming

Low Level Programming

Low Level Programming

Programmers writing low level programs do not usually write in machine code. Instead they write in a language which is then translated into machine code.

Each instruction in machine code contains a function code and one or more addresses or operands .

An assembly language is a low level language which allows the programmer to use:

1 Mnemonics instead of the function codes; and

2 Symbolic addresses.

A mnemonic is a word or set of letters which can be used to represent a function code and which is easy to remember. The programmer writes the mnemonic instead of having to look up or remember the actual binary function code.

A symbolic address is a name invented by the programmer to identify a location. Without this idea the programmer would have to use absolute addresses, which are in binary .

Symbolic addresses are used so that:

1 There is no need to remember, or even to know, where each location is in the store.

2 If a program is changed parts of it will be moved in the store; the absolute addresses of the instructions may change but their symbolic addresses will stay the same.

Examples of mnemonics and symbolic addresses

The mnemonics used here are like those in some real low level languages. However, there are many languages which use different ones.

1 LDA can often be used as a mnemonic for LOAD ACCUMULATOR.

2 JMP PRNT. Here JMP is a mnemonic for ‘jump to the instruction’. PRNT is the symbolic address the programmer has chosen to represent the first instruction of a print routine.

3 LOOP ADD VALUE * Add VALUE to accumulator

              JMP PRNT * Print contents of accumulator

                INX * Add 1 to X register

               BNE LOOP * Branch to LOOP if (X) is not 0

Here      LOOP, VALUE and PRNT are symbolic addresses. ADD, JMP, INX and BNE are mnemonics.

The explanations at the right are programmer’s comments.

Example of a low level language program with mnemonics

Note: For simplicity it can be assumed that the addresses and data in this example are decimal numbers. It has been assumed that each instruction takes up one location of the store.

Address of instruction

Mnemonic

Address or operand

Explanation

100

101

102

103

LDA#

DEC

BNE

RTS

100

101

Load the number 100 into accumulator

Decrement accumulator

If accumulator is not zero,

branch to location 101

Return from subroutine

This small section of program seems to do little. The accumulator starts at 100 and is reduced to 0 as the program keeps executing the instructions in 101 and 102. Then it ‘returns from subroutine’. In fact the purpose of it is merely to delay the computer for the small fraction of a second that it takes to do this.

ASSEMBLERS

An assembler is a program which translates a program written in an assembly language into one in machine code. Assemblers were given this name because of the way they put together the machine code program in the store.

The main functions of an assembler are to:

1 Translate an assembly language source program into a machine code object program.

2 Work out where to store the object program and its data.

3 Detect errors in the source program and say what they are.

4 Link the program to any other programs or routines it uses. 5 Print a listing of the source and object programs.

Example of an assembly language program

The following program compares two numbers, A and B, and subtracts the smaller from the larger. It then stores the difference in RESULT. The language used is made up, but it is very similar to a number of actual assembly languages.

/ ORIGIN = 1025

* PROGRAM TO FIND THE POSITIVE DIFFERENCE BETWEEN TWO NUMBERS

LOA A * LOAD A INTO THE ACCUMULATOR

SUB B *SUBTRACT B FROM THE ACCUMULATOR

BPS STORE * IF ACCUMULATOR IS POSITIVE BRANCH TO STORE

LOA B * OTHERWISE LOAD B INTO THE ACCUMULATOR

SUB A * SUBTRACT A FROM THE ACCUMULATOR

STORE STA RESULT’ STORE THE ANSWER IN RESULT

HLT * HALT THE PROGRAM

/END

Notes:

1 ‘/’ indicates an instruction which is a directive. A directive is an instruction which tells the assembler how to deal with the rest of the program. Directives are carried out while assembling and are not translated into machine code.

IORIGIN = 1025 means ‘store the object program starting at location 1025’.

/ END means ‘there are no more instructions to translate’.

2 ‘*’ indicates the start of a comment (or a remark). A comment is a statement written by the programmer to explain the program to anyone reading it. Comments are not translated into machine code.

USE OF A TABLE TO SHOW THE EFFECT OF A PROGRAM

One way to demonstrate or to check the running of a program with certain data is to draw a table. The table should show the instructions carried out and the values in important locations and registers. (See also Trace tables.)

Example of the use of a table to show the effect of a program

Assume that the program in the previous example is run twice:

1 For the first run A is 21 and B is 32.

2 For the second run A is 45 and B is 15.

The following table shows what happens:

First run: A=21, B=32

Current Instruction

Accumulator

Result

Comments

LDA A

SUB B

BPS STORE

LDA B

SUB A

STORE STA RESULT

HLT

21

-11

-11

32

11

11

11

?

?

?

?

?

11

11

Subtract B from A

Accumulator negative. No jump

Subtract B from A

Second run: A=45, B=15

Current Instruction

Accumulator

Result

Comments

LDA A

SUB B

BPS STORE

STORE STA RESULT

HLT

45

30

30

30

30

?

?

?

30

30

Subtract B from A

Accumulator positive. Jump to STORE

Leave a comment

Your email address will not be published. Required fields are marked *