ecall

ecall

RISC-V Assembly Reference

lyszt.net covers ecall and other RISC-V instructions in more detail.

Description

ecall asks the environment your program runs in to perform a service. Assembly can add and move numbers, but the screen, the keyboard and other devices belong to the system that hosts the program, so the code cannot reach them on its own. ecall stands for environment call. It stops your code, the host carries out the service, and control comes back to your code. The host is an operating system on real hardware, and in this simulator it is the simulator itself.

The program names the service it wants. By convention the service number goes in a7, any data the service needs goes in a0, and then ecall runs. This simulator offers these services: 1 prints the integer in a0, 4 prints the text at the address in a0, 5 reads a number from input into a0, 10 ends the program, and 32 pauses for a0 milliseconds.

To print a number, load 1 into a7 to select the print service, load the number into a0, and run ecall. The example does this. Service 10 ends a program, which is why the lessons finish with the two lines below. Without it the processor continues past the last line into whatever comes next.

li a7, 10
ecall

The RISC-V manual states that ecall makes a service request to the execution environment, and that the environment decides how parameters are passed, usually in defined integer registers. This simulator is the environment here and it defines the service numbers listed above. The manual also notes that ecall was once named scall. Its companion ebreak returns control to a debugger. Both raise a precise requested trap to the environment.

A full operating system works the same way, with a longer list of services.

Example

li a7, 1     # service 1 = print integer
li a0, 42
ecall        # prints 42