Syscalls
In RISC-V, syscalls allow a program to request services from the execution environment.
This is done using the ecall instruction.
In the ÆRIS simulator, syscalls are mainly used for console input and output.
Unlike a real operating system, ÆRIS does not have a kernel mode, so ecall is handled directly by the simulator.
How ecall Works
Section titled “How ecall Works”A syscall follows three steps:
- Set the syscall code in register
a7 - Pass the arguments in registers (
a0,a1, …) - Execute the
ecallinstruction
Example:
li a7, 1li a0, 42ecallThis prints the number 42.
Available Syscalls
Section titled “Available Syscalls”| Code | Name | Description |
|---|---|---|
| 1 | PrintInt | Prints the integer in a0 |
| 2 | PrintString | Prints a string from the address in a0 |
| 3 | ReadInt | Reads an integer from the console |
| 4 | ReadString | Reads a string from the console |
PrintInt (1)
Section titled “PrintInt (1)”Prints the integer value stored in a0.
Example:
li a7, 1li a0, 123ecallOutput:
123PrintString (2)
Section titled “PrintString (2)”Prints a sequence of characters from the address in a0.
In ÆRIS, strings do not have an automatic null terminator.
Example:
.datamsg: .ascii "Hello"
.textmain: li a7, 2 la a0, msg ecallReadInt (3)
Section titled “ReadInt (3)”Reads an integer from the user.
Example:
li a7, 3ecallBehavior:
- The simulator pauses execution
- The user enters a number
- The value is stored in
a0
ReadString (4)
Section titled “ReadString (4)”Reads a sequence of characters from the user and stores it in memory.
Arguments:
| Register | Description |
|---|---|
a0 | Buffer address |
a1 | Maximum size |
Example:
.databuffer: .word 0,0,0,0,0,0,0,0
.textmain: li a7, 4 la a0, buffer li a1, 32 ecallBehavior:
- The user types a string
- The characters are written in memory starting at
a0 - At most
a1bytes are stored