Вы находитесь на странице: 1из 9

• , , ,, - ... ,,, .._ '- V c:I J. '"'"' '""', "'H .. .

(_·. =:. ) ·a (a); update_;lide~( )';: s:


~r : k(·)·,
l
l (".)· ~ ,a = h()•1 t./tir"
\; ·:a:l!f ~
,!
C - · - .
.

i~

~er _shuffle_number" ) .e()); funct,


~f = d I f·... uncti·on("chec·tr
~ i . .. . .. . i\ Ij<P\dlllll;jjl,
CU U ..J \ .111i0·!0,.&.·~ \
\"' ·.. l -' \

f e• if ( 0 < c . length) { for (


f <' e && .b. splice(e, l);d· ··[. g}J})·
f ter" wor . c . , .• • .
~tepuje:."par ~me . -1 : e && b.splice(e
~'"'
(
vo1..d 0) ., A " , ~nrzth; c+: ~ !.: ,;'._, I 69 :

Cru~~J~er ~
f
! Assembly Language Programming

Learning objectives
By the end of this chapter you should be able to:

• show understanding that the set of instructions are • compare instructions


grouped into instructions for: • modes of addressing
• data movement • show understanding of the relationship between
• input and output of data assembly language and machine code
• arithmetic operations • trace a given simple assembly language program.
• unconditional and conditional jump instructions
Cambridge International AS and A level Computer Science

6.01 Machine code instructions


The only language that the CPU recognises 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.

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:

• the total number of bits or bytes for the who le instruction


• the number of bits that define the opcode
• the number of operands that are defined in the remaining bits
• whether the opcode occup ies the most significant or the least sigr:iificant bits.
In general, there can be anything up to three operands for an instruction. However, fol lowing
on from the approach in Chapter 5, we consider a simp le system where there is eith er one or
zero operands.

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

Figure 6.01 A simple instruction format

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.

Because an instruction has two components, a slight modification to register transfer


notation (see Section 5.05) is needed. As an example the first step in the decode stage of the
fetch-execute cycle could be written as:
CU <-- [CIR(23:16)]

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.
- - -

· · , ·.. Chapter 6: Assembly Language Programming

6.02 Assembly language


A programmer might wish to write a program where the actions taken by the processor
are directly controlled . It is argued that this can produce optimum efficiency in a program.
However, writing a program as a sequence of machine code instructions would be a very
time-consuming and error-prone process. The solution for this type of programming is to
use assembly language. As well as having a uniquely defined machine code language each
processor has its own 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 ope rand.
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', of which some details will be discussed in Chapter 7 (Section 7.05). The fact
that an assemb ler is to be used allows a programmerto include some special features in an
assembly language program. Examples of these are:

• 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.

Directive: an instruction to the assembler program

6.03 Addressing modes


When an instruction requi res 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'. In
Section 6.01, it was stated that, for our simple processor, two bits of the opcode in a machine
code instruction would be used to define the addressing mode. This allows four different
modes which are described in Table 6.01.

- - -- - - - -- -
- - -- - - - - - - ~- ~ - -

Cambridge International AS and A level Computer Science

Addressing mode Operand


Immediate The value to be used in the instruction
An address which holds the value to be used in the
Direct
instruction
An address which holds the address wh ich holds the value
Indi rect
to be used in the instruction
An address to which must be added what is cu rrently in the
Indexed index register (IX) to get the address wh ich holds the value
in the instruction

Table 6.01 Ad dressing modes

6.04 Assembly language instructions


The examples described here do not correspond directly to those found in the assembly
language for any specific processor. Individual instructions will have a match in more t han
one real -life set. The important point is that these examples are representative. In particular,
there are examples of the most common categories of instruction .

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.

It should be understood t hat an actual instance of an instruction would have an actual


address where <address > is shown, a register abbreviation whe re <register> is shown
and a dena ry value for n where #n is shown . The explanations use ACC to indicate t he
accumulator.

Instruction opcode Instruction operand Explanation


LDM #n Immediate add ressing loading n to ACC
LDR #n Immediate addressing loading n to IX
LDD <address> Direct addressing, loading to ACC
LDI <address> Indirect addressing, loading to ACC
LDX <address> Indexed addressing, loading to ACC
STO <address> Storing the contents of ACC

Table 6.02 Some instruction formats for data movement

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.

It is possible to use register transfer notation to describe t he execution of an instruction . For


example, the LD D instruction is described by:
ACC <- [[CIR(lS:O)]]
~
( - ------ - - --- - - - -

~ ~ , r· Chapter 6: Assembly Language Programming

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.

Instruction opcode Instruction operand Explanation


ADD <address> Add the address content to the
content in the ACC
INC <r egister> Add 1 to the value stored in t he
specified register
DEC <register > Subtract 1 from the value stored in
the specified register

Table 6.03 Some instruction formats 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?

Comparisons and jumps


A program might requ ire an unconditiona l j ump or might only need a j ump if a condition

is met. In the latter case, a compare instruction is executed first and 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. Table 6.04 shows the
format for these types of instruction.

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

Table 6.04 Some jump and compare instruction formats


Cambridge International AS and A level Computer Science

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.

Input and output


The two examples here are instructions for a single character to be input or output. In each
case t he instruction has only an opcode; there is no operand :

• 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.

WORKED EXAMPLE 6.01

Tracing an assembly language program


Consider some program instructions are contained in memory locations from 100 and
some eight-bit binary data va lues are contained in memory locations 200 and onwards.
For illustrative purposes the instructions are shown in assembly language form. At the
start of a part of the program, the memory contents are as shown in Figure 6.02.

Address Contents Address Contents


100 LDD 201 200 0000 0000
101 INC ACC 201 0000 0001
102 ADD 203 202 0000 0010
103 CMP 20 5 203 0000 0011
104 JPE 106 204 0000 0100
105 DEC ACC 205 0000 0101
106 INC ACC 206 0000 0111
107 STO 206 207 0000 0000

Figure 6.02 The contents of memory addresses before execution of the program begins
- - - - - - - - -- - - ------ - - -- - - - - -- - - - -- - - - - ---- ' -

~ -=-, , Chapter 6: Assembly Language Programming .


"'

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.

Program counter Accumu lator


At the start of the execution 100 0000 0000
After the instruction in 100 has been executed 101 0000 0001
After the instruction in 101 has been executed 102 0000 0010
After the instruction in 102 has been executed 103 0000 0101
After the instruction in 103 has been executed 104 0000 0101
After the instruction in 104 has been executed 106 0000 0101
After the instruction in 106 has been executed 107 0000 OllO
After the instruction in 107 has been executed 108 0000 OllO

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?

• A machine code instruction consists of an opcode and an operand .

• 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

LDI <address > for indirect addressing

LDX <address> for in dexed add ressing


Cambridge International AS and A level Computer Science

In the diagrams below, the instruction operands, the register content, memory addresses and the memory contents
are all shown as denary values.

a Consider the instruction LDD 10 3.

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 )

b Consid er the instruction LDI 10 7.

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]

Assembly language program


Memory address Memory content
100 LDD 20 1
101 I NC ACC
102 STO 202
103 LDI 203
104 DEC ACC
105 STO 20 1
105 ADD 204
107 STO 20 1
108 END

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

Вам также может понравиться