Assembly Language
Assembly Language
Key terms
Assembly language – a low-level chip/machine specific programming language that uses mnemonics.
Opcode – short for operation code, the part of a machine code instruction that identifies the action the
CPU will perform.
Operand – the part of a machine code instruction that identifies the data to be used by the CPU.
Assembler – a computer program that translates programming code written in assembly language into
machine code. Assemblers can be one pass or two pass.
Instruction set – the complete set of machine code instructions used by a CPU.
Addressing modes – different methods of using the operand part of a machine code instruction as a
memory address.
Absolute addressing – mode of addressing in which the contents of the memory location in the operand
are used.
Direct addressing – mode of addressing in which the contents of the memory location in the operand
are used, which is the same as absolute addressing.
Indirect addressing – mode of addressing in which the contents of the contents of the memory location
in the operand are used.
Indexed addressing – mode of addressing in which the contents of the memory location found by adding
the contents of the index register (IR) to the address of the memory location in the operand are used.
Immediate addressing – mode of addressing in which the value of the operand only is used.
Relative addressing – mode of addressing in which the memory address used is the current memory
address added to the operand.
Symbolic addressing – mode of addressing used in assembly language programming, where a label is
used instead of a value.
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.
Writing programs in machine code is a specialised task that is very time consuming and often error
prone, as the only way to test a program written in machine code is to run it 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.
The first programming language to be developed was assembly language, this is closely related to
machine code and uses mnemonics instead of binary.
The structure of assembly language and machine code instructions is the same.
Each instruction has an opcode that identifies the operation to be carried out by the CPU.
Most instructions also have an operand that identifies the data to be used by the opcode.
Stages of assembly
Before a program written in assembly language (source code) can be executed, it needs to be translated
into machine code.
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.
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.
Two pass assemblers need to scan the source program twice, so they can replace labels in the assembly
program with memory addresses in the machine code program.
Pass 1
» Add any new labels to the symbol table with the address, if known.
» Generate object code, including opcode and operand, from the symbol table generated in Pass 1.
» Save or execute the program. The second pass is required as some labels may be referred to before
their address is known.
If the program is to be loaded at memory address 100, and each memory location contains 16 bits, the
symbol table for this small section of program would look like this:
These instructions allow data stored at one location to be copied into the accumulator.
This data can then be stored at another location, used in a calculation, used for a comparison or output.
These instructions allow data to be read from the keyboard or output to the screen.
Compare instructions
Addressing modes
Assembly language and machine code programs use different addressing modes depending on the
requirements of the program.
Absolute addressing – the contents of the memory location in the operand are used. For example, if the
memory location with address 200 contained the value 20, the assembly language instruction LDD 200
would store 20 in the accumulator.
Direct addressing – the contents of the memory location in the operand are used. For example, if the
memory location with address 200 contained the value 20, the assembly language instruction LDD 200
would store 20 in the accumulator.
Indirect addressing – the contents of the contents of the memory location in the operand are used. For
example, if the memory location with address 200 contained the value 20 and the memory location with
address 20 contained the value 5, the assembly language instruction LDI 200 would store 5 in the
accumulator.
Indexed addressing – the contents of the memory location found by adding the contents of the index
register (IR) to the address of the memory location in the operand are used. For example, if IR contained
the value 4 and memory location with address 204 contained the value 17, the assembly language
instruction LDX 200 would store 17 in the accumulator.
Immediate addressing – the value of the operand only is used. For example, the assembly language
instruction LDM #200 would store 200 in the accumulator.
Relative addressing – the memory address used is the current memory address added to the operand.
For example, JMR #5 would transfer control to the instruction 5 locations after the current instruction.
Symbolic addressing – only used in assembly language programming. A label is used instead of a value.
For example, if the memory location with address labelled MyStore contained the value 20, the assembly
language instruction LDD MyStore would store 20 in the accumulator.
Labels make it easier to alter assembly language programs because when absolute addresses are used
every reference to that address needs to be edited if an extra instruction is added, for example.
Labels
In a high-level language, adding three numbers together and storing the answer would typically be
written as a single instruction:
The same task written in assembly language could look like this:
If the program is to be loaded at memory address 100 after translation and each memory location
contains 16bits, the symbol table for this small section of program would look like this:
When this section of code is executed, the contents of ACC, CIR and the variables used can be traced
using a trace table.
In a high-level language, adding a list of numbers together and storing the answer would typically be
written using a loop.
The same task written in assembly language would require the use of the index register (IX). The
assembly language program could look like this:
If the program is to be loaded at memory address 100 after translation and each memory location
contains 16bits, the symbol table for this small section of program would look like this:
When this section of code is executed the contents of ACC, CIR, IX and the variables used can be traced
using a trace table:
BIT MAP MANIPULATION.
Key terms
Shift – moving the bits stored in a register a given number of places within the register; there are
different types of shift.
Logical shift – bits shifted out of the register are replaced with zeros.
Cyclic shift – no bits are lost, bits shifted out of one end of the register are introduced at the other end of
the register.
Control – to automatically take readings from a device, then use the data from those readings to adjust
the device.
Mask – a number that is used with the logical operators AND, OR or XOR to identify, remove or set a
single bit or group of bits in an address or register.
Binary shifts
A shift involves moving the bits stored in a register a given number of places within the register.
Each bit within the register may be used for a different purpose. For example, in the IR each bit identifies
a different interrupt. There are several different types of shift.
Logical shift – bits shifted out of the register are replaced with zeros. For example, an 8-bit register
containing the binary value 10101111 shifted left logically three places would become 01111000.
Arithmetic shift – the sign of the number is preserved. For example, an 8-bit register containing the
binary value 10101111 shifted right arithmetically three places would become 11110101.
Cyclic shift – no bits are lost during a shift. Bits shifted out of one end of the register are introduced at
the other end of the register. For example, an 8-bit register containing the binary value 10101111 shifted
left cyclically three places would become 01111101.
Left shift – bits are shifted to the left; gives the direction of shift for logical, arithmetic and cyclic shifts.
Right shift – bits are shifted to the right; gives the direction of shift for logical, arithmetic and cyclic
shifts.
The logical shifts that you are expected to use in assembly language programming.
For example, a control system with eight different sensors would need to record when the data from
each sensor had been processed.
This could be shown using 8 different bits in the same memory location.
Instructions used to check, set and clear a single bit or group of bits
The assembly language code to test sensor 3 could be: