1. Trang chủ
  2. » Công Nghệ Thông Tin

Writing a Simple Program in an Assembly Language

24 536 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Writing a Simple Program in an Assembly Language
Trường học Renesas
Chuyên ngành Assembly Language
Thể loại Essay
Định dạng
Số trang 24
Dung lượng 392,72 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

For example, a machine instruction to add the R0L and R1L as an arithmetic register is expressed as follows in 16 bits: 4.1 CPU Internal Registers Before developing a program with asse

Trang 1

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:

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

Trang 2

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 digit binary numbers.In addition to 32-bit data, they can also store 16- or 8-bit data

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

Trang 3

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

Trang 4

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 numbered address first This means that an even-numbered address is always stored in the PC (see "Data in the memory")

even-CCR (condition code register)

This is used to control interrupts or test conditions Although it is an 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

Trang 5

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

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

Trang 6

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]

Trang 7

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

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

Trang 8

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 purpose register, from a purpose 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

general-Samples MOV.B R0L,R1L Transfers 8-bit data from the R0L to the 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 ADD.B R0L,R1L Adds the R1L and R0L and stores the results in the 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

Trang 9

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,

The destination address is specified by giving it a name ("symbol") Sample

BRA ABC Unconditionally branches to the symbol ABC.

Instruction ABC: Instruction

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

Trang 10

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:

Trang 11

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:

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

Trang 12

This source program is converted into machine instructions by the assembler as follows:

Address Machine instruction Instruction

.END

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]

Ngày đăng: 29/09/2013, 11:20

TỪ KHÓA LIÊN QUAN