68K Subroutine by trigyy

VIEWS: 356 PAGES: 27

This article will explain the concepts of subroutine and its application. The term Nested Subroutine also will be explained in this article. Lastly, what is the delay in subroutine and how to program in 68000 microprosessor

More Info
Concepts and Applications

          Microprocessor    1
 To examine the concepts of subroutine and
 its application

                   Microprocessor            2
To be able to write a delay subroutine
To be able to compute the duration of a
To be able to tackle large programs

                   Microprocessor         3

Many programs require certain instructions to
performed several times.
The required instructions can be put into a
A subroutine is a sequence of instructions that
is written just once but executed when needed
by the main program.
  Example: Time delay routine.

                      Microprocessor              4
Main program


               Microprocessor                5
 In a 68000 system the subroutine is
 called/invoke by the BSR instruction.
 The RTS instruction is used to return to
 the main program.
   RTS is the last instruction in the subroutine.

      Why do we need to use subroutine?

                        Microprocessor              6
Reasons For Using Subroutines
 Programs are usually shorter
 Programs are more reliable
   Once a subroutine has been developed and
   tested it can be re-used without further testing
 Programs are more maintainable because
 they are shorter, simpler and more
 readable. Also it is easy to change the
 functionality of a subroutine, if required.

                       Microprocessor                 7
  The execution speed of a program is
  slower if it uses subroutines
  At machine code level, there are some
  complications introduced e.g. saving the
  environment of the calling program.
  Difficult program bugs can occur due to
  subroutine side effects e.g. stack,
  memory corruption's due to invalid
  parameter passing register saving

                      Microprocessor         8
Saving the CPU Environment
  In some cases, when a subroutine is
  called, it will need to use the CPU
  Before a subroutine is executed, the
  registers must be saved because their
  contents belong to the main program
  and will probably be needed when the
  subroutine returns.                              CPU registers –
    Main program saves any registers it needs      (Environment)
    onto stack and recovers them after the
    subroutine call.
    Subroutine responsible for saving registers.

                             Microprocessor                      9
Nested Subroutine
A nested subroutine is a subroutine that
calls another subroutine.

                    Microprocessor         10
Calculating Time Delays
 For a CPU running at a constant clock rate:
     Cycle Time = 1 / clock frequency
 Every machine or assembly instruction takes one or
 more clock cycles to complete.
 Consider the following time delay routine:

                  move.b              #value,d0
    loop          sub.b               #1,d0
                  bne                 loop
   The delay will depend upon the variable value

                         Microprocessor            11
Microprocessor   12
Microprocessor   13
From the Table D-5, “Immediate Instruction
Execution Times” - sub.b #1,d0 has an execution
time of 8 cycles
                                     No. of read cycles
   No. of clock                           No. of write cycles
From the Table D-9, “Conditional and Branch
Instruction Execution Times” - bne loop has an
execution time of 10 cycles (bne is one of the Bcc
Each pass through the loop will take 4 + 10 = 14

                         Microprocessor                         14
The Flite 68K system has a 10MHz clock, giving a
time for each cycle of 0.0000001s or 0.1µs Recall
that delay loop takes 14 cycles for each pass. This
represents a time delay of 14 x 0.1 µs = 1.4 µs.
Now, if the variable value is FFh (255 decimal).
The delay is:
 255 x 1.4 µS = 0.000357 S = 357 µS (0.357 ms)
Now, if the variable value is FFFFh (65535
decimal). The delay is:
   65535 x 1.4 µS = 0.091749 S = 91.749 ms

                        Microprocessor                15
Worked Example:
Write a program fragment which gives a delay of
115 mS.

    115 mS
           = 82142.8 = 82143
    1.4 µS

  So 82143 is the value to be loaded into the D0
    The source code is as follows;

                            Microprocessor         16
       move.b                #82142,d0
loop   sub.b                 #1,d0
       bne                   loop

            Microprocessor               17
Compute the total execution time for the
following 68000 assembler code fragment

             Move.b         #0A,D1
     Here    Add.w          (A1)+,D2
             Subq.b          #1,D1
             BNE             Here

                     Microprocessor        18
Microprocessor   19
           Instructions                    Clock cycles
                                        Overhead    Loop
        Move.b    #10,D1                    8
Here    Add.w     (A1)+,D2                         4
        Sub.q     #1,D1                            4
        BNE       Here                             10 (8)
                                       Loop taken   4+4+10 = 18
                                       Loop not taken 4+4+8 = 16

       4 + 9(18) + 16 = 226 clock cycles
                      Microprocessor                               20

                      Antonakos, pp.166 - 172
     Microprocessor                             21
Break the program down into smaller
modules or tasks.
 Each task can be thought as a subroutine.
 The subroutine can be called by the main
 program when needed.
 Each subroutine performs a specific task and
 hence each subroutine can be easily tested and

                    Microprocessor                22
Testing the Modules
 Testing of each subroutine module can be
 done separately, through a special program
 called a driver.
  The driver supplies the subroutine with sample
  input data and examines the sub-routine's output
  for correctness.
  It is up to the programmer to select the type and
  quantity of the sample data.

                     Microprocessor                   23
Test Data

            Test Driver



                 Microprocessor             24
Subroutine to be tested
                                         Antonakos, pp.171

The following subroutine was written to solve the
quadratic equation
             y = 5x2 - 2x + 6

Quad   move.w   D0,D1   ;Save copy of input value
       muls     D0,D1   ;Compute X^2
       muls     #5,D1   ;Compute 5X^2
       muls     #2,D0   ;Compute 2X
       sub.w    D0,D1   ;Compute 5X^2 - 2X
       add.w    #6,D1   ;Compute 5X^2 - 2X + 6

                        Microprocessor                       25
Determine what the correct results
should be. For example, consider the
following input and output pairs

      X – input                      y – output
           0                              6
           1                              9
          10                            486
         100                          49806

                      See pdf file for software solution

                    Microprocessor                    26
•The software driver will send each x-input value to the
procedure one at a time and check for a match with the
expected y-output value each time.

•If all four tests pass, the driver assumes the new
routine is acceptable. If anyone test fails, an error
message is output.

                         Microprocessor                27

To top