Sample Programming in an Assembly Language

22 431 0
Sample Programming in an Assembly Language

Đ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

Chapter 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 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 http://resource.renesas.com Page 62 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 http://resource.renesas.com Page 63 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 The table below shows the conditions for the conditional branch instruction: http://resource.renesas.com Page 64 Table 6.2: Conditions for Conditional Branch Instruction http://resource.renesas.com Page 65 http://resource.renesas.com Page 66 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 http://resource.renesas.com Page 67 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 for this type of instruction when the loop counter becomes (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 http://resource.renesas.com Page 68 You can also stop looping by judging that the address of the memory has reached a specific value http://resource.renesas.com Page 69 6.3 Subroutines If the same collection (function) of instructions is executed several times in a program, writing the function every time makes the program difficult to understand In addition, it makes the program larger since it also increases the instruction count To prevent this, programs should be developed by separating repeated functions from the main flow These separated functions are called "subroutines" (By contrast, the main program flow is called "main routine".) http://resource.renesas.com Page 70 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 JSR Subroutine name (Called with program counter relative addressing) @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 http://resource.renesas.com Page 71 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? 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 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 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 + (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 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, execution moves to the subroutine http://resource.renesas.com Page 72 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 http://resource.renesas.com Page 73 You want to branch if R0 is less than R1 after the following instruction Which branch instruction you use? It is assumed, however, that unsigned data are stored in R0 and R1 CMP.W R0,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 After the following instruction is executed, is Z in the CCR "0" or "1"? CMP.W R0,R1 http://resource.renesas.com Page 74 Answer: Since is stored in the ER0 register as a result of subtraction, Z in the CCR is 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 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 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: http://resource.renesas.com Page 75 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 http://resource.renesas.com Page 76 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 http://resource.renesas.com Page 77 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 http://resource.renesas.com Page 78 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) http://resource.renesas.com Page 79 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 byte for byte-size division (overflow) When the quotient exceeds bytes for word-size division (overflow) When divided by zero http://resource.renesas.com Page 80 6.7 Logical Operation The H8/300H is provided with an instruction for logical operation per data bit This section introduces programs using logical operation Logical operation is used to retrieve the statuses of multiple bits in data, set multiple bits to or 1, or invert the current statuses The table below shows the logical operation results of 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 bit only This section introduces a sample program using this instruction Although a specific 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: http://resource.renesas.com Page 81 ... in the following cases: 6.8 Bit Handling The H8/300H is provided with a bit handling instruction for handling a specific bit only This section introduces a sample program using this instruction... 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... Branch Instruction http://resource.renesas.com Page 65 http://resource.renesas.com Page 66 6.2 Programs Containing a Loop Repetitive (looping) processing in assembly language is achieved using

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

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan