Skip to content

MARS syscalls

Eric Konks edited this page Mar 2, 2020 · 8 revisions

To make simulation interactive, we use system calls in the way MARS and SPIM simulators do.

MARS kernel is enabled by --mars option. Currently only little-endian MIPS and RISC-V are supported.

To make syscall in MIPS, use following instruction sequence:

   <load arguments>
   li $v0, <code>
   syscall

To make syscall in RISC-V, use following instruction sequence:

   <load arguments>
   li x2, <code>
   ecall

Table of system calls

Type Code in $v0 / x2 Arguments Result
print integer 1 $a0 / x4 = integer to print
print float 2 $f12 = float to print
print double 3 $f12 = double to print
print string 4 $a0 / x4 = address of null-terminated string to print
read integer 5 $v0 / x2 contains integer read
read float 6 $f0 contains float read
read double 7 $f0 contains double read
read string 8 $a0 / x4 = address of input buffer
$a1 / x5 = maximum number of characters to read
See note below table
sbrk
(allocate heap memory)
9 $a0 / x4 = number of bytes to allocate $v0 / x2 contains address of allocated memory
exit
(terminate execution)
10
print characte 11 $a0 / x4 = character to print
read character 12 $v0 / x2 contains character read
open file 13 $a0 / x4 = address of null-terminated string containing filename
$a1 / x5 = flags
$a2 / x6 = mode
$v0 / x2 contains file descriptor (negative if error).
read from file 14 $a0 / x4 = file descriptor $v0 / x2 contains number of characters read (0 if end-of-file, negative if error). 
write to file 15 $a0 / x4 = file descriptor
$a1 / x5 = address of output buffer
$a2 / x6 = number of characters to write
$v0 / x2 contains number of characters written (negative if error).
close file 16 $a0 / x4 = file descriptor
exit2 (terminate with value) 17 $a0 / x4 = termination result
Clone this wiki locally