Chapter 4
Writing a Simple Program in an Assembly Language
This chapter gives an overview of a program developed in an assembly
language used by the H8/300H. Only basic instructions are introduced here to
help you understand how a program proceeds and how the contents of the
registers and memory change as it progresses.
To understand these subjects, you need a knowledge of binary numbers
described in Chapter 2. Learn how a program is configured and proceeds
before going on to the next chapter which explains instructions in detail.
The assembly language is the most basic programming language and
corresponds to machine instructions one-to-one, making it the most suitable
language for understanding microcomputer operation. Although C-language is
also becoming popular in the microcomputer field, studying programs written
in the assembly language will be very helpful for developing a program with
C-language afterward. The CPU can execute machine instructions only. No
matter in which language a program is written, it must be converted into
machine instructions in the end. Since machine instructions are collections of
0s and 1s, it is difficult to develop a program directly with machine language.
For this reason, assembly language is used since it enables machine language
to be expressed in easily understandable alphabets. For example, a machine
instruction to add the R0L and R1L as an arithmetic register is expressed as
follows in 16 bits:
0000 1000 1000 1001
In assembly language, it is expressed as follows:
ADD.B R0L,R1L
A program written in assembly language is referred to as a source
program.
4.1 CPU Internal Registers
Before developing a program with assembly language, you need to
know what kinds of registers and functions the CPU has. Figure 4.1 shows the
CPU internal registers of the H8/300H.
Page 29
Figure 4.1: CPU Internal Registers
The internal registers are classified into general-purpose and control
registers.
The general-purpose registers are used to calculate data and store
addresses.
The control register is further classified into the PC (program counter)
to control program progress and the CCR (condition code register) to test
conditions.
How to use general-purpose registers
The CPU has 8 general-purpose registers, each capable of storing 32digit binary numbers.In addition to 32-bit data, they can also store 16- or 8-bit
data.
When 32-bit data is stored, they are described as follows in an instruction,
using 8 registers in all:
ER0, ER1, ER2, ER3, ER4, ER5, ER6, ER7
When 16-bit data is stored, they are described as follows in an instruction,
using registers as 16 units in all:
E0, E1, E2, E3, E4, E5, E6, E7, R0, R1, R2, R3, R4, R5, R6, R7
When 8-bit data is stored, they are described as follows in an instruction, using
registers as 16 units in all:
R0H, R0L, R1H, R1L, R2H, R2L, R3H, R3L, R4H, R4L, R5H, R5L, R6H,
R6L, R7H, R7L
Page 30
This is illustrated in Figure 4.2.
Figure 4.2: General-purpose Register
Example of calculation by general-purpose registers
In this example, an add instruction is used to show how general-purpose
registers are actually used.
ADD.B R0L,R1H is an instruction for 8-bit addition. ADD represents
"ADDition" and B represents "Byte" (8 bits). The contents of the R1H and R0L
are added and the results are stored in the R1H.
This will not influence the E1 or R1L. Only 8-bit results are obtained.
Any 8-bit register is available for this calculation. For example, you can
specify the same register like "ADD.B R1L,R1L". In this case, the R1L is
doubled.
ADD.W R0,E1 is an instruction for 16-bit addition. ADD represents
"ADDition" and W represents "Word" (16 bits). The contents of the E1 and R0
are added and the results are stored in the E1.
Page 31
This will not influence the R1.Only 16-bit results are obtained.
ADD.L ER0,ER1 is an instruction for 32-bit addition.ADD represents
"ADDition" and L represents "Long word" (32 bits). The contents of the ER1
and ER0 are added and the results are stored in the ER1.
The 32-bit results are stored in the ER1.
SP (stack pointer)
A special function has been added to the ER7 as a stack pointer. The
ER7 is usually not used for calculation but as a stack pointer. The stack pointer
function is described in detail in "Subroutines" and "Interrupt Operations".
PC (program counter)
n the program counter, the "address of the instruction to be executed
next" is always stored and the data is automatically updated every time the
CPU reads instructions. Since the addresses are 24 bits, the PC also has 24-bit
configuration. Programmers need not pay special attention to how the PC is
configured. Every time an instruction is read, the address of the next instruction
is automatically stored.
In the case of the H8/300H, an instruction is always read from an evennumbered address first. This means that an even-numbered address is always
stored in the PC (see "Data in the memory").
CCR (condition code register)
This is used to control interrupts or test conditions. Although it is an 8bit register, every bit has a different meaning. Interrupt control is described in
detail in "Exception Handling".
This section describes the part used for conditional test. Every time an
instruction is executed, the N, Z, V and C bits change to reflect the results.
Conditions are tested based on their changes. An instruction to be tested exists
separately. The N, Z, V and C bits are called "flags".
N (Negative) flag: When the execution results are regarded to be a signed
binary number, set to 1 if it is negative, or 0 if positive.
Z (Zero) flag: Set to 1 if the execution results are zero, otherwise, 0.
V (oVerflow) flag: When the execution results are regarded to be a signed
binary number, set to 1 if it overflows, otherwise, 0.
C (Carry) flag: Set to 1 if execution results in a carry or borrow, otherwise, 0.
Page 32
Conditional test in a program is performed by these four flags. Any
condition can be tested using them.
Conditional test using the CCR
As for two numeric values, X and Y, let's consider how to test their
collating sequence.To test the collating sequence, subtraction is used. By
subtracting Y from X, the sequence can be tested based on how N, Z, V and C
in the CCR change.
Let's assume that C becomes 1 after subtracting Y from X. This means
that a borrow occurred after subtraction. A borrow occurs when X is less than
Y. If a borrow does not occur, you can judge that X is equal to or greater than
Y.
If Z is 1, X is equal to Y since the subtraction results are zero.If Z is 0, you can
judge that X is not equal to Y.
As described above, the collating sequence can be tested based on C
and Z obtained after subtraction. In this case, however, X and Y are assumed to
be unsigned binary numbers. If they are signed binary numbers, the N or V
flag, instead of C, is used for conditional test.
Data in the memory
The following describes how to store 8-, 16- and 32-bit data into the
memory.
Not only the H8/300H but all 16-bit microcomputers use 8 bits of the memory
per address. So, one 8-bit data block exactly occupies one address.
One 16-bit data block occupies two addresses. The upper 8 bits are stored in a
smaller address and the lower 8 bits in a larger one. The smaller one must be an
even-numbered address. Although each data block is stored separately in two
addresses, the smaller one is regarded to be the address storing the data. For
example, "16-bit data in the H'1000 address" means that the upper 8 bits are
stored in the H'1000 address and the lower in the H'1001 address.
Page 33
In an instruction to read or write 16-bit data, you should specify an
even-numbered address (smaller address). If you attempt to read or write 16-bit
data by specifying an odd-numbered address, reading/writing will fail. For the
reason why this restriction applies, refer to "Connecting CPU to Memory (16bit Data Bus)".
In the case of the H8/300H, an instruction is always read in 16-bit
units. This means that an instruction must be stored in an even-numbered
address. H8/300H machine instructions are composed in 16-bit integral
multiples. If the first instruction falls in an even-numbered address, the
subsequent instructions also fall in even-numbered addresses.
One 32-bit data block occupies four addresses of the memory. Since the
H8/300H cannot read or write 32-bit data at a time, data are divided into 16-bit
units for reading/writing. In this case, the first data must also fall in an evennumbered address. Likewise, the most significant 8 bits are stored in the
smallest address and the least significant 8 bits in the largest one.
[Explanation with motion pictures and sound]
Page 34
1. ( T ) There are eight 32-bit general-purpose registers in all.
There are eight general-purpose registers, from ER0 to ER7.
2. ( T ) The ER7 is a stack pointer.
Among the general-purpose registers, only the ER7 has a special stack
pointer function.
3. ( F ) The CCR is a 16-bit register.
The CCR (Condition Code Register) is a control registers with 8-bit
configuration.
4. ( F ) The PC stores the instruction currently being executed.
The PC (Program Counter) does not store instructions but the
"address" of the instruction to be executed next.
5. ( F ) Although the ER0 can perform addition, the ER6 cannot.
All general purpose registers from ER0 to ER7 can handle the same
instructions.(The ER7, however, has a special stack pointer function)
6. ( T ) The least significant 8 bits of the ER0 is the R0L.
The upper 16 bits of the ER0 are the E0 and the lower 16 bits are the
R0.And the upper 8 bits of the R0 are the R0H and the lower 8 bits are the R0L.
7. ( F ) The upper 16 bits of the ER0 is the R0.
The upper 16 bits of the ER0 are the E0 and the lower 16 bits are the
R0.
8. ( F ) The Z flag in the CCR is set to zero when calculation results in
zero.
Since this flag is named "Zero", it is set to 1 when calculation results in
zero.
9. ( T ) The N flag in the CCR is set to zero when the calculation results are
positive.
Since this flag is named "Negative", it is set to 1 when the calculation
results are negative. Zero when positive.
10. ( F )The C flag in the CCR is set to zero when calculation results in a
carry.
Since this flag is named "Carry", it is set to 1 when calculation results
in a carry. Otherwise, zero.
11. ( T )One address of the memory is 8 bits.
Except for special microcomputers such as 4-bit types, 8 bits (1 byte) of
the memory are used per address.
12. ( T )8-bit data can be stored in both even- and odd-numbered
addresses.
Since 8-bit data exactly occupies one address of the memory, it can be
stored in either an even- or odd-numbered address.
13. ( T )16-bit data must be stored in an even-numbered address.
Since the H8/3048 reads and writes 16 bits of data at a time, the upper
8 bits must be stored in an even-numbered address and the lower 8 bits in the
next address. If 16-bit data is stored in an odd-numbered address and the next
even-numbered address, reading/writing will fail.
Page 35
4.2 Instruction Configuration
This section describes some basic instructions used in assembly
language. And the subsequent sections explain how to develop a program using
them.
MOV instruction
The MOV (MOVe data) instruction is used for data transfer. Although
"transfer" may sound like moving the original data, the function of this
instruction is similar to copying and the original data remains.
It is available from the memory to a general-purpose register, from a generalpurpose register to the memory, between general-purpose registers and from
data to a general-purpose register. This instruction is most frequently used in a
program.
Samples
Transfers 8-bit data from the R0L to the R1L.
MOV.B
R0L,R1L
MOV.B
@H'1000,R0L Transfers the 8 bits in the H'1000 address to the R0L.
MOV.B
R1L,@H'2000 Transfers the R1L to the 8 bits in the H'2000 address.
MOV.B
#1,R0L
Inputs (transfers) data "1" in the R0L.
ADD instruction
The ADD (ADD binary) instruction is used for addition. The results are
stored in the general-purpose register written on the right.
Samples
Adds the R1L and R0L and stores the results in the R1L.
ADD.B
R0L,R1L
ADD.B
#H'12,R0L Adds the R0L and H'12 (18 in decimal notation) and stores
the results in the R0L.
SUB instruction
The SUB instruction (SUBtract binary) is used for subtraction. It
subtracts the contents of the general-purpose register written on the left from
those on the right and stores the results in the register written on the right.
Sample
SUB.B
R0L,R1L
Subtracts the R0L from the R1L and stores the results in
the R1L.
CMP instruction
The CMP (CoMPare) instruction is used for comparison. It performs
subtraction not to obtain the results but simply for comparison. What matters
most is not what the answer is but how N, Z, V and C in the CCR change after
subtraction. In other words, the CMP instruction simply performs subtraction
and changes N, Z, V and C in the CCR.
A CMP instruction must be followed by a conditional branch
instruction. This is because comparison is meaningless without conditional test.
Page 36
Samples
Subtracts the R0L from the R1L, changing the CCR.
CMP.B
R0L,R1L
Conditional branch instruction
Subtracts H'12 (18 in decimal notation) from the R0L,
CMP.B
#H'12,R0L
changing the CCR.
Conditional branch instruction
BRA instruction
The BRA (BRanch Always) instruction is called "unconditional branch
instruction". Executing this instruction results in branching to the specified
address. Branching is similar to "jumping". It causes jumping forward or
backward, skipping some instructions.
The destination address is specified by giving it a name ("symbol")
Sample
BRA ABC Unconditionally branches to the symbol ABC.
Instruction
ABC: Instruction
BGT instruction
The BGT (Branch Greater Than) instruction is one type of conditional
branch instruction. It compares data as a "signed binary number" and branches
to the specified instruction if it is greater. Otherwise, it does nothing and the
next instruction is executed.
Sample
CMP.B
R0L,R1L
Compares the R1L with the R0L.
BGT
ABC
If the R1L is greater, branches to the symbol ABC.
Instruction
Otherwise, the next instruction is executed.
Instruction
ABC: Instruction
BHI instruction
The BHI (Branch HIgh) instruction is another type of conditional
branch instruction. It compares data as an "unsigned binary number" and
branches to the specified instruction if it is greater. Otherwise, it does nothing
and the next instruction is executed.
Sample
Compares the R1L with the R0L.
CMP.B
R0L,R1L
If the R1L is greater, branches to the symbol ABC.
BHI
ABC
Instruction
Otherwise, the next instruction is executed.
Instruction
ABC: Instruction
Page 37
4.3 Adder Program
4.3.1 How to Develop a Source Program
This section describes how to develop a source program to add 8-bit
data with assembly language.
It is assumed that 8-bit unsigned binary numbers are stored in the
H'2000 and H'2001 addresses of the memory. Here, you will create a program
to add these two data blocks and write the results in the H'2002 address. Up to
8 bits of results are obtained even if addition results in a carry, generating 9
bits.
Since addition is performed, the following instruction is used:
ADD.B R0L,R1L
Any 8-bit register can be used as general-purpose registers other than
the R0L or R1L.
To perform addition using this instruction, you must input data to be added in
the R0L and R1L beforehand. To input data from the H'2000 address of the
memory to the R0L general-purpose register, use the following instruction:
MOV.B @H'2000,R0L
To input data from the H'2001 address to the R1L general-purpose register, use
the following instruction:
MOV.B @H'2001,R1L
H'2000 represents an address in hexadecimal notation. Memory addresses are
generally expressed in this notation. "@" is a mandatory prefix to indicate a
memory address.
The above instructions should be arranged as follows for addition:
MOV.B
@H'2000,R0L
MOV.B
@H'2001,R1L
ADD.B
R0L,R1L
This, however, simply stores the addition results in the R1L and they
are not written in the H'2002 address of the memory.
MOV.B
R1L, @H'2002
Use the above instruction to write the addition results in the H'2002
address of the memory.
MOV.B
@H'2000,R1L
MOV.B
@H'2001,R1L
ADD.B
R0L,R1L
MOV.B
R1L,@H'2002
Consequently, addition is completed with the above four instructions.
Simply arranging these four instructions, however, will not make a complete
program.
Page 38
After reading one instruction, the CPU automatically stores the address
of the next instruction in the PC and reads the next instruction after execution
is completed. Since the CPU does not understand whether the next address has
an instruction or not, it assumes that there must be an instruction in the next
address and executes it even after executing the above four instructions. This
results in a runaway since the CPU executes non-existing instructions. To
prevent this, use the BRA instruction as follows:
MOV.B
@H'2000,R1L
MOV.B
@H'2001,R1L
ADD.B
R0L,R1L
MOV.B
R1L,@H'2002
ABC: BRA ABC
The above instruction leads to unlimited execution of the BRA
instruction. This prevents the program from proceeding and running away.
The program, however, is still incomplete.
This program does not indicate at which address of the memory the
program itself should be located. It is indicated using an assembler control
instruction. The assembler control instruction is not executed by the CPU but
used to instruct an assembler, which is machine language conversion software.
.SECTION
PROG,CODE,LOCATE=H'1000
Use the control instruction shown above. Every assembler control
instruction is prefixed with "." (period). With it, you can easily distinguish
between assembler control instructions and those executed by the CPU
(execution instructions).
.SECTION indicates the section control instruction, PROG represents
the section name (section can be named originally based on certain rules),
CODE refers to the instruction code, and LOCATE=H'1000 specifies that
instructions should be located starting from the H'1000 address of the memory.
The .CPU control instruction to specify the CPU type is also required
since the assembler for the H8/300H is compatible with several CPU types. In
addition, the .END control instruction must be written on the last line.
Finally, a complete program is written as shown in List 4.1.
List 4.1: Simplest Program
Page 39
This source program is converted into machine instructions by the
assembler as follows:
Address
Machine instruction Instruction
H'001000
H'001004
H'001008
H'00100A
H'00100E
6A082000
6A092001
0889
6A892002
40FE
ABC:
.CPU
.SECTION
300HA
PROG,CODE,LOCATE=H'1000
MOV.B
MOV.B
ADD.B
MOV.B
BRA
.END
@H'2000,R0L
@H'2001,R1L
R0L,R1L
R1L,@H'2002
ABC
The machine instruction is expressed in hexadecimal notation. Since
one address of the memory is 8 bits, it is expressed with a 2-digit, hexadecimal
number. Since the machine instruction "MOV.B @H'2000,R0L" is eight digits,
four addresses of the memory are used to store it (called "4-byte instruction").
In the case of the H8/300H, the shortest instruction is 2 bytes and the longest is
10 bytes.
The first MOV instruction is stored in the four addresses starting from
H'1000 (H'001000) and the next MOV instruction in the four addresses from
H'1004. The ADD instruction is stored from H'1008, the next MOV instruction
from H'100A and the last BRA instruction from H'100E. Since .CPU,
.SECTION and .END are control instructions, they do not correspond to
machine instructions.
Let's consider how the contents of general-purpose registers and the
memory change when a program is executed. It is assumed that H'4C
(B'01001100) is stored in the H'2000 address and H'40 (B'01000000) in the
H'2001 address. Also, the contents of the R0L general-purpose register is
assumed to be H'00 and those of the R1L to be H'00.
[Simulation]
Page 40
4.3.2 Rules on Source Programs
There are some rules when developing source programs in assembly
language. If they are not followed, an error will occur on assembly. This
section describes the rules relating to source programs.
Configuration of an instruction
An instruction is configured as follows:
Sample instruction
ADD.B R0L,R1L
ADD is the operation portion of the instruction representing "ADDition".
.B is the size specification portion indicating that the instruction's operation is in 8 bit
units.
(.W represents 16 bits and L represents 32 bits.)
The R0L and R1L are collectively called "operand", which is an operation target.
The R0L (on the left) is specifically called the source operand.
And the R1L (on the right) is specifically called the destination operand.
The results and answers to calculations are stored in the destination operand.
Note that some instructions have only one operand (destination operand) or
none at all.
Sample instruction with one operand
Sample instruction with no operand
INC.B R0L
RTS
How to write one line (without a symbol)
Rules: One or more spaces or tabs must be placed at the beginning.
Instructions and operands must be separated by one or more spaces or tabs.
An instruction may be written in both upper and lower cases.
A line must end with a return.
Page 41
Samples:
MOV.B
MOV.B R0L,R1L
mov.b
r0l,r1l
Mov.b
@h'1000,R1h
R0L,R1L
MOV.BR0L,R1L
Good sample
Good sample
Good sample
Bad sample (no space or tab at the beginning)
Bad sample (instruction and operand are not
separated by one or more spaces or tabs)
How to write one line (with a symbol)
Rules: Write a symbol first. Suffix the symbol with ":" (colon).
The rest are the same for a line without a symbol.
Rules on symbols: Available characters are A to Z, a to z, 0 to 9, _ and $
Upper and lower cases are handled as different characters
The first character must not be a numeric value
The same name as a CPU internal register must not be used
Samples:
Good sample
LOOP: MOV.B R0L,R1L
R1:
MOV.B R0L,R1L
Bad sample (the same name as an internal
register is used as a symbol)
Samples available as symbols:
Loop
Upper and lower cases may be mixed
"_" is available as a character
End_of_Loop
A numeric value can be used except at the beginning
DATA1
Samples not available as symbols:
1second
Starts with a numeric value
Second/100
"/" is used
Total.Data
"." is used
CCR
The same name as an internal register
Lines with symbols only
Only symbols may be written on lines. In this case, they are treated like
those written for the following instruction. So, the two samples below represent
exactly the same program:
Samples:
LOOP: MOV.B R0L,R1L
Sample with symbol and instruction
written on the same line
Sample with symbol and instruction
LOOP:
MOV.B
R0L,R1L
written on different lines
4.3.3 Inserting Comments
Comments are inserted to make programs readily understood. They serve as
memos and have no influence on program operation. Comments can be
Page 42
inserted in two ways. One is to place ";" (semicolon) at the beginning of a line,
which causes the entire line to be treated as a comment. All characters such as
alphabets, numeric values and special symbols can be used.
Sample:
;********************************
;* H8/300H Sample program
*
;* 2002.9.1
*
;********************************
Another is to insert a comment by suffixing an instruction with ";".This
way, you can add a comment to each instruction.
Program operation is easy to understand when each instruction has a comment.
In List 4.2, comments have been added to the adder program described earlier.
List 4.2: Program with Comments
4.3.4 How to Use .EQU Control Instruction
If a memory address is written in hexadecimal notation in a program, it
is difficult to determine what kind of data is included. So, it is helpful if an
address including data can be expressed by a symbol, rather than in
hexadecimal notation. The .EQU control instruction is the simplest way to
express an address with a symbol.
DATA1:
.EQU H'2000
DATA2:
.EQU H'2001
ANSWER:
.EQU H'2002
The above instructions make DATA1 equal to H'2000.For example, in
the following instruction:
MOV.B
@DATA1,R0L
DATA1 is converted into H'2000 by the assembler and it becomes
equal to:
MOV.B
@H'2000,R0L
Page 43
This method is useful when numeric values and addresses are fixed and
will not be changed. List 4.3 shows a program rewritten with this method.
List 4.3: Program Using .EQU
4.3.5 How to Use .RES Control Instruction
The .RES control instruction is used to reserve an area for writing in the
RAM.A RAM address is generally specified not by the .EQU control
instruction but by a combination of .RES and .SECTION control instructions.
This is because the .RES control instruction has the following benefits:
The beginning address can be freely changed using .SECTION
Data areas can be easily inserted or deleted
The .RES control instruction is used as follows:
Samples:
.SECTION
WORK,DATA,LOCATE=H'2000
AB: .RES.L
1
; Reserves one 32-bit area using the symbol AB.
; Reserves one 8-bit area using the symbol CD.
CD: .RES.B
1
.RES.B
1
; Simply reserves one 8-bit area. This is used to correct the
16- or 32-bit area to be reserved next to an even-numbered address.
EF:
.RES.W
XYZ: .RES.B
2
6
; Reserves two 16-bit areas using the symbol EF.
; Reserves six 8-bit areas using the symbol XYZ.
A symbol attached to a reserved area represents the address.
In the example below, since the WORK section is located at the H'2000
address, DATA1, DATA2 and ANSWER represent the H'2000, H'2004 and
H'2006 addresses respectively.
.SECTION
WORK,DATA,LOCATE=H'2000
DATA1:
.RES.L
1
Page 44
DATA2:
ANSWER:
.RES.W
.RES.B
1
1
If the RAM starts from the H'2000 address as with the program
described earlier, write as follows to reserve an area for writing there:
.SECTION
WORK,DATA,LOCATE=H'2000
DATA1:
.RES.B
1
DATA2:
.RES.B
1
ANSWER:
.RES.B
1
In the above, DATA1, DATA2 and ANSWER represent the H'2000,
H'2001 and H'2002 addresses respectively.
List 4.4 shows a program rewritten using .RES control instructions.
You can see that the ROM starts from the H'1000 address, in which a
program is stored, and the RAM starts from the H'2000 address, which is used
as a work area.
Page 45
List 4.4: Program Using .RES
4.3.6 How to Use .DATA Control Instruction
The programs described so far require that values to be added be
written at the DATA1 and DATA2 addresses by some means before they are
executed. This is because DATA1 and DATA2 are stored in the RAM. Since
the contents of the RAM are cleared when it is turned off, it is unpredictable
what are stored there after it is turned on again. In other words, you cannot
determine what must be included in the RAM data area. In the RAM, you can
only reserve an area for writing data temporarily.
On the contrary, the .DATA control instruction is used to set a certain
value in the ROM. Although the use is similar to .RES, it differs in that the
control instruction is followed by "the value to be set in an area", not by "the
count of areas".
Samples:
.SECTION
WORK,DATA,LOCATE=H'1100
; Reserves an 8-bit area including a value "10" using the
AB: .DATA.B
10
symbol AB.
CD:
.DATA.B
H'A6 ; Reserves an 8-bit area including a value "H'A6" using the
EF:
.DATA.W
H'12AB; Reserves a 16-bit area including a value "H'12AB" using
symbol CD.
XYZ: .DATA.L
40000
the symbol EF.
; Reserves a 32-bit area including a value "40000" using
the symbol XYZ.
In the case of the .DATA control instruction, a symbol attached to a
reserved area also represents the address.
.SECTION
WORK,DATA,LOCATE=H'1100
DATA1:
.DATA.L
10000
Page 46
DATA2:
ANSWER:
.DATA.W
.DATA.B
1000
10
In the above example, since the WORK section is located at the H'1100
address, DATA1, DATA2 and ANSWER represent the H'1100, H'1104 and
H'1106 addresses respectively.
If the ROM is also located at the H'1100 address, and if "10" and "100"
to be added should be provided separately, write as follows to prepare a
separate section for storing the addition results in:
.SECTION
ROM_DATA,DATA,LOCATE=H'1100
DATA1:
.DATA.B
10
DATA2:
.DATA.B
100
.SECTION
RAM_DATA,DATA,LOCATE=H'2000
ANSWER:
.RES.B
1
The above makes DATA1 represent the H'1100 address including "10"
("H'0A" in hexadecimal notation), DATA2 represent the H'1101 address
including "100" ("H'64" in hexadecimal notation) and ANSWER represent the
H'2000 address.
List 4.5 shows a program rewritten using .DATA control instructions.
You can see that the program starts from the H'1000 address, "10" and "100"
are stored in the H'1100 and H'1101 addresses respectively as data in the ROM,
and the H'2000 address is used as a work area in the RAM.
Page 47
List 4.5: Program Using .DATA
4.4 Collating Sequence Test Program
This section describes a program to test the collating sequence. Let's
assume that two 8-bit data blocks (both unsigned) are stored in the RAM and
you create a program to sort them in descending order. The program should
also store larger data in the DATA1 (H'2000) address and smaller in the
DATA2 (H'2001).
As instructions to test conditions, the BHI (Branch HIgh) and BGT
(Branch Greater Than) instructions have already been described. The BHI tests
the collating sequence assuming data to be unsigned and the BGT assuming
data to be signed. In combination with the CMP instruction, they are used as
follows:
; Compares the contents of the R1L and R0L
CMP.B
R1L,R0L
BHI ABC
; Branches to ABC if the contents of the R1L are greater
Instruction
; Otherwise, the next instruction is executed
Instruction
ABC: Instruction
The BHI is an instruction to perform branching if data is greater. "If
data is greater" means "if the data on the right is greater than that on the left
based on comparison" by the CMP instruction. Attach a symbol to the
instruction you want to branch. If the condition is satisfied, branching forward
or backward occurs, skipping some instructions. Otherwise, the next instruction
is executed.
The BHI instruction performs branching when both Z and C flags in the
CCR are 0. The CMP instruction subtracts the R1L from the R0L. If Z is 0 as a
Page 48