h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 62
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.
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 63
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.
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 64
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:
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 65
Table 6.2: Conditions for Conditional Branch Instruction
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 66
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 67
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.
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 68
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.
h t t p : / / r e s o u r c e . r e n e s a s . c o m Page 69
You can also stop looping by judging that the address of the memory
has reached a specific value.