CMPUT 229 - Computer Organization and Architecture I

System Calls

229 RISC-V Examples - System Calls

Examples of Code Patterns and Structure in RISC-V - System Calls

System Calls

This page introduces system calls and goes over some commonly used ones. System calls are also known as environment calls in RISC-V.

What is a System Call?

A system call is a special instruction that notifies the operating system to complete a task for you. For a variety of reasons, (security, resource management) certain computer resources are not directly accessible to a user program. For example, most input/output devices like the keyboard and monitor are independently managed by the computer's operating system. A RISC-V assembly program must call on the OS to access them on the RISC-V program's behalf. This is what is known as a system call.

Restricting access to these resources is paramount to any computer meant for general use. Consider the implications of an OS giving free reign over the keyboard to any user program that asks for it. What if multiple programs need to access the monitor at the same time? The operation of the computer could go wrong very quickly from a security and resource-management perspective.

System Calls in RISC-V

Two registers and a single instruction are involved in a system call in RISC-V. The idea is to store important pieces of information into registers a0and a7, and then run the instruction ecall to perform the system call. The register a7 specifies the type of system call. Each type of system call has a number associated with it. To specify the type of system call the assembly program simply puts the corresponding number into a7. If the call needs a value, this value must be put into a0. For instance, to print an integer value, the value of the integer must be put into a0. For a system call that allocates memory, the number of bytes that will be allocated is put into a0.

The following code snippet illustrates the use of system calls with one that prints the character a to the screen. The system call number for printing a char is 11. In this example, the ASCII code for a is used to specify the character to be printed.

# Print 'a' to the screen
addi    a7, zero, 11    # a7 <- 11, specifies the PrintChar call
addi    a0, zero, 97    # a0 <- 97, ascii value of 'a'

# Make the system call.
ecall

Some system calls have a return value. For example, a system call that allocates space for a number of bytes in memory, must return the address of the first byte allocated. This address will be in a0 after the system call executes and can be used by the assembly program to store values in the allocated space.

While a lot of the system calls are relatively straight forward, there are some system calls that may use more of the argument registers (a1, a2, a3...). To find out the specifics of a given system call, RARS has a handy section going into detail under their help menu.

Common System Calls

Here is a list of some common system calls:

System call a0 a7
PrintInt Integer to print 1
PrintString Address of null-terminated string 4
ReadInt N/A, returns int read in a0 5
PrintChar Char to print, in the lowest 8 bits 11
Exit N/A, exits the program with code 0 10

A few last remarks; when debugging a common approach is to place print statements throughout the program to trace the code execution/values the variables take. RARS has some nifty features like listing the register values as you step through instruction by instruction, but another tool at your disposal is using the set of print system calls to help you debug. Additionally, below is a link to a code file containing examples of some of the above system calls.

Examples of system calls