Assembly language programing
Machine code instructions:
We need to start with a few facts:
The only language that the CPU recognises is machine code
Machine code consists of a sequence of instructions
An instruction contains opcode
An instruction may not have an operand but up to three operands are possible
Different processers have different instruction sets associated with them
Different processers will have comparable instructions for the same operations, but
the coding of the instruction will be different
For a particular processer, 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 of the or significant bits
Opcode – Defines the action associated with the instruction
Operand – Defines any data needed by the instruction
Machine code instructions – A binary code with a defined number of bits that comprises an
opcode and, most often, one operand
The number of bits needed for the opcode depends on the number of different opcodes in
the instruction set for the processor. The opcode can be structured with the first few bit
defining the operation and the remaining bits associated with addressing. A sensible
instruction format for our simple processor is shown below.
Opcode
Address Register
Operation mode Addressing Operand
4 bits 2 bits 2 bits 16 bits
This has an 8 bit opcode consisting of four bits for the operation and the remaining two bits
for addressing registers. This allows 16 different operations each with one of four addressing
modes. This 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.
When an instruction arrives in the CPU the control unit checks the opcode to see what
action it defines. This first step in the decode stage of the fetch-execute cycle can be
described using the register transfer notation. However, a slight amendment is needed to
the format. The following shows the transfer of bits 16 to 23, which represent the opcode,
from the current instruction register to the control unit:
CU <- [CIR (23:16)]
Assembly language:
Assembly language – A low-level language related to machine code where opcodes are
written as mnemonics and there is a character representation for an operand.
Assembler – A program used to translate an assembly language program into machine code.
Mnemonic – A symbolic abbreviation.
For each machine code instruction there is an equivalent assembly language instruction
which comprises:
A mnemonic for the opcode
A character representation
The use of an assembler allows a programmer to include some special features in an
assembly language program for example:
Comments
Symbolic manes for constants
Labels for addresses
Macros
Directives
Directives – An instruction to the assembler program.
Symbolic, relative and absolute addressing:
An assembly program using symbolic addressing with explanations:
A simple assembly language program using relative and absolute addressing:
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 examples are:
Removal of comments
Replacements 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
Two-pass assembler – This translates assembly language code into machine code in two
stages:
First Pass: Collects label information and assigns addresses.
Second Pass: Resolves labels to addresses and generates the final machine code.
This approach ensures that labels can be used before they are defined and that all addresses
are correctly resolved.
The opcode is used for mnemonics and the oprand is used for the data.
Some points to not 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.
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 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.
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.
Addressing modes:
Addressing mode – When the instruction uses a value this defines how the operand must be
used to find the value
Assembly language instructions:
Data movement:
Some instruction formats for data movement:
Examples of some data stored in memory:
LDD 103
LDI 106
STO 106
LDD INDEXVALUE
MOV IX
LDX 102
Input and output – There are two instructions provided for input or output. In each case the
instruction has only 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 keyboard.
The instruction with opcode OUT is used to displayed on the screen the character for
which the ASCII code is stored in the ACC
Comparisons and jumps:
Arithmetic operations:
Shift operations:
There are two shift instruction 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
In a logical shift no consideration is given as to what the binary code in the accumulator
represents. Because a shift operation moves a bit from the accumulator into the carry bit.
Logical shift – Where bits in accumulator are shifted to the right or to the left and a
zero moves into the bit position vacated.
Cyclic shift – 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 the accumulator.
Bitwise logic operation:
Further consideration of assembly language
instructions:
Register transfer notation – We can use this to describe the execution of an instruction. For
example, the LDD instruction is described by:
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.