A.2 OS Calls

Calling the operating system is done by the ecall instruction. The operation to be carried out is specified by the a0 register. Potential arguments to the system call are passed in the other argument registers.

Action a0 Description
print integer 1 a1 = number to be printed.
print string 4 a1 = address of ASCIIZ string.
convert integer 5 Return integer interpretation of null-terminated string from a1 in a0.
read integer 6 Return read number in a0.
read string 8 a1 = start address of memory area where characters are put.
a2 = maximum number of characters to read.
exit 10 End program execution.
print character 11 a1 = ASCII code of character to print.
read character 12 Return ASCII code of read character in a0.
enable terminal 0x130 Enables the VSCode terminal for receiving input.
read character poll 0x131 a0 = 0 if all input read
a0 = 1 if waiting for input
a0 = 2 if a character was read
a1 contains the character

A.2.1 Specialized OS calls

There are more ecalls for specialized tasks:

Action a0 Description
heap allocation (sbrk) 9 Allocates a a1 large segment on the heap returned in a0
open file 13 a1 filename, a2 permissions, returns filehandle in a0
read file 14 Reads a3 bytes from filehandle from a1
Writes them to address at a2
a0 contains number of bytes read
write file 15 a1 = filehandle
a2 = buffer with data
a3 = amount to write
a4 = size of each item
returns number of items written in a0
close file 16 a1 filehandle, return 0 if successfull, EOF otherwise
exit with error code 17 a1 error code
flush 18 a1 filehandle, return 0 if successfull, EOF otherwise
end of file 19 a1 filehandle, return a0 is non-zero if file at end
error check 20 a1 filehandle, return a0 is non-zero if file encountered errors
print hex 34 a1 number to print in hex