Writing a Simple Program in an Assembly Language - Pdf 62


h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 29
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

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 h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 31
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.

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 even-
numbered 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 8-
bit 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. h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 33
Conditional test in a program is performed by these four flags. Any
condition can be tested using them.

Conditional test using the CCR
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 34
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 (16-
bit 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 even-
numbered 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]
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 35


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.

h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 36
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.

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.
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 37
Samples
CMP.B R0L,R1L
Subtracts the R0L from the R1L, changing the CCR.
Conditional branch instruction
CMP.B #H'12,R0L
Subtracts H'12 (18 in decimal notation) from the 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

CMP.B R0L,R1L
Compares the R1L with the R0L.

BHI ABC
If the R1L is greater, branches to the symbol ABC.
Instruction
Otherwise, the next instruction is executed.
Instruction
ABC: Instruction


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status