COMP-1200 System Calls and Functions

Learning Objectives

  • Compare and contrast system calls and function calls.
  • Illustrate the behavior of a program using functions.
  • Use a system call or another function.

System Calls

  • Definition: System calls provide functions as part of the operating system (OS). They are included in the OS Kernel segment.
  • Purpose: To safely and conveniently perform low-level, privileged operations.
  • When a system call is made, control is passed to the operating system, and when it returns, the program continues execution from where it left off.

Understanding the Von Neumann Model

  • Components:
    • CPU (Central Processing Unit)
    • Control Unit
    • Memory
    • I/O Devices (Keyboard, Mouse, Monitor, etc.)

Interacting with External Devices

  • Certain operations, especially I/O operations, require specialized knowledge about I/O device registers:
    • Knowledge about what to do with registers.
    • Understanding the sequence of operations needed to use them.

Importance of the Operating System

  • I/O resources are shared among multiple users/programs. A mistake in one program could adversely affect others, for example:
    • Printer accidentally printing 1,000 copies
    • Soundcard becoming unavailable due to errors.

System Calls in RISC-V

  • The RISC-V architecture uses the ecall instruction to implement system calls.
  • Use of Registers:
    • a7 Register: Chooses which system call to use.
    • a0, a1, … registers: Used to pass arguments to system calls.
    • Return Values: Returned via the a0 register.

Table of System Call Numbers in RARS

NameSyscall NumberArgumentsReturn Values
printInt1a0: integerN/A
readInt5N/Aa0: integer
exit10N/AN/A
printChar11a0: ASCII byteN/A
readChar12N/Aa0: ASCII byte

Example: Printing Integers

  • Code Example:
# printInt(11)
li a0, 11  # a0 = 11
li a7, 1   # a7 = 1
ecall       # syscall(1)

# exit()
li a7, 10  # a7 = 10
ecall       # syscall(10)
  • Equivalent Code:
printInt(11);  
exit();

Reading Integers Example

  • Code Example:
# a0 = readInt()
li a7, 5   # a7 = 5
ecall       # syscall(5)
sub t0, zero, a0  # t0 = -a0
add t0, t0, t0     # t0 = t0 + t0
  • Purpose: To read an integer input from the keyboard.

System Calls in Other Operating Systems

  • System calls may differ across operating systems:
    • Example: In Linux, exit is syscall #94, not 10.
    • There is no readInt in Linux; instead, use read (syscall #63).

Functions Overview

  • A function is a reusable block of code that performs a specific task, allowing code to be written once and reused multiple times.

Function Mechanics

  • Functions can use both local and global variables.
  • Arguments are passed using a-registers (a0, a1, …)
  • Return values are typically stored in a0 or a1.
  • Temporary registers (t0, t1): Assumed to be unavailable after a function call, used for brief calculations.
  • Saved registers (s0, s1, …): Used for longer-term value storage during function calls.

Declaring Functions

  • Functions start with a label (e.g., main, add) and conclude with a ret instruction to return to the original function.

Control Flow Instructions

  • JAL (Jump And Link): Used to call a function by jumping to its label and saving the return address in ra.
  • RET Instruction: Returns to the address saved in ra.

Example Function: add1

  • Purpose: Add 1 to the integer provided as input.
  • Code Example:
# int add1(int x)
add1:   addi a0, a0, 1
ret
  • Equivalent in Java:
int add1(int x) {
  return x + 1;
}

Tracing add1

  • Execution flow through registers:
    • On entry: Set a0 to 11, call add1, return to main.

Example Function: mult3

  • Purpose: Multiply the integer by 3.
  • Code Example:
# int mult3(int x)
mult3:   add t0, a0, a0
            add a0, a0, t0
            ret
  • Equivalent in Java:
int mult3(int x) {
  return x + x + x;
}

What's Next?

  • Topics to be covered:
    • Control Flow Instructions
    • Function Stack Management