Download Print this page

RCA COSMAC User Manual page 54

Hide thumbs Also See for COSMAC:

Advertisement

COSMAC Microprocessor
53
Branching Between Pages
The branch instructions (1=3) are limited to branches within the currently addressed 256-byte memory
page. In larger programs, it is often necessary to be able to branch to any location in memory. The sequence
of instructions shown in Fig. 58 illustrates one method of performing such a
long branch.
ADDRESS
BYTE
OPERATION
COMMENTS
0025
FB
MIR(P))+D
0026
05
0027
B4
D+R(4).1
0573+R(4)
002B
FB
MIRIP))+D
0029
73
002A
A4
D+R(4).0
002B
D4
4+P
CONTROL TO R(4)
002C
--
R(3) LEFT POINTING HERE
Fig.
58 -
Long branch code.
Initially, R(3) is the program counter (P=3). The sequence of instructions shown puts the 2-byte
dest,ination address (0573) into R(4). Setting P=4 then causes a branch to the instruction sequence
beginning at M(0573) with R(4) as the program counter. Note that if the sequence using R(4) as program
counter ends by setting P=3, execution resumes at 002C, with R (3) as program counter.
Subroutine Techniques
In large programs, a given short sequence of instructions might be used many times. For example, one
short sequence might generate random numbers. The required instructions could be rewritten each place in
the program that the function is needed. However, this duplication of instructions can consume much
memory storage space, especially if the sequence is long. An alternate method is to write the sequence only
once as a subroutine. Each time that the main program needs a random number it would branch to this
subroutine by means of a
subroutine
call, Completion of the subroutine would cause a return to the main
program at the instruction following the branch to the subroutine. The use of subroutines reduces the
amount of memory required for programs since the subroutine instruction sequence occurs only once
instead of each time it is used in a program.
As an example, suppose the designer often wants to execute a long branch. To reduce the code needed
for each long branch, one register such as R(4) could be dedicated as the permanent program counter for a
long branch subroutine. Its entry address, say 1234, would be loaded once at the beginning of the main
program. If R(3) is the main program counter, then a long branch to location 075A would appear as
the following subroutine call:
D4
07
5A
The subroutine itself would be as shown in Fig. 59.
Address to be branched to
will be picked up by subroutine,
This subroutine uses three useful devices: (1) The old program counter R (3) is used to pick up arguments
for the subroutine - - in this case the new address. (2) A temporary location M(R(2)) was needed since
R(3) could not be changed while its old value was still needed to fetch the 5A. (3) By branching to the top
before returning to R(3). the subroutine leaves the program counter R(4) ready for another call by the
main program, or by other subroutines.

Advertisement

loading