Page 127 - Understanding Automotive Electronics
P. 127
2735 | CH 4 Page 114 Tuesday, March 10, 1998 11:06 AM
4 MICROCOMPUTER INSTRUMENTATION AND CONTROL
Figure 4.9
Binary Computation
of Branch Address
FPO
Jump-to-Subroutine Instruction
Subroutines are short A subroutine is a short program that is used by the main program to
programs used to per- perform a specific function. It is located in sequential memory locations
form specific tasks, par- separated from the main program sequence. If the main program requires some
ticularly those tasks that function such as addition several times at widely separated places within the
must be performed sev- program, the programmer can write one subroutine to perform the addition,
eral times within the then have the main program jump to the memory locations containing the
same program. subroutine each time it is needed. This saves having to rewrite the addition
program over and over again. To perform the addition, the programmer simply
includes instructions in the main program that first load the numbers to be
added into the data memory locations used by the subroutine and then jump to
the subroutine.
The second and third Refer to Figure 4.10 to follow the sequence. It begins with the program
bytes of a jump-to-sub- counter pointing to address location 100, where it gets the jump-to-subroutine
routine instruction pro- instruction (step 1). Each jump-to-subroutine instruction (step 2) requires that
vide the address of the the next two bytes must also be read to obtain the jump address (step 2a).
subroutine to be jumped Therefore, the program counter is incremented once for each byte (steps 3 and
to. 4) and the jump address is loaded into the address register. The program
counter is then incremented once more so that it points to the op-code byte of
the next instruction (step 5).
Saving the Program Counter
The contents of the program counter are saved by storing them in a
special memory location before the jump address is loaded into the program
counter. This program counter address is saved so that it can be returned to in
the main program when the subroutine is finished. This is the record that was
mentioned before.
114 UNDERSTANDING AUTOMOTIVE ELECTRONICS