View Index by Level
RANDOM PAGE

SITE SEARCH

LOG
IN

SIGN UP

HELP

Subroutines


This is the AQA version closing after June 2019. Visit the the version for Eduqas instead.

To gain access to revision questions, please sign up and log in.

A2

Subroutines Defined

Subroutines are small programs inside the main program performing a well-defined self-contained task.

Examples

A Time Delay

Method One - Simple but not Recommended

Use several NOP commands to create the delay.

CTRL+Click here to run the simulator.

; ===== MAIN PROGRAM =====
START:
        NOP
        NOP
        NOP
        CALL    DELAY
        NOP
        NOP
        JMP     START

; ===== TIME DELAY =======
DELAY:
        NOP
        NOP
        NOP
        NOP
        RET
; ========================
START:         This labels address zero.
NOP         This does nothing for one clock cycle.
CALL DELAY         This copies 7 (the address labelled by DELAY) into the program counter.

        This also copies 4 (the return address) onto the stack.

DELAY:         This labels address seven.
RET         This copies 4 (the address saved on the stack) into the program counter.
JMP START         This copies 0 (the address labelled by START) into the program counter.

Method Two - A Fixed Time Delay

CTRL+Click here to run the simulator.

; ===== MAIN PROGRAM =====
START:
        NOP
        NOP
        NOP
        CALL    DELAY
        NOP
        NOP
        JMP     START

; ===== TIME DELAY =======
DELAY:
        MOVW    0x10
REP:
        SUBW    0x1
        JPZ     DONE
        JMP     REP
DONE:
        RET
; ========================
START:         This labels address zero.
NOP         This does nothing for one clock cycle.
CALL DELAY         This copies 7 (the address labelled by DELAY) into the program counter.

        This also copies 4 (the return address) onto the stack.

DELAY:         This labels address seven.
MOVW 0x10         This copies hexadecimal 10 into W, the working register. This sets the delay.
REP:         This labels address eight.
SUBW 0x1         This subtracts one from W and stores the answer back into W.
JPZ DONE         If the status register (SR) zero flag is set, this will jump to address 0xB.

        The zero flag will be set when W has finally counted down to zero.

JMP REP         This will set the program counter to the address labelled by REP (address eight).
DONE:         This labels address 0xB.
RET         This copies 4 (the address saved on the stack) into the program counter.
JMP START         This copies 0 (the address labelled by START) into the program counter.

Method Three - A Variable Time Delay

CTRL+Click here to run the simulator.

; ===== MAIN PROGRAM =====
START:
        NOP
        NOP
        MOVW    0x15    ; Set the delay
        CALL    DELAY
        NOP
        NOP
        JMP     START

; ===== TIME DELAY =======
DELAY:
        SUBW    0x1
        JPZ     DONE
        JMP     DELAY
DONE:
        RET
; ========================
START:         This labels address zero.
NOP         This does nothing for one clock cycle.
MOVW 0x15         This copies hexadecimal 15 into W, the working register. This sets the delay.
CALL DELAY         This copies 7 (the address labelled by DELAY) into the program counter.

        This also copies 4 (the return address) onto the stack.

DELAY:         This labels address seven.
SUBW 0x1         This subtracts one from W and stores the answer back into W.
JPZ DONE         If the status register (SR) zero flag is set, this will jump to address 0xA.

        The zero flag will be set when W has finally counted down to zero.

JMP DELAY         This will set the program counter to the address labelled by REP (address eight).
DONE:         This labels address 0xA.
RET         This copies 4 (the address saved on the stack) into the program counter.
JMP START         This copies 0 (the address labelled by START) into the program counter.

Method Four - Use TMR and PRE

Full details are on the Timers and Interrupts page.

 

 

 

reviseOmatic V3     Contacts, ©, Cookies, Data Protection and Disclaimers Hosted at linode.com, London