RISC-V Syscalls

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 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

1 - Print integer

Arguments

a0
integer to print

2 - Print float

Arguments

f12
float to print

3 - Print double

Arguments

f12
double to print

4 - Print string

Arguments

a0
address of null-terminated string to print

5 - Read integer

Result

v0
contains integer read

6 - Read float

Result

f0
contains float read

7 - Read double

Result

f0
contains double read

8 - Read string

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.

Arguments

a0
address of input buffer
a1
maximum number of characters to read

9 - Sbrk (allocate heap memory)

Result

v0
contains address of allocated memory

Arguments

a0
number of bytes to allocate

10 - Exit (terminate execution)

11 - Print character

Service 11 - Prints ASCII character corresponding to contents of low-order byte.

Arguments

a0
character to print

12 - Read character

Result

v0
contains character read

17 - Get cwd

30 - Time (system time)

Service 30 - System time as milliseconds since 1 January 1970.

Result

a0
low order 32 bits of system time
a1
high order 32 bits of system time

34 - Print integer in hexadecimal

Displayed value is 8 hexadecimal digits, left-padding with zeroes if necessary.

Arguments

a0
integer to print

35 - Print integer in binary

Displayed value is 32 bits, left-padding with zeroes if necessary.

Arguments

a0
integer to print

36 - Print integer as unsigned

Displayed as unsigned decimal value.

Arguments

a0
integer to print

41 - Random int

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.

Result

a0
contains the next pseudorandom, uniformly distributed int value from this random number generator's sequence

Arguments

a0
i.d. of pseudorandom number generator (any int)

42 - Random int range

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.

Result

a0
contains pseudorandom, uniformly distributed int value in the range 0 <= [int] < [upper bound], drawn from this random number generator's sequence

Arguments

a0
i.d. of pseudorandom number generator (any int)
a1
upper bound of range of returned values

43 - Random float

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.

Result

f0
contains the next pseudorandom, uniformly distributed float value in the range 0.0 <= f < 1.0 from this random number generator's sequence

Arguments

a0
i.d. of pseudorandom number generator (any int)

44 - Random double

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.

Result

f0
contains the next pseudorandom, uniformly distributed double value in the range 0.0 <= f < 1.0 from this random number generator's sequence

Arguments

a0
i.d. of pseudorandom number generator (any int)

50 - ConfirmDialog

Result

a0
contains value of user-chosen option 0: Yes 1: No 2: Cancel

Arguments

a0
address of null-terminated string that is the message to user

51 - InputDialogInt

Result

a0
contains int read
a1
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

Arguments

a0
address of null-terminated string that is the message to user

52 - InputDialogFloat

Result

f0
contains float read
a1
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

Arguments

a0
address of null-terminated string that is the message to user

53 - InputDialogDouble

Result

f0
contains double read
a1
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

Arguments

a0
address of null-terminated string that is the message to user

54 - InputDialogString

See Service 8 note below table

Result

a1
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.

Arguments

a0
address of null-terminated string that is the message to user
a1
address of input buffer
a2
maximum number of characters to read

55 - MessageDialog

Arguments

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)

56 - MessageDialogInt

Arguments

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

57 - Close file

Arguments

a0
file descriptor

58 - MessageDialogDouble

Arguments

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

59 - MessageDialogString

Arguments

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

60 - MessageDialogFloat

Arguments

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

63 - Read from file

Result

v0
contains number of characters read (0 if end-of-file, negative if error)

Arguments

a0
file descriptor
a1
address of input buffer
a2
maximum number of characters to read

64 - Write to file

Result

v0
contains number of characters written (negative if error)

Arguments

a0
file descriptor
a1
address of output buffer
a2
number of characters to write

93 - Exit2 (terminate with value)

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

Arguments

a0
termination result

1024 - Open file

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).

Result

v0
contains file descriptor (negative if error)

Arguments

a0
address of null-terminated string containing filename
a1
flags
a2
mode