ASSEMBLY LANGUAGE
PROGRAMMING
MACHINE CODE INSTRUCTIONS
The only language that the CPU recognizes is machine code. Therefore, when a program
is running and an instruction is fetched from memory this has to be in the format of a
binary code that matches the specific machine code that the CPU uses.
Machine code also called machine language or object code or first generation language
Machine code is defined as low level language that can be understood directly by the
computer’s CPU.
MACHINE CODE
INSTRUCTIONS(Cont…)
Machine code consists of a sequence of instructions
An instruction contains an opcode, Opcode defines the action associated with
the instruction
An instruction may not have an operand but up to three operands are possible.
Operand defines any data needed by the instruction.
Different processors will have comparable instructions for the same operations, but
the coding of the instructions will be different.
ADVANTAGES OF MACHINE CODE
It makes fast and efficient use of the CPU
It is stable and can hardly break down
CPU understands machine code without translation
DISADVANTAGES OF MACHINE CODE
It is limited to one machine. Even if two different processors have the same instruction, the
machine codes for them will be different but the structure of the code for an instruction will
be similar for different processors.
It is very long, difficult to learn
It is time consuming and boring
It is difficult to read and understand machine code
It is difficult to correct errors
All operation codes have to be remembered
THE COMPONENTS OF MACHINE
CODE INSTRUCTION
The following must be defined for each individual machine code instruction:
The total number of bits or bytes for the whole instruction
The number of bits that define the opcode
The number of operands that are defined in the remaining bits
Whether the opcode occupies the most significant or the least significant bits.
THE COMPONENTS OF MACHINE
CODE INSTRUCTION (CONT…)
Machine code instruction: a binary code with a defined number of bits that comprises
an opcode and, most often, one operand
OPCODE
This has an eight-bit opcode consisting of four bits for the
operation, two bits for the address mode and the remaining two
bits for addressing registers. This allows 16 different operations
each with one of four addressing modes.
The opcode will occupy the most significant bits in the
instruction. Because in some circumstances the operand will be
a memory address it is sensible to allocate 16 bits for it. This is
in keeping with the 16-bit address bus.
ASSEMBLY LANGUAGE
The essence of assembly language is that for each machine code
instruction there is an equivalent assembly language instruction
which comprises:
A mnemonic (a symbolic abbreviation) for the opcode
A character representation for the operand.
ASSEMBLY LANGUAGE(Cont…)
If a program has been written in assembly language it has to be
translated into machine code before it can be executed by the
processor. The translation program is called an 'assembler‘.
Using an assembly language, the programmer has the advantage
of the coding being easier to write than it would have been in
machine code.
FEATURES IN ASSEMBLY LANGUAGE
An assembler allows a programmer to include some special
features in an assembly language program. Such as:
Comments
Symbolic names for constants
Labels for addresses
Macros
Directives
FEATURES IN ASSEMBLY LANGUAGE
(CONT…)
The first three items on this list are there to directly assist the
programmer in writing the program . Of these, comments are
removed by the assembler and symbolic names and labels require
a conversion to binary code by the assembler. A macro is a
sequence of instructions that is to be used more than once in a
program.
FEATURES IN ASSEMBLY LANGUAGE
(CONT…)
Directive is instructions to the assembler as to how it should
construct the final executable machine code. This might be to
direct how memory should be used or define files or procedures
that will be used. They do not have to be converted into binary
code.
SYMBOLIC, RELATIVE AND ABSOLUTE
ADDRESSING
SYMBOLIC, RELATIVE AND ABSOLUTE
ADDRESSING(Cont…)
The convention has been followed that a label is written with a
following colon which is ignored when the label is referenced.
The use of symbolic addressing allows a programmer to write some
assembly language code without having to bother about where
the code will be stored in memory when the program is run.
However, it is possible to write assembly language code where the
symbolic addressing is replaced by either relative addressing or
absolute addressing.
SYMBOLIC, RELATIVE AND ABSOLUTE
ADDRESSING(Cont…)
SYMBOLIC, RELATIVE AND ABSOLUTE
ADDRESSING(Cont…)
For the relative addressing example, the assumption is that a special
function base register BR contains the base address. The
contents of this register can then be used as indicated by [BR]. Not
that there are no labels for the code. The left-hand column is just
illustration identifying the offset from the base address which is
the address of the first instruction in the program.
SYMBOLIC, RELATIVE AND ABSOLUTE
ADDRESSING(Cont…)
For the absolute address example there are again no labels for
the code. The left-hand column is again just for illustration
but this time identifying actual memory addresses. This has
been coded with the understanding that the first instruction in the
program is to be stored at memory address 200.
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER
For any assembler there are a number of things that have to be
done with the assembly language code before any translation can
be done. Some example are:
Removal of comments
Replacement of a macro name used in an instruction by the list
of instructions that constitute the macro definition.
Removal and storage of directives to be acted upon later.
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
A two – pass assembler is designed to handle programs
written in the style of the symbolic addressing. A two-pass
assembler is needed so that in the first pass the location of
the addresses for forward references can be identified.
During the first pass the assembler uses a symbol table.
The code is read line by line. When a symbol address is met
for the first time its name is entered into the symbol table.
Alongside the name a corresponding address has to be added as
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
Symbol table
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
Note that the assembler has to count the instructions as it
reads the code. Then when it encounters a label, it can
enter the offset value into the symbol table. In this example
the first entry made in the offset column is the +7 for STRPLP
For the second pass the assembler uses the symbol table
and a lookup table that contains the binary code for each
opcode. The output of from the second pass will be a
machine code program.
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
An opcode lookup table
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
Machine code created from assembly code
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
Some points to note are as follows:
Most of the instructions have an operand which is a 16-bit
binary number.
Usually this represents an address but for the SUB and
LDM instructions the operand is used as a value.
THE ASSEMBLY PROCESS FOR A
TWO-PASS ASSEMBLER(Cont…)
There is no operand for the IN and END instructions.
The INC instruction is a special case. There is an operand
in the assembly language code, but this just identifies a
register. In the machine code the register is identified
within the opcode, so no operand is needed.
THE ASSEMBLY PROCESS FOR A TWO-PASS
ASSEMBLER(Cont…)
The machine code has been coded with the first instruction
occupying address zero.
This code is not executable in this form, but it is valid output
from the assembler.
Changes will be needed for the addresses when the program is
loaded into memory ready for it to be executed.
ADDRESSING MODES
Three memory locations following the program code have
been allocated a value zero to ensure that they are
available for use by the program when it is executed.
When an instruction requires a value to be loaded into a
register there are different ways of identifying the value.
These different ways are described as the 'addressing modes'.
Two bits of the opcode in a machine code instruction would be used
to define the addressing mode. This allows four different modes
ADDRESSING MODE (CONT…)
Addressing
Addressing mode
mode Use of the operand
Immediate The operand is the value to be used in the
instruction SUB #48 is an example
Direct The operand is the address which holds the value to
be used in the instruction. ADD TOTAL is an
example
Indirect The operand is an address that holds the address
which has the value to be used in the instruction
Indexed The operand is an address to which must be added
the value currently in the index register (IX) to get
address which holds the value to be used in the
ADDRESSING MODE (CONT…)
For immediate addressing there are three options for defining the
value
#48 specifies the denary value 48
#B00110000 specifies the binary equivalent
#&30 specifies the hexadecimal equivalent
ASSEMBLY LANGUAGE
INSTRUCTIONS
Data movement
These types of instruction can involve loading data into a register or storing data in
memory.
Instruction Instruction Explanation
opcode operand
LDM #n Immediate addressing load the number n to ACC
LDR #n Immediate addressing load the number n to IX
LDD <Address> Direct addressing load the contents at the given address
to ACC
LDI <Address> Indirect addressing. The address to be used is at the
given address. Load the contents of this second address
to ACC
LDX <Address> Indexed addressing. Form the address from <address>+
the contents of the index register. Copy the contents of
this calculated address to ACC.
MOV <Register> Move the contents of the accumulator to the register
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
Example
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
The following shows some examples of the effect of an instruction or a sequence
of instructions based on the memory content shown in above figure
LDD 103 The value 110 is loaded into the accumulator
LDI 106 The value 208 from address 101 is loaded into the
accumulator
STO 106 The value 208 is stored in address 106
LDD INDEXVALUE The value 3 is loaded into accumulator
MOV IX The value 3 from accumulator is loaded into the index
register
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
INPUT AND OUTPUT
There are two instructions provided for input or output. In each
case the instruction has an opcode; there is no operand.
The instruction with opcode IN is used to store in the ACC the
ASCII value of a character typed at the keyboard.
The instruction with opcode OUT is used to display on the screen
the character for which the ASCII code is stored in the ACC.
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
Comparisons and jumps
A program might need an unconditional jump or might need a
jump if a condition is met. In the second case, a compare
instruction is executed first.
ASSEMBLY LANGUAGE
INSTRUCTIONS (CONT…)
Instruction Instruction Explanation
opcode operand
JMP <Address> Jump to the given address
CMP <Address> Compare the contents of ACC with the contents of
<address>
CMP #n Compare the contents of ACC with the number n
CMI <Address> Indirect addressing. The address to be used is at
the given address. Compare the contents of ACC
with the contents of this second address.
JPE <Address> Following a compare instruction, jump to
<address> if the compare was true
JPN <Address> Following a compare instruction, jump to
<address> if the compare was false.
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
Note that the comparison is restricted to asking if two values are
equal. The result of the comparison is recorded by a flag in the
status register. The execution of the conditional jump
instruction begins by checking whether or not the flag bit has been set.
This jump instruction does not cause an immediate jump. This is because a
new value has to be supplied to the program counter so that the next
instruction is fetched from this newly specified address. The incrementing
of the program counter that took place automatically when instruction was
fetched is overwritten.
ASSEMBLY LANGUAGE
INSTRUCTIONS (CONT…)
Arithmetic operations
Instruction Instruction Explanation
opcode operand
ADD <Address> Add the contents of the given address to the ACC
ADD #n Add the denary number n to the ACC
SUB <Address> Subtract the contents of the given address from
the ACC
SUB #n Subtract the denary number n from the ACC
INC <Register> Add 1 to the contents of the register (ACC or IX)
DEC <Register> Subtract 1 from the contents of the register (ACC
or IR)
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
The following should be noted concerning the program
The first tree instructions initialize the count and the sum
The instruction in address 103 is the one that is returned to in each
iteration of the loop; in the first iteration it is loading the value 0 into
the accumulator when this value is already stored but this cannot be
avoided.
The below figure is a program to calculate the result of dividing 75 by 5
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
The next three instructions are increasing the count by 1 and
storing the new value
Instruction 106 to 108 add 5 to the sum
Instructions 109 and 110 checks to see if the sum has reached
75 and if it has not the program begins the next iteration of the
loop.
Instructions 111 to 113 are only used when the sum has reached
75 which causes the value 15 stored for the count to be output.
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
SHIFT OPERATIONS
There are two shift instructions available
LSL #n
Where the bits in the accumulator are shifted logically n places to the
left
LSR #n
Where the bits are shifted to the right.
Logical shift where bits in the accumulator are shifted to the right or to
the left and a zero moves into the bit position vacated.
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
In the status register this can be used to examine individual bits.
For a left logical shift, the most significant bit is moved to
the carry bit, the remaining bits are shifted left and a zero
is entered for the least significant bit. For a right logical
shift, it is the least significant bit that is moved to the carry
bit and a zero is entered for the most significant bit.
ASSEMBLY LANGUAGE
INSTRUCTIONS (CONT…)
If the accumulator content represents an unsigned integer, the
left shift operation is a fast way to multiply by two.
However, this only gives correct result if the most significant bit is
a zero. For an unsigned integer the right shift represents
integer division by two.
For example: 00110001 (denary 49) gives if right shifted
00011000 (denary 24)
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
It should apparent that a logical shift cannot be used for
multiplication or division by two when a signed integer is
stored. This is because the operation may produce a result where the
sign of the number has changed.
Cyclic shift is similar to a logical shift but bits shifted from one end
reappear at the other end
Arithmetic shift uses the shift to carry out multiplication or division
of a signed integer stored in accumulator.
ASSEMBLY LANGUAGE INSTRUCTIONS
(CONT…)
BITWISE LOGIC OPERATION
INSTRUCTION INSTRUCTION EXPLANATION
OPCODE OPERAND
AND #Bn Bitwise AND operation of the contents of ACC with the
binary number n
AND <Address> Bitwise AND operation of the contents of ACC with the
contents of <address>
XOR #Bn Bitwise XOR operation of the contents of ACC with binary
number n
XOR <Address> Bitwise XOR operation of the contents of ACC with the
contents of <address>
OR #Bn Bitwise OR operation of the contents of ACC with binary
number n
OR <Address> Bitwise OR operation of the contents of ACC with the
contents of <address>
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS
Register transfer notation
ACC ←[[CIR(15:0)]]
The instruction is in the CIR and only the 16-bit address needs to
be examined to identify the location of the data in memory. The
contents of that location are transferred into the accumulator.
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
COMPUTER ARITHMETIC
The following worked example illustrates how the values stored in
the Status Register can identify a specific overflow condition
FURTHER CONSIDERATION OF
ASSEMBLY LANGUAGE
INSTRUCTIONS(Cont…)
The use of the following three flags is required
The carry flag, identified as C, which is set to 1 if there is a
carry
The negative flag, identified as N, which is set to 1 if a result is
negative
The overflow flag, identified as V, which is set to 1 if overflow is
detected
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
Using the status register during an arithmetic operation
1. Consider the addition of two positive values where the sum of
the two produces an answer that is too large to be correctly
identified with the limited number of bits used to represent the
values.
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
1. For example if we use an eight-bit binary integer representation
and attempt to add denary 66 to denary 68
01000010
+ 01000100
-----------------------
10000110
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
Flags: N V C
11 0
The addition of denary 66 to denary 68
The answer produced is denary -122. Two positive numbers
have been added to get a negative number. This impossibility
is detected by the combination of the negative flag and the
overflow flag being set to 1. The processor examines the flags,
identifies the problem and generates an interrupt.
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
TRACING AN ASSEMBLY LANGUAGE PROGRAM
One way of checking to see if an assembly language program has
errors is to carry out a dry (practice) run. The main feature of this
will be to check how the contents of the accumulator changes as
the program runs.
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
Tracing an assembly language program
For this example the trace table needs a column for the
accumulator, two for memory locations and one for the output.
The tracing is based on an initial user input of 15, a second input of
27 and a final input of 31
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
100 IN
101 STO 200
102 IN
103 STO 201
104 IN
105 ADD 200
106 STO 200
107 ADD 201
108 INC ACC
109 OUT
110 END
FURTHER CONSIDERATION OF ASSEMBLY
LANGUAGE INSTRUCTIONS(Cont…)
The trace table showing the execution of the program
Accumulator Memory location Memory location Output
200 201
15
15
27
27
31
46
46
73
74
74
Exercises
The following table shows part of the instruction set for a processor. The processor has
one general purpose register, the Accumulator (ACC) and an Index Register (IX).
Exercises(Cont…)
Exercises(Cont…)
Exercises(Cont…)