RISC-V Syscalls

Making a syscall

RISC-V syscalls are used to make requests to the operating system. They are not instructions that are executed by the CPU, but rather instructions that are used by the simulator to make requests to the operating system.

Each syscall has a unique code that is used to identify it. You must put the syscall code inside the a7 register before calling the ecall instruction. As an example:

# Load the integer 42 into register a0, which is
# the register that will be printed by the syscall
li a0, 42
# Set the syscall code for printing an integer
li a7, 1
ecall

Print integer

a7 = 1
a0
Integer to print

Print float

a7 = 2
f12
Float to print

Print double

a7 = 3
f12
Double to print

Print string

a7 = 4
a0
Address of null-terminated string to print

Read integer

a7 = 5
v0 after
Contains integer read

Read float

a7 = 6
f0 after
Contains float read

Read double

a7 = 7
f0 after
Contains double read

Read string

a7 = 8
a0
Address of input buffer
a1
Maximum number of characters to read
Note
Service 8 - Follows semantics of UNIX 'fgets'. For specified length n, string can be no longer than n-1. If less than that, adds newline to end. In either case, then pads with null byte If n = 1, input is ignored and null byte placed at buffer address. If n < 1, input is ignored and nothing is written to the buffer.

Sbrk (allocate heap memory)

a7 = 9
a0
Number of bytes to allocate
v0 after
Contains address of allocated memory

Exit (terminate execution)

a7 = 10

Print character

a7 = 11
a0
Character to print
Note
Service 11 - Prints ASCII character corresponding to contents of low-order byte.

Read character

a7 = 12
v0 after
Contains character read

Get cwd

a7 = 17

Time (program time)

a7 = 30
a0 after
Low order 32 bits of the program time
a1 after
High order 32 bits of the program time
Note
Service 30 - Milliseconds since the run started, rather than since 1 January 1970 as in RARS: it is the time the program can observe passing, and in a testcase it comes from a virtual clock that starts at zero and only advances through the waits of service 32.

Sleep

a7 = 32
a0
The length of time to sleep in milliseconds
Note
Service 32 - Lets that much program time pass before the next instruction. The editor stays responsive while it waits and the wait costs no instructions, so a program idling on the keyboard never reaches the execution limit; in a testcase it completes at once and advances the virtual clock instead.

Print integer in hexadecimal

a7 = 34
a0
Integer to print
Note
Displayed value is 8 hexadecimal digits, left-padding with zeroes if necessary.

Print integer in binary

a7 = 35
a0
Integer to print
Note
Displayed value is 32 bits, left-padding with zeroes if necessary.

Print integer as unsigned

a7 = 36
a0
Integer to print
Note
Displayed as unsigned decimal value.

Random int

a7 = 41
a0
I.d. of pseudorandom number generator (any int)
a0 after
Contains the next pseudorandom, uniformly distributed int value from this random number generator's sequence
Note
Each stream (identified by a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.

Random int range

a7 = 42
a0
I.d. of pseudorandom number generator (any int)
a1
Upper bound of range of returned values
a0 after
Contains pseudorandom, uniformly distributed int value in the range 0 <= [int] < [upper bound], drawn from this random number generator's sequence
Note
Each stream (identified by a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.

Random float

a7 = 43
a0
I.d. of pseudorandom number generator (any int)
f0 after
Contains the next pseudorandom, uniformly distributed float value in the range 0.0 <= f < 1.0 from this random number generator's sequence
Note
Each stream (identified by a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.

Random double

a7 = 44
a0
I.d. of pseudorandom number generator (any int)
f0 after
Contains the next pseudorandom, uniformly distributed double value in the range 0.0 <= f < 1.0 from this random number generator's sequence
Note
Each stream (identified by a0 contents) is modeled by a different Random object. There are no default seed values, so use the Set Seed service (40) if replicated random sequences are desired.

ConfirmDialog

a7 = 50
a0
Address of null-terminated string that is the message to user
a0 after
Contains value of user-chosen option 0: Yes 1: No 2: Cancel

InputDialogInt

a7 = 51
a0
Address of null-terminated string that is the message to user
a0 after
Contains int read
a1 after
Contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field

InputDialogFloat

a7 = 52
a0
Address of null-terminated string that is the message to user
f0 after
Contains float read
a1 after
Contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field

InputDialogDouble

a7 = 53
a0
Address of null-terminated string that is the message to user
f0 after
Contains double read
a1 after
Contains status value 0: OK status -1: input data cannot be correctly parsed -2: Cancel was chosen -3: OK was chosen but no data had been input into field

InputDialogString

a7 = 54
a0
Address of null-terminated string that is the message to user
a1
Address of input buffer
a2
Maximum number of characters to read
a1 after
Contains status value 0: OK status. Buffer contains the input string. -2: Cancel was chosen. No change to buffer. -3: OK was chosen but no data had been input into field. No change to buffer. -4: length of the input string exceeded the specified maximum. Buffer contains the maximum allowable input string plus a terminating null.
Note
See Service 8 note below table

MessageDialog

a7 = 55
a0
Address of null-terminated string that is the message to user
a1
The type of message to be displayed: 0: error message, indicated by Error icon 1: information message, indicated by Information icon 2: warning message, indicated by Warning icon 3: question message, indicated by Question icon other: plain message (no icon displayed)

MessageDialogInt

a7 = 56
a0
Address of null-terminated string that is an information-type message to user
a1
Int value to display in string form after the first string

Close file

a7 = 57
a0
File descriptor

MessageDialogDouble

a7 = 58
a0
Address of null-terminated string that is an information-type message to user
f12
Double value to display in string form after the first string

MessageDialogString

a7 = 59
a0
Address of null-terminated string that is an information-type message to user
a1
Address of null-terminated string to display after the first string

MessageDialogFloat

a7 = 60
a0
Address of null-terminated string that is an information-type message to user
f12
Float value to display in string form after the first string

Read from file

a7 = 63
a0
File descriptor
a1
Address of input buffer
a2
Maximum number of characters to read
v0 after
Contains number of characters read (0 if end-of-file, negative if error)

Write to file

a7 = 64
a0
File descriptor
a1
Address of output buffer
a2
Number of characters to write
v0 after
Contains number of characters written (negative if error)

Exit2 (terminate with value)

a7 = 93
a0
Termination result
Note
Service 93 - If the RISCV program is run under control of the MARS graphical interface (GUI), the exit code in a0 is ignored.

Open file

a7 = 1024
a0
Address of null-terminated string containing filename
a1
Flags
a2
Mode
v0 after
Contains file descriptor (negative if error)
Note
Service 1024 - MARS implements three flag values: 0 for read-only, 1 for write-only with create, and 9 for write-only with create and append. It ignores mode. The returned file descriptor will be negative if the operation failed. MARS maintains file descriptors internally and allocates them starting with 3. File descriptors 0, 1 and 2 are always open for: reading from standard input, writing to standard output, and writing to standard error, respectively (new in release 4.3).