Assembly Language
Assembly
language is a low-level programming language.
Turning assembly language into
machine code.
Every
processor has a set of basic machine operations which it can carry out. Each
operation is represented by a machine code. This is called the
operation code or Opcode for short.
Opcode
|
Operation
|
001
|
Add
a number
|
010
|
Subtract
a number
|
100
|
Output
a value
|
Some
operations need a second or third piece of data in order to carry out
the instruction. The other piece of data is called the operand.
It
can be either a memory address or a number.
Hence,
a machine instruction consists of an opcode and an operand.
For
example: an instruction like 001 00001, the processor breaks it into two parts.
Opcode 001 and operand 00001.
An
assembly language is used to help human being write codes for the computer.
Assembly language
All
the opcodes are given a meaningful name for a human being to understand. For
example 001 means to ADD. The latter is in mnemonics. Hence, assembly language
makes use of mnemonics instead of the binary opcode.
Machine Code program
|
Assembly Language
program
|
Description
|
011 0011
|
LDD 3
|
Load
value 3 to the processor
|
001 0101
|
ADD 5
|
Add
5 to this value
|
100 0000
|
OUT
|
Output
the current value
|
101 1111
|
STO 15
|
Store
the value at address 15
|
All
the calculations are stored in the accumulator.
Using labels for addresses
The
memory address of a particular operand is called label. Using mnemonics to
represent opcodes and labels to represent the memory locations makes the
program code very much easier for the programmer to understand. The use of
labels for memory addresses is called Symbolic
addressing.
Assembly
language has a one-to-one relationship with machine code. That is, every
assembly language instruction translates into exactly one machine code
instruction.
An
assembler is used to convert the assembly language into machine code.
How an assembler works
An
assembler goes through the program line by line and generates the machine code
for that instruction. Then it proceeds to the next instruction. However, for a
program like the one below:
Input a, b
C = a*b
JMP Hello
Output c
Hello:
output “Hi, it’s a jump instruction”
When
the assembler is processing the file one line at a time, then it does not know
where the function Hello is in the source code.
To solve that problem, the assembler needs to scan the source code twice.
The first time (known as pass one) the assembler counts how long the machine
code instructions will be and find out the addresses of all the labels. A symbol table is created which will
store the addresses of all the labels in the source code.
On
the second pass, the machine code is generated with the help of the symbol
table.
To
summarise
PASS
1:
-
Assign addresses to all instructions in
the program
-
Addresses of symbolic labels are stored
-
Some assembler directives will be processed
PASS
2:
-
Translate the opcodes and symbolic
operands
-
Generate data values
-
Assembler directives will be processed
-
Write the object program and assembly
listing
Assembly language contains both
macro and directives
Macro
• A group of instructions given a name
// subroutine
• A group of instructions that need to
be executed several times within the same program
• The statements are written once and
called using the name whenever they need to be executed
• Macro code is inserted into the source
file at each place it is called
•
By example
An assembler directive is
a message to the assembler that tells the assembler what it needs to know in
order to carry out the assembly process. For example the EQU assembler
directive simply equates a symbolic name to a numerical value (data item).
Consider
January EQU 1
January EQU 1
February
EQU 2
The
assembler substitute the equated value for the symbolic name, for example, the
instruction ADD.B #January,D2 can be written as ADD.B #1, D2.
A directive is
not a program instruction. It is information for the assembler.
More examples:
-
State the start
address for the program
-
tell the
assembler to set aside space for variables
Relative
addressing:
It is a number
(the offset) which is added to the base address to give the actual address.
For example:
Relative
address
|
0
|
1
|
2
|
3
|
4
|
5
|
6
|
Addresses
|
102
|
103
|
104
|
105
|
106
|
107
|
108
|
Relative to
address 102
Absolute
addressing:
The operand is a
numeric address // The numeric address is given // referring directly to a
memory location
Absolute
address
|
102
|
103
|
104
|
||||
Addresses
|
102
|
103
|
104
|
105
|
106
|
107
|
108
|
No comments:
Post a Comment