M68K Subroutines

Mark Smotherman. Last updated September 2002.

(under construction)

Introduction

Dedicated registers

            a6 (fp) - frame pointer
            a7 (sp) - stack pointer

Subroutine instructions

            jsr
            bsr
            rts

Special instructions for stack frame support

            link
            unlk

Calling program structure


Subroutine structure


Example assembly code generated by C compiler

        _main:
                link  a6,#0            / update frame pointer
                addl  #-8,sp           / provide space for a,b
                movl  #0x5,a6@(-0x4)   / a = 5
                movl  #0x2c,a6@(-0x8)  / b = 44
                pea   a6@(-0x8)        / push &b onto stack
                pea   a6@(-0x4)        / push &a onto stack
                jbsr  _swap            / jump to subroutine swap
                addqw #0x8,sp          / remove parameters from the stack
                unlk  a6               / restore old frame pointer
                rts
        _swap:
                link  a6,#0            / update frame pointer
                addl  #-4,sp           / provide space for temp
                movl  a6@(0x8),a0      / move &a to register a0
                movl  a0@,a6@(-0x4)    / temp = *x => temp = *(&a)
                movl  a6@(0x8),a0      / move &a to register a0
                movl  a6@(0xc),a1      / move &b to register a1
                movl  a1@,a0@          / *x = *y   => *(&a) = *(&b)
                movl  a6@(0xc),a0      / move &b to register a0
                movl  a6@(-0x4),a0@    / *y = temp => *(&b) = temp
                unlk  a6               / restore old frame pointer
                rts

        note: moveml is used at the top and bottom of routines for saving
        and restoring registers.

stack contents as example code executes

                       A                                      +--------+
         low addresses |                                      | temp   |<-sp
                                                              +--------+
                                                              | old fp |<-fp
        high addresses |                    +--------+        +--------+
                       V                    | old pc |<-sp    | old pc |
                          +--------+        +--------+        +--------+
                          | &a     |<-sp    | &a     |        | &a     |
                          | &b     |        | &b     |        | &b     |
        +--------+        +--------+        +--------+        +--------+
        | b      |<-sp    | b = 44 |        | b = 44 |        | b = 44 |
        | a      |        | a = 5  |        | a = 5  |        | a = 5  |
        +--------+        +--------+        +--------+        +--------+
        | -      |<-fp    | -      |<-fp    | -      |<-fp    | -      |

        after 2nd         just before       just after        after 2nd
        inst. of main     call in main      call              inst. of swap


[History of subroutines page] [Mark's homepage] [CPSC homepage] [Clemson Univ. homepage]

mark@cs.clemson.edu