CMPUT 229 - Computer Organization and Architecture I

Labels and Sections

229 RISC-V Examples - Labels and Sections

Examples of Code Patterns and Structure in RISC-V - Labels and Sections

Labels and Sections

This page introduces labels and two types of sections that are common in a RISC-V assembly program.

Sections

The two types of sections are the data section and the text section. The data section contains static data allocations while the text section contains assembly instructions. An assembler is a program that converts the text of an assembly program into a binary representation of the program. A directive is used to tell the assembler where each section starts. They direct the assembler to do a specific task or behave in a certain way. A directive always starts with a period.

The directive to start the data section is as follows:

.data

Typically, a section directive appear unindented and alone on the line so that they are easy to visualize. The directive to start the text section is:

.text

There is no need to mark where a section ends. It ends implicitly at the start of another section or at the end of the file. Typically the data section appeard before the text section in an assembly program. However, this order is not a requirement. The overall file structured is as follows:

.data

# Allocate static data

.text

# Assembly code

The # character begins a comment. For more directives see the Data Section Directives.

Labels

Labels are names used to identify sections of memory. These names can be used by the programmer to refer to memory locations abstractly. The assembler will translate them into appropriate addresses in the binary representation of the program. A label that appears in the data section, can be thought of as a named variable that you can read from and write to. When used for code, a label can be a procedure name or the target of a jump or a branch instruction. Consider the code snippet below:

.data

exampleLabel1:
    .space 1

The label exampleLable1 appears in the data section of the program. A labels is a string composed of letters, numbers, and underscores. The string is followed by a colon. A label must start with a letter or an underscore. It cannot start with a number. A label can be indented but in most styles for assembly programming, labels are not indented. The label exampleLable1 above will be used in the program to access the single byte that is allocated by the directive .space 1. This directive specifies that the assembler must reserve space for one byte at that location in memory. Whenever the assembler encounters the label exampleLable1 it will replace it with the address of the byte allocated (see Data Section Directives) .

Labels do not need to be alone on a line. The following line of code is also acceptable:

exampleLabel2: .space 1

When a label appear in the text section, a label can be the destination of a jump or branch instruction, or it can be the name of a procedure --- the words routine, subroutine, function, and procedure mean the same thing in assembly programming. The solution of a programming assignment often consists on writing the assembly code for a function that executes a specified task. In most programming assignments a main function is provided and that main function will call the function specified in the assignment. For example, if an assignment specifies that a function foo that returns the value zero should be written, the text section of the solution to this assignment will start with the label foo:

foo:
    add   a0, zero, zero    # t0 <- 0
    jalr  zero, ra, 0       # Return

Link to a file containing all of the above code snippets