Home > Mobile >  How to avoid repetitive syscalls and moves in MIPS Assembly?
How to avoid repetitive syscalls and moves in MIPS Assembly?

Time:01-31

Here's a fragment of my code and as you can see the only difference between these three is the move from $v0 to $t0, $t1, or $t2. Does anyone know if there's a way to simplify these.

                        .text
    main:               #start of program
    loop:
    #read the ints (store them in t0, t1, t2)
    la  $a0, input_msg  #load input_msg into $a0 to prepare for syscall
    
    li  $v0, 4      #set $v0 to 4, which will print what $a0 points at
    syscall         #make the syscall
    li  $v0, 5      #prepare $v0 for reading an int
    syscall         #read the int
    move    $t0, $v0    #move the information from $v0 to $t0
    
    li  $v0, 4      #set $v0 to 4, which will print what $a0 points at
    syscall         #make the syscall
    li  $v0, 5      #prepare $v0 for reading an int
    syscall         #read the int
    move    $t1, $v0    #move the information from $v0 to $t1
    
    li  $v0, 4      #set $v0 to 4, which will print what $a0 points at
    syscall         #make the syscall
    li  $v0, 5      #prepare $v0 for reading an int
    syscall         #read the int
    move    $t2, $v0    #move the information from $v0 to $t1

CodePudding user response:

There's no way to simplify; that is already minimal.

Any syscall requires a function code to be placed into $v0.

So, another syscall requires repurposing of the $v0 register resulting in loss of the prior value unless it is preserved elsewhere.

This is one of the differences between assembly / machine code and other programming languages.  Other languages have logical variables that have names, types, scope/lifetime, and hold values a runtime.  A local variable will hold its value over a system or library call (unless explicitly being passed by reference, or in higher level languages than C, is closed over).

Whereas in machine code we have physical storage, namely registers and memory, and, especially the registers are often being repurposed.  So, that is something that the compiler and assembly programmers just have to deal with when mapping logical variables of our algorithms into the physical storage machine code.

  •  Tags:  
  • Related