Search code examples
assemblymipssystem-callsmars-simulator

MIPS: System Calls


I have an assignment that is the following:

write and test a MIPS assembly language program that repeatedly reads in integers and adds them into a running sum. The program should stop when it gets an input that is 0, printing out the sum at that point. Use the MIPS system calls

when is it that I would need to invoke a system call in this program?


Solution

  • You need to to use system services (by making syscalls) when you want to provide input to your program or when you want to get output from your program. You do it by putting an appropriate code in the register $v0 and calling syscall.

    Read an integer and store it in $t0 ($v0 code for integer input is 5):

    addi $v0, $0, 5 
    syscall
    add  $t0, $0, $v0
    

    Print integer that is stored in $t0 (for integer output $v0 is 1):

    addi $v0, $0, 1
    add  $a0, $0, $t0
    syscall
    

    Other $v0 codes are listed in the reference that was provided to you in the comment to your question.