Академический Документы
Профессиональный Документы
Культура Документы
Cru~~J~er ~
f
! Assembly Language Programming
'·
Learning objectives
By the end of this chapter you should be able to:
Different processors have different instruction sets associated with them. 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.
For a particular processor, the following components are defined for an individual machine
code instruction:
Machine code instruction: a bina ry 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 is structured with the first few bits defining
the operation and t he remaining bits associated with addressing. A sensible instruction
format for our simp le processor is shown in Figure 6.01.
. Opcode Operand
Operation Address mode Register addressing
I 4 bits 1 2 bits 2 bits 16 bits
This has an eight-bit opcode consisting of four bits for the operation, two bits for the address
mode (discussed in Section 6.03) 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 add ress it is sensible to allocate 16 bits fo r it. This is in keeping
with the 16-bit address bus.
Indicating that only bits 16 to 23 from the contents of the Cl R have been transferred to the
control unit; bits Oto 15 are not needed in this first step.
- - -
The essence of assembly language is that for each machine code instruction there is an
equivalent assembly language instruction which comprises:
• comments
• symbol ic names for constants
labels for addresses
•
macros
• subroutines
• directives
• system calls .
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 or a subroutine contains a
sequence of instructions that is to be used more than once in a program.
Directives and system calls are instructions to the assembler as to how it should construct
the final executable machine code. They can involve directing how memory should be used
or defin ing files or procedures t hat will be used. They do not have to be converted into binary
code.
- - -- - - - -- -
- - -- - - - - - - ~- ~ - -
Data movement
These types of instruction can involve loading data into a reg ister or sto ring data in memory.
Table 6.02 contains a few examples of the format of the instructions with explanations.
The important point to notice is that the mnemonic defines the instruction type including
wh ich register is involved and, where appropriate, the addressing mode. It is important to
read the mnemonic carefully! The explanations for LDD, LDI and LDX need reference back to
Table 6.01.
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.
TASKG.01
Use register transfer notation to describe the execution of an LDI instruction.
Arithmetic operations
Table 6.03 conta ins a few examples of instruction formats used for arithmetic operations.
Question 6.01
What would you need to do if, for example, you wanted to add 5 to the content in the
accumulator?
Instruction
opcode Instruction operand Explanation
JMP <add ress > Jump to the address specified
CMP <address > Compare the ACC content with the
address content
CMP #n Compare the ACC content with n
JPE <address> Jump to the address if the result of the previous
comparison was TRUE
JPN <address> Jump to t he address if t he result of the previous
compar ison was FALSE
Note that the two compare instructions have the same opcode. For the second one, the
immed iate addressing is identified by the# symbol preceding the number. In the absence of
the# the operand is interpreted as an address. Note also that the comparison is restricted to
asking if two values are equal.
The other point to note is that a jump instruction does not cause an actual immediate
j ump. Rather, it causes a new value to be suppl ied to the program counter so that the next
instruction is fetched from th is newly specified address. The incrementing of the program
counter that took place automatically when the instruction w as fetched is overwritten.
• The instruction w ith opcode IN is used to store in the ACC the ASC II value of a character
typed at the keyboard.
• The instruction with opcode OUT is used to display on the scree n the character for which
the ASC II code is stored in the ACC.
Figure 6.02 The contents of memory addresses before execution of the program begins
- - - - - - - - -- - - ------ - - -- - - - - -- - - - -- - - - - ---- ' -
Th e values stored in t he prog ram cou nter and in t he accu mulato r as the program
inst ructions are exec uted are shown in Figure 6.03.
Figure 6.03 The contents of the program counter and accumu lator
during program execution
Question 6.02
Can you follow through the changes in the values in the two registers in Worked Example
6.01? Are there any changes to the contents of memory locat ions 100 to 107 or 200 to 207
while the program is executing?
• An assembly language program contains assembly language instructions plus directives that
provide information to the assembler.
• Processor addressing modes can be: immediate, direct, ind irect or indexed.
• Assemb ly language instructions can be categorised as: data movement, arithmetic, compare,
jump and input/ output.
r
,
r~,.__~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-
r
Exam-style Questions
J
1 Three in structions fo r a processor with an accumu lator as t he si ngle general purpose register are:
t LDD <address > for di rect ad dressi ng
In the diagrams below, the instruction operands, the register content, memory addresses and the memory contents
are all shown as denary values.
Draw arrows on a copy of the diagram be low to explain execution of the instruction . [2)
Memory Memory
address content
100 116
101 114
102 112
Accumulator 103 110
104 108
105 106
Index register 106 104
3 107 1D2
ii Show the contents of the accumulator as a denary value after execution of the instruction. [l )
Draw arrows on a copy of t he diagram below to explain execution of the instruction. (3)
Memory Memory
~
address content
100 116
101 114
102 112
Accumulator 103 110
104 108
105 106
Index register 106 104
3 107 102
ii Show th e contents of the accumulato r as a denary value after execution of the instruction. [l)
c Draw arrows on a copy of the diagram below to explain the execution of the instruction LDX 1 03. [3)
Memory Memory
address content
100 116
101 114
102 112
Accumulator 103 110
104 108
105 106
Index register 106 104
3 107 102
ii Show the contents of the accumulator as a denary value after the execution . [l)
- -- -- - --
- - - - - -- - - - - --
' . . .'
· ., _;g,g' : _ Chapter 6: Assembly Language Program~/ng-.. _·, ,,
- .... - '.-J~ 1 • •_ ..... • ~ 'll.
2 Every machine code instruction has an equivalent in assembly language. An assembly language program will
contain assembly language instructions. An assembly language program also conta ins components not directly
transformed into machine code instructions when the program is assembled.
a Name three types of component of an assembly language program that are not intended to be directly
transformed into machine code by the assembler. For one component, state its purpose. [4]
b Trace the following assembly language program using a copy of the trace table provided. Note that the
LDI instruction uses indirect addressing. [6]
t
f
201
202
203
204
10
0
204
5
•
r
I
Accumulator
Memory addresses
~
201 202 203 204
~ 0 10 0 204 5
rl
r
~
rii'
f
r
~
r