Assembly Language

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ASSEMBLY LANGUAGE

Machine code
• The only programming language that a CPU can use is machine code.
• Every different type of computer/chip has its own set of machine code instructions.
• A computer program stored in main memory is a series of machine code instructions
that the CPU can automatically carry out during the fetch-execute cycle.
• Each machine code instruction performs one simple task, for example, storing a value
in a memory location at a specified address.
• Machine code is binary, it is sometimes displayed on a screen as hexadecimal so that
human programmers can understand machine code instructions more easily.

Every instruction in assembly language program code (the source) translates into exactly
one instruction in the machine code (the object code)

Disadvantages of machine code

Writing programs in machine code is a specialised task that is very time consuming and
and see what happens.

• In order to shorten the development time for writing computer programs, other
programming languages were developed, where the instructions were easier to learn
and understand.
• Any program not written in machine code needs to be translated before the CPU can
carry out the instructions, so language translators were developed.

Assembly Language
• The first programming language to be developed was assembly language, this is
closely related to machine code
• It uses mnemonics instead of binary.

Opcode Operand Opcode Operand

ADD 20 0214 00000001000011000

Assembly language mnemonics Machine code Machine code

Hexadecimal Binary
• The structure of assembly language and machine code instructions is the same.

Opcode Operand

Each instruction has an opcode that Most instructions also have an operand
identifies the operation to be carried out that identifies the data to be used by the
by the CPU. opcode.

Assembly code is 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

Symbolic and absolute addressing


The following is a sequence of instructions with the first instruction loaded at address
300

300 LDI 309

301 CMP 314

302 JPE 308

303 OUTCH

304 LDD 309

305 INC ACC

306 STO 309

307 JMP 300

308 END

309 500

310 65

311 74

312 65

313 90

314 32
Absolute addressing
Note all the address values used by the program are shown as actual address numbers.
This has implications later when the program is translated by the assembler. If the
machine code similarly shows the absolute address values, then the program when
executed must be loaded to start at address 300.

Symbolic addressing
When we write computer programs in a high level language we use Symbolic Addressing,
where we refer to memory locations using identifier names for variables, functions , data
structures etc.

Example program to calculate profit:

LDD REVENUE

SUB COST

STR PROFIT

Advantage of symbolic addressing

• First it makes the programs easier to read as we are using words that mean something
to us. If we used absolute addressing instead it would be very difficult to understand
the purpose of the code
• It means that if the memory location where that data is stored changes we only have
to change it once in our program.

Note:

• The computer cannot process these symbolic addresses so when the program is
assembled all symbolic addresses are replaced with the memory address assigned to
that symbol. The assembler builds a symbol table or symbolic names and the
corresponding addresses in memory.

Profit program after the symbolic addresses have been swapped out

LDD #4454

SUB #3326

STR #4410
Directives
Assembler directives are instructions that direct the assembler to do something such as:

• Set aside space for variables


• Include additional source files , e.g INCLUDE “File56. LIB”
• State the start address for the program , e.g ORG 0300

Macros
A group of instructions may need to be executed several times within the same
program. The statements would be written once and the block of statements given an
identifier name. The statements can then be called using the name whenever they need
to be executed.

The syntax within the source code would be:

MACRO MyMacro

Instruction list

ENDMACRO

MACRO is an example of a directive within the program code.

The code is then called or evoked whenever need with the call:

MyMacro

When the assembly starts it will look for macro calls and duplicate the macro code
whenever it is called.

Stages of assembly
• Before a program written in assembly language (source code) can be executed, it
needs to be translated into machine code.
• The translation is performed by a program called an assembler.
• An assembler translates each assembly language instruction into a machine code
instruction.
• An assembler also checks the syntax of the assembly language program to ensure
that only opcodes from the appropriate machine code instruction set are used. This
speeds up the development time, as some errors are identified during translation
before the program is executed.
Types of assembler
There are two types of assembler: single pass assemblers and two pass assemblers.

• A single pass assembler puts the machine code instructions straight into the
computer memory to be executed.

• A two pass assembler produces an object program in machine code that can be
stored, loaded then executed at a later stage. This requires the use of another
program called a loader. Two pass assemblers need to scan the source program
twice, so they can replace labels (symbols) in the assembly program with
memory addresses in the machine code program.

E.g

Label Memory Address

LDD Total 0140

Assembly language mnemonics Machine code

Hexadecimal

To carry out the assembly, the software must have access to the opcode table showing
each opcode and its machine code equivalent. If symbolic addressing has been used in
the code, the assembler will add entries to a symbol table.
Example:

The first task of the assembly is the ‘pre-assembly’ process to scan the code and look for
any use of macros. If present the macro code will be inserted at each point in the code
where the macro is evoke. This program does not contain any macros.

First Pass

The assembler then makes a first pass through the code to find the symbolic addresses,
enter them in the symbol table and (if known) enter the absolute addresses. We shall
enter this address as a value relative to the address used for the first instruction.

Entries to the symbol table during translation


Pass 1

• Read the assembly language program one line at a time.


• Ignore anything not required, such as comments.
• Allocate a memory address for the line of code.
• Check the opcode is in the instruction set.
• Add any new labels to the symbol table with the address, if known.
• Place address of labelled instruction in the symbol table.

Second Pass

The assembler then makes a second pass through the code. Now the addresses are
known, the complete machine code program can be produced. The table below shows
the object code for our source code.

Pass 2

• Read the assembly language program one line at a time.


• Generate object code, including opcode and operand, from the symbol table
generated in Pass1.
• Save or execute the program.

The second pass is required as some labels may be referred to before their address is
known

You might also like