Subroutines

Subroutines in ASM2 are similar to functions but are typically used for smaller, reusable tasks within a larger program. They might not return a value themselves but can modify existing variables or registers.

Naming and Usage Convention:

  • CamelCase with Dot Prefix: Subroutine names must use camelCase and start with a ..

  • No Initial Numbers: Cannot start with a number.

  • Not Solely Numeric: Cannot be just a number.

  • No Special Characters: Cannot contain special characters.

Usage: CALL .subroutineName

General Structure:

assemblyCopy code.functionName
    ; Context and description
    ; OPERANDS Setup: DESTINATION, SOURCE

    CALL .subroutineName

    ; More function code

END

.subroutineName
    ; Context and description
    ; OPERANDS Setup: DESTINATION, SOURCE (if needed)

    ; Subroutine body

    RET

Example:

assemblyCopy code.calculateSquare
    ; Calculates square of a number
    ; Input: OPERANDS Setup: DESTINATION, SOURCE

    CALL .multiplyNumbers
    MOV DESTINATION, ReturnValue
    
    RET

.multiplyNumbers
    ; Multiplies two numbers
    ; Input: OPERANDS Setup: DESTINATION, SOURCE

    MUL DESTINATION, SOURCE
    
    RET

.main
    ; Main function code

    CALL .calculateSquare
    
    ; Continue with main code

END

Key Points:

  • Specialized Use: For specific, often-repeated tasks within larger functions.

  • No Direct Return Value: Typically modifies existing variables or registers instead of returning values.

  • Enhanced Program Structure: Contributes to a clearer, more organized program structure.

  • CALL Instruction: Used to invoke the subroutine from a function or main program.

Last updated