CMPUT 229 - Computer Organization and Architecture I

If/Elif/Else Conditional

229 RISC-V Examples - If/Elif/Else Conditional

Examples of Code Patterns and Structure in RISC-V - Main Function

The Main Function

This page introduces the main function/routine in RISC-V programs.

When writing a RISC-V program, and not just a function, the starting or entry point for executing the code is the main function.

The main label behaves similar to any other function in the .text section; there is just some behind the scenes work to tell the assembler to "Start here". An example of the main function in RISC-V:

# Needs to be in the text section as it is code.
.text

main:

    # Program code.

    # It is also a good idea to explicitly exit from the program,
    # so it doesn't just fall off and stop executing.
    li  a7, 10
    ecall

In the majority of programming assignments, the main appears in a common.s file and should not be included in the solution. The grading system uses a different common.s code that might call the function in the solution several times. Therefore, it is important that the function in the solution has a proper return statement -- it cannot simply terminate the program when it is done.

Example of a main function