ElMassas
ElMassas

Reputation: 407

Understanding opening and reading a file in assembly

I've got a project for a class in which we have to transform a picture using sobel operators in assembly, and I'm having a bit of a problem understanding some example code.

In the data segment and in the main function is everything that's need to make the function work (The code works, I just don't understand why). The bit I don't understand is the use of the li command to change the values of the $aregisters.

read_rgb_image:
# Place file and size in register, 
move $s0, $a0 #file name
move $s1, $a1 #buffer
move $s2, $a2 #buffersize

# Open file
move $a0, $s0 # File's directory/name
li $a1, 0     #THIS LI ARE THE COMMANDS I DON'T UNDERSTAND
li $a2, 0     # Read life
li $v0, 13    # Option for opening file
syscall      

# Read file
move $a0, $v0  # File descriptor
move $a1, $s1  # Buffer with result
move $a2, $s2  # Space of information to read
li $v0, 14     # Read file
syscall

# Store read buffer
move $s1, $v0

# Close file
li $v0, 16 # Close file
syscall

# Return
move $v0, $s1 # Make return
jr $ra
nop

Can somebody please explain this to me?!

Upvotes: 1

Views: 487

Answers (2)

Adam
Adam

Reputation: 846

MIPS defines pseudoinstructions that are not actually part of the instruction set but are commonly used by programmers and compilers.

load immediate li is a pseudoinstruction it loads a 32-bit constant using a combination of lui and ori instructions.

The following instructions are equivalent.

MIPS instruction

lui $s0, 0x1234
ori $s0, 0xAA77

pseudoinstruction

li $s0, 0x1234AA77

Upvotes: 1

user3344003
user3344003

Reputation: 21627

The LI instruction loads an numeric value into a register. Before you make a system call, you need to load the $v0 register with the number assigned to the system service. You also need to load argument registers with system service specific parameters. Without the documentation, we can only guess (beyond the comments in the code) why the specific values being loaded into the registers. However, that is what is going on.

We can guess that you are calling the 4-paremeter version of the open function and that your specifing the file name, mode=0, and flags=0 using the argument registers.

Upvotes: 2

Related Questions