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

Sample Programming in an Assembly Language

22 432 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 đề Sample programming in an assembly language
Thể loại Chapter
Định dạng
Số trang 22
Dung lượng 887,47 KB

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

Nội dung

Before executing the CMP instruction, store data in a general-purpose register... 6.2 Programs Containing a Loop Repetitive looping processing in assembly language is achieved using the

Trang 1

Chapter 6 Sample Programming in an Assembly Language

This chapter introduces some sample programs so that you can actually develop programs using various instructions

Programs can be developed in several ways and there is no single right answer During development, you will have many questions such as "Can the same be achieved by another method?" and "What will happen by doing this?" Rather than just worrying, go on and develop the program in mind and execute

it using the simulator If the results are the same as those obtained by the sample program, your program is also the right one Developing a different program by modifying a sample program is another effective way of learning This chapter will help you understand how various instructions and addressing modes described in Chapter 5 work for program development

6.1 Conditional Test Programs

This section introduces conditional test programs

To branch a process based on the collating sequence of values, the CMP (compare) instruction is used in combination with the conditional branch instruction You will use different branch instructions depending on whether the values to be compared are signed or not

The table below shows how to select branch instructions:

Table 6.1: Conditional Branch Instruction

Next, check the CMP instruction specifications in the instruction table

Trang 2

The CMP instruction can compare 8-, 16- and 32-bit data The comparison targets, however, must be general-purpose registers, or immediate and a general-purpose register Before executing the CMP instruction, store data in a general-purpose register

Trang 3

The conditional branch instruction can also be used independent of the CMP instruction To branch using the conditional branch instruction only, the status of each flag in the CCR changed by the previous instruction determines whether to branch or not

Trang 4

Table 6.2: Conditions for Conditional Branch Instruction

Trang 6

6.2 Programs Containing a Loop

Repetitive (looping) processing in assembly language is achieved using the conditional branch instruction This section introduces programs containing loops

After each repetitive processing, the value stored in a general-purpose register is incremented or decremented This value is generally called "loop counter" The value of the loop counter is judged by the conditional branch instruction at the end of each processing to determine whether to repeat it or not

Trang 7

It is usually recommended that the loop counter be decremented for repetitive processing This is because if the loop counter is incremented as shown in sample program 1, the CMP instruction is required to judge whether

to repeat the processing or not

On the other hand, if the counter is decremented, the CMP instruction is not required since the end of repetition can be determined based on whether the counter is zero or not This is because Z in the CCR becomes 1 for this type of instruction when the loop counter becomes 0 (otherwise, Z is 0)

This enables an instruction "to repeat processing if Z in the CCR is 0"

In this case, you can use the BNE instruction to branch if Z in the CCR is 0

Trang 8

You can also stop looping by judging that the address of the memory has reached a specific value

Trang 10

Moving execution to a separated function is referred to as "calling a subroutine (subroutine call)" and subroutine call instructions (BSR and JSR instructions) are used for this purpose They are written as follows:

BSR Subroutine name (Called with program counter relative addressing) JSR @Subroutine name (Called with absolute address addressing)

* Subroutine name: Symbol prefixed to a subroutine

In the called subroutine, instructions are executed in ordinary order and control is returned to the source after execution is completed

The RTS instruction is used to return execution to the source This means that all subroutines end with the RTS instruction This instruction is written as follows:

RTS (The RTS instruction has no operand) After the RTS instruction is executed, processing resumes from the instruction next to the one which has called the subroutine

Trang 11

Let's consider how the RTS instruction returns execution to the source

If one subroutine is called from different places, execution is returned to the respective places by the RTS instruction

Before introducing the principle of subroutine operation, introduction of the stack is indispensable

What is the stack?

1 It refers to RAM space which is extended from larger to smaller addresses as necessary to input information while the direction is reversed from smaller to larger for outputting stored information

2 In a RAM prepared as the stack, up to which addresses have been used and which will be used next are automatically managed by the stack pointer (SP) Once the stack area address has been set in the SP, programmers need not be concerned about where the current SP is

All programmers need to do before using the stack for calling a subroutine or other purpose is

to set a value for "the last address of the area to be used as the stack + 1 (even number)" in the

SP (the ER7 in case of the H8/300H series)

In the stack, you can store even-number-sized information only Although any RAM is available for the stack area, the internal RAM space is generally used

3 When using the stack area in a program, programmers must take the stack capacity (how many bytes are required in all) into account Calculate the capacity used in the developed program and be sure to secure sufficient free RAM space

Subroutines are called using the above stack function

Subroutine call instructions (JSR and BSR instructions)

The CPU always maintains the address of the next instruction in the program counter

The subroutine call instruction first stores the address of the instruction written next to itself in the stack (this becomes the address returned from the subroutine) After this operation,

Trang 12

Instruction to return from subroutine (RTS instruction)

The RTS instruction at the end of a subroutine writes the address stored in the stack by the subroutine call instruction to the PC This enables the instruction next to the subroutine call instruction to be executed next to the RTS instruction In other words, processing is returned to the source of the subroutine

Trang 13

1 You want to branch if R0 is less than R1 after the following instruction Which branch instruction do you use? It is assumed, however, that unsigned data are stored in R0 and R1

Answer: BHI

The destination operand plays the main role in comparison using the CMP instruction

In this question, R1 is used as the destination operand

Assuming that R1 is greater than R0, you should use BHI

2 After the following instruction is executed, is Z in the CCR "0" or "1"?

Trang 14

Answer: 1

Since 0 is stored in the ER0 register as a result of subtraction, Z in the CCR is

1

3 The following program is designed to obtain the minimum value from

10 blocks of byte data at the ROM_DT address using a subroutine and store it in the RESULT address Enter appropriate information in parentheses It is assumed, however, that signed data are stored in the ROM_DT address

Answers are written in red below

BNE LOOP

In the R1L register, "9" is entered as the counter to repeat the loop nine times The counter is decremented by 1 per processing, which is repeated until the counter becomes zero The BNE (branch if not zero) instruction is used to loop unless zero

MOV.B R0L,@RESULT Since the minimum data is stored in the R0L register after being compared 9 times, it is written to the RESULT address

MIN: CMP.B R0H,R0L

In the first instruction of a subroutine, specify its name

MIN is the name of the subroutine and:

Trang 15

JSR @MIN

is used to jump to it

BLE RETURN This subroutine compares the contents of the R0H and R0L registers assuming them to be signed and puts the smaller in the R0L register

If the contents of R0L are equal to or smaller than as a result of comparison, processing is returned from the subroutine without any operation As a conditional test instruction, use the one assuming data to be signed

RTS Instruction to return from a subroutine

DATA.B 99,0,-5,39,-2,68,-16,5,20 Defines 8-bit data DATA.B is mainly used to represent data in the ROM

6.4 Register Indirect with Displacement

Register indirect with displacement addressing (described in Chapter 5)

is used to retrieve specific data from consecutive data in the memory such as arrays

Trang 16

6.5 Post-increment Register Indirect/Predecrement Register Indirect

This section introduces sample programs using post-increment register indirect and predecrement register indirect (described in Chapter 5)

Post-increment register indirect

Post-increment register indirect is used to refer to consecutive values in the memory

To be more specific, it stores the address of the memory to be referred

to in a general-purpose register, refers to the contents and increments the value (address) stored in the register by the byte count of the handled data

Trang 17

Predecrement register indirect

Predecrement register indirect is used to store values sequentially in consecutive areas in the memory

To be more specific, it stores "the last address of the target memory + 1" in a general-purpose register, decrements the value in the register by the byte count

of the data to be handled and writes information to the address

Trang 18

6.6 Multiplication and Division Programs

The H8/300H is provided with multiplication and division instructions This section introduces programs using these instructions

Multiplication instructions

There are two types of multiplication instructions: one for unsigned values (MULXU instruction) and the other for signed values (MULXS instruction)

Trang 19

Division instructions

There are also two types of division instructions: one for unsigned values (DIVXU instruction) and the other for signed values (DIVXS instruction)

For the division instructions, the results are not guaranteed in the following cases:

When the quotient exceeds 1 byte for byte-size division (overflow)

Trang 20

The table below shows the logical operation results of 2 data blocks:

Table 6.3: Logical Operation Table

Based on these results, logical operation is used in the following cases:

6.8 Bit Handling

The H8/300H is provided with a bit handling instruction for handling a specific 1 bit only This section introduces a sample program using this instruction

Although a specific 1 bit among data can also be handled using the logical instruction described earlier, its target is general-purpose registers only

On the contrary, the bit handling instruction can handle not only data stored in

a general-purpose register but also directly handle the contents of the memory between H'FFFF00 and H'FFFFFF addresses using absolute address

addressing

Bits in 1-byte data are numbered as follows:

Trang 21

1 Point out the errors in the following instructions (Some instructions, however, may not include errors.)

Instruction (1) DIVXU.B #H'15,R0 (2) MOV.B @(#H'10,R0L),R0H (3) BSET #1,@H'FFFFF0

(4) BSET #1,@H'200000 (5) BTST #7,R0L

(6) BTST #8,R0L (7) BTST.W #3,R0

Answers (1) Only a general-purpose register can be specified for the DIVXU instruction operand

(2) Register indirect with displacement can specify 32-bit purpose registers only

general-(3) Correct

(4) Although the bit handling instruction can specify H'FFFF00 to H'FFFFFF using an absolute address, other addresses must be specified by register indirect

(5) Correct

(6) Available bit numbers are between 0 and 7

(7) The bit handling instruction can handle byte data only

2 Answer the following questions

(1) How can you rewrite only the least significant 2 bits in 1-byte data to 0?

Answer:

Execute AND with #B'11111100

AND operation is used to set specific bits to 0 since it results in 0 if either of the two is 0

Use the BCLR instruction to set only one bit to 0

(2) How can you rewrite only the least significant 2 bits in 1-byte data to 1?

Answer:

Execute OR with #B'00000011

OR operation is used to set specific bits to 1 since it results in 1 if either of the two is 1

Use the BSET instruction to set only one bit to 1

(3) How can you invert only the least significant 2 bits in 1-byte data?

Answer:

Trang 22

EOR with 0 results in no change but with 1 in inversion Utilizing this function, you can use EOR operation with 1 to invert specific bits Use the BNOT

instruction to invert one bit only

(4) How can you check the statuses of the least significant 2 bits in 1-byte data and ignore the rest?

Answer:

Execute AND with #B'00000011

After setting untargeted bits to 0 using AND operation, you can judge whether the target bits are zero or not, or compare them with specific bit patterns using the CMP instruction

(5) How can you check the status of only the least significant bit in R0L and ignore the rest?

BNE LOOP

Answer The count of bits set to 1 in 1-byte data at the T_DATA address

The key instruction is:

BTST R0H,R4L

on the fourth line It tests 1 bit in R4L using R0H as the bit number It changes R0H from 7 to 0 and checks whether 8 bits in R4L are 1 or 0 sequentially from the most to least significant bits Since R0L is incremented by 1 per bit having

a value of "1", the count of 1s in the T_DATA address is stored in R0L.

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

TỪ KHÓA LIÊN QUAN